Portál AbcLinuxu, 11. května 2025 10:00
typedef struct retezce {
char retezec[21];
struct retezce *dalsi;
} RETEZCE;
tak 6 z 10 testovacich prikladu skoncilo na chybe:
*** buffer overflow detected ***: /run-1346315753-1140906172/solution terminated ======= Backtrace: ========= /lib/i386-linux-gnu/tls/i686/nosegneg/libc.so.6(__fortify_fail+0x45)[0xb7635dd5] /lib/i386-linux-gnu/tls/i686/nosegneg/libc.so.6(+0xfebaa)[0xb7634baa] /lib/i386-linux-gnu/tls/i686/nosegneg/libc.so.6(+0xfdedd)[0xb7633edd] /run-1346315753-1140906172/solution[0x8048d41] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048ceb] /run-1346315753-1140906172/solution[0x8048902] /lib/i386-linux-gnu/tls/i686/nosegneg/libc.so.6(__libc_start_main+0xf3)[0xb754f4d3] /run-1346315753-1140906172/solution[0x8048a91] ======= Memory map: ======== 08048000-0804a000 r-xp 00000000 ca:02 18850134 /run-1346315753-1140906172/solution 0804a000-0804b000 r--p 00001000 ca:02 18850134 /run-1346315753-1140906172/solution 0804b000-0804c000 rw-p 00002000 ca:02 18850134 /run-1346315753-1140906172/solution 09240000-0b406000 rw-p 00000000 00:00 0 [heap] b729f000-b74eb000 rw-p 00000000 00:00 0 b74eb000-b7507000 r-xp 00000000 ca:01 394527 /lib/i386-linux-gnu/libgcc_ ...pokud jsem ale poradi prvku v teto strukture otocil,
typedef struct retezce {
struct retezce *dalsi;
char retezec[21];
} RETEZCE;
tak je vysledek dobre pro vsechny testovana data.
Co tedy presne ta chybova hlaska znamena a proc zrovna otoceni prvku v te strukture to resi? Kdybych si ten ukazatel prepisoval, tak by mi ten program nefungoval vubec, tim to nebude.
Řešení dotazu:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
typedef struct pismena {
int pocet[256];
struct pismena *next[256];
} PISMENA;
typedef struct retezce {
char retezec[21];
struct retezce *dalsi;
} RETEZCE;
typedef struct vysledek {
//int pole[300000];
retezce *next[300001];
retezce *last[300001];
} VYSLEDEK;
void store(pismena *memory, char *line, int length) {
if(length == 1) {
memory->pocet[line[0]]++;
return;
}
if(memory->next[line[0]] == NULL) {
memory->next[line[0]] = (PISMENA *) malloc(sizeof(PISMENA));
for(int i=0; i<256; i++) {
memory->next[line[0]]->pocet[i] = 0;
memory->next[line[0]]->next[i] = NULL;
}
}
store(memory->next[line[0]], line + 1, strlen(line + 1));
}
void uloz(VYSLEDEK *vysled, int kam, char* co) {
retezce *retez = (RETEZCE *) malloc(sizeof(RETEZCE));
retez->dalsi = NULL;
strcpy(retez->retezec, co);
/* cout << "co ";
cout << co;
cout << '\n';*/
if(vysled->next[kam] == NULL) {
vysled->next[kam] = retez;
vysled->last[kam] = retez;
} else {
vysled->last[kam]->dalsi = retez;
vysled->last[kam] = retez;
}
}
void search(VYSLEDEK *vysled, PISMENA *memory, char *max, int depth, char *curr) {
for(int i=32; i<256; i++) {
curr[depth] = (char)i;
curr[depth + 1] = '\0';
if(memory->pocet[i] > 0) {
uloz(vysled, memory->pocet[i], curr);
//strcpy(max, curr);
}
if(memory->next[i] != NULL) {
search(vysled, memory->next[i], max, depth + 1, curr);
}
}
}
void remove(PISMENA *memory, char *max) {
if(strlen(max) == 1) {
memory->pocet[max[0]] = 0;
} else {
remove(memory->next[max[0]], max + 1);
}
}
int main (void) {
int total;
cin >> total;
char line[21];
char max[21];
char curr[21];
int maxval;
PISMENA *memory = (PISMENA *) malloc(sizeof(PISMENA));
for(int i=0; i<256; i++) {
memory->pocet[i] = 0;
memory->next[i] = NULL;
}
for(int i=0; i < total; i++) {
cin >> line;
store(memory, line, strlen(line));
}
int k;
cin >> k;
/*
for(int i=0; i < k; i++) {
max[0] = '\0';
curr[0] = '\0';
maxval = 0;
search(memory, max, &maxval, 0, curr);
cout << max;
cout << '\n';
remove(memory, max);
}
*/
VYSLEDEK *vysled = (VYSLEDEK *) malloc(sizeof(VYSLEDEK));
max[0] = '\0';
curr[0] = '\0';
maxval = 0;
for (int i=0; i<300000; i++) {
vysled->next[i] = NULL;
vysled->last[i] = NULL;
}
search(vysled, memory, max, 0, curr);
int counter = 0;
int pos = 300000 - 1;
while(pos >= 0) {
/* cout << "pos ";
cout << pos;
cout << '\n';*/
if(vysled->next[pos] != NULL) {
RETEZCE *ret = vysled->next[pos];
while(ret != NULL) {
cout << ret->retezec;
cout << '\n';
counter++;
/* cout << counter;
cout << '\n';*/
if (counter == k) {
/* cout << "koncim\n";*/
/*while(true) {
}*/
exit(0);
}
if (ret != vysled->last[pos]) {
ret = ret->dalsi;
} else {
ret = NULL;
}
}
}
pos--;
}
}
Pro jistotu jeste kompletni zadani:
Frequency Counting of Words / Top N words in a document. Given N terms, your task is to find the k most frequent terms from given N terms. Input format : First line of input contains N, denoting the number of terms to add. In each of the next N lines, each contains a term. Next line contains k, most frequent terms. Output format : Print the k most frequent terms in descending order of their frequency. If two terms have same frequency print them in lexicographical order. Sample input : 14 Fee Fi Fo Fum Fee Fo Fee Fee Fo Fi Fi Fo Fum Fee 3 Sample output : Fee Fo Fi Constraint : 0 < N < 300000 0 < term length < 20.
0 < term length < 20tedy slova delsi nez 20 znaku nejsou povolena.
0 < term length < 25
Podle vysledku jsem asi motivoval i par lidi, aby si to take vyzkouseli :)
All runtime errors (segfaults, stack overflows, uncaught exceptions, etc) will also show up as 'Wrong answer'. Pokud by se objevila pouze odpoved Wrong answer, tak bych na to, ze to ten server testuje vstupem mimo rozsah zadani, asi nikdy neprisel. Mozna, ze se znalost tohoto triku, tedy ze server muze vratit nejen Passed nebo Wrong, ale i behovou chybu, bude hodit v nekterem z pristich pokracovani oblibeneho podrate.cz
std::unordered_map
. Výsledný program bude mít tak 20 řádků (pořád víc než two-liner v Pythonu, ale už přijatelně).
typedef struct retezce {
struct retezce *dalsi;
char retezec[21];
} RETEZCE;
je pote alokovano stejne jako
typedef struct retezce {
struct retezce *dalsi;
char retezec[24];
} RETEZCE;
a pak tato chyba neni odchycena. Nebo se mylim?
Ano, to zarovnání je dost pravděpodobné.
Podstatné je ale něco úplně jiného: pokud chcete někdy programovat i něco jiného než jen umělé školní příklady, ve vlastním zájmu co nejrychleji zapomeňte na přístup "To, že mi program padá, je chyba nekorektních vstupních dat, program je v pořádku." Čtete-li data od uživatele, ze souboru nebo po síti, musíte počítat s tím, že nemusejí splňovat formální požadavky, a váš program se s tím musí v mezích možností rozumně vypořádat. A ne, segfault není rozumné vypořádání se.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.