20 10 646371829920732613433350295911348731863560763634906583816269 637943246892596447991938395877747771811648872332524287543417 420073458038799863383943942530626367011418831418830378814827 679789991249141417051280978492595526784382732523080941390128 848936060512743730770176538411912533308591624872304820548423 057714962038959390276719431970894771269272915078424294911604 285668850536322870175463184619212279227080486085232196545993 274120348544992476883699966392847818898765000210113407285843 826588950728649155284642040381621412034311030525211673826615 398392584951483398200573382259746978916038978673319211750951 759887080899375947416778162964542298155439321112519055818097 642777682095251801728347934613082147096788006630252328830397 651057159088107635467760822355648170303701893489665828841446 069075452303785944262412169703756833446978261465128188378490 310770144518810438159567647733036073099159346768788307780542 503526691711872185060586699672220882332373316019934540754940 773329948050821544112511169610221737386427076709247489217919 035158663949436676762790541915664544880091332011868983231199 331629190771638894322709719381139120258155869538381417179544 000361739177065479939154438487026200359760114591903421347697 [Key No. 1] 934134543994403697353070375063 [Key No. 2] 261985859328131064098820791211 [Key No. 3] 306654944587896551585198958148 [Key No. 4] 338705582224622197932744664740 [Key No. 5] 619212279227080486085232196545 [Key No. 6] 333721611669515948347341113196 [Key No. 7] 558413268297940936497001402385 [Key No. 8] 212078302886403292548019629313 [Key No. 9] 877747771811648872332524287543 [Key No. 10] 488616113330539801137218227609
Found key: [Key No. 9] [Key No. 5]
#include <cstdio> #include <cstring> char t[60005], tmp[10], key[65]; bool hash[60005]; struct node { node *next[10]; int id; node() { memset(next, NULL, sizeof(next)); id = -1; } }; void Insert(node *p, char *s, int id) { for(int i = 0; s[i] != '\0'; i++) { int idx = s[i] - '0'; if(p -> next[idx] == NULL) p -> next[idx] = new node(); p = p -> next[idx]; } p -> id = id; } void Search(node *p, char *s) { for(int i = 0; s[i] != '\0'; i++) { int idx = s[i] - '0'; if(p -> id != -1 && !hash[p -> id]) { printf(" [Key No. %d]", p -> id); hash[p -> id] = true; } if(p -> next[idx] == NULL) return; p = p -> next[idx]; } } int main() { int n, m, cnt1 = 0, cnt2 = 0; scanf("%d %d", &m, &n); getchar(); while(cnt2 < m) { char ch = getchar(); if(ch == '\n') cnt2++; else t[cnt1++] = ch; } t[cnt1] = '\0'; node *root = new node(); memset(hash, false, sizeof(hash)); for(int i = 1; i <= n; i++) { //tmp读取"[Key","No.","1] "这些没用字符串 scanf("%s%s%s%s", tmp, tmp, tmp, key); Insert(root, key, i); } printf("Found key:"); for(int i = 0; t[i] != '\0'; i++) Search(root, t + i); printf("\n"); }
原文地址:http://blog.csdn.net/tc_to_top/article/details/43919729