Question: give n phone numbers (only composed of digits 0-9) and ask if one number is the prefix of another number (1 ≤ Number of test groups t ≤ 40, 1 ≤ n ≤ 10000 ). Question link: http://poj.org/problem? Id = 3630 --> This question is too speechless... Obviously, it's just a Trip, but it's all about TLE until the end of the game. The reason is too much... Pointer write Trip... The static array is used to write the Trip...
# Include <cstdio> # include <cstring> # include <queue> using namespace std; const int maxw = 10 + 5; const int maxc = 100000 + 10; char p [maxw]; bool OK, isp [maxc]; int ch [maxc] [10]; struct Trip {int sz; Trip () {sz = 1; memset (isp, 0, sizeof (isp); memset (ch, 0, sizeof (ch);} int idx (char c) {return c-'0 ';} void insert (char * s) {int len = strlen (s), I; int u = 0; for (I = 0; I <len; I ++) {int c = Idx (s [I]); if (! Ch [u] [c]) ch [u] [c] = sz ++; else {if (I = len-1) {OK = 0; // The current phone number is another phone prefix break;} if (isp [ch [u] [c]) {OK = 0; // In addition, the phone number is the current phone prefix break;} u = ch [u] [c];} isp [u] = 1;} void solve () {if (OK) puts ("YES"); else puts ("NO") ;}}; int main () {int t, n; scanf ("% d ", & t); while (t --) {OK = 1; scanf ("% d", & n); Trip trip; for (int I = 0; I <n; I ++) {scanf ("% s", p); if (OK) trip. insert (p);} trip. solve ();} return 0 ;}