POJ 1816 Wild Words
Posted in PKU OJ on 四月 30th, 2010 by 纳米 – Be the first to comment2010-04-28,Coding,RE,WA,AC
解题报告:
其实这道题不想说什么,因为本来想用AC自动机实现,结果搞来搞去都实现不好,最后也只能枚举*匹配长度+判重解决。求AC自动机的方法……
顺便,后来Debug了很久,发现是把”Not match”打成了”No match”,郁闷
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <stdio.h> #include <stdlib.h> #include <string.h> const int maxn = 100003, maxstrlen = 25; int next[maxn], vis[maxn], ansarr[maxn], anslast; struct trieNode { trieNode *next[28], *pre; int mark; trieNode() { memset(next, NULL, sizeof(next)); pre = NULL; mark = 0; } } root; void delete_repeat(char text[]) { int i = 1, j = 0; do { if (text[i] != '*' || text[i-1] != '*') text[++j] = text[i]; } while (text[i++] != '\0'); } int num(char p) { if (p >= 'a' && p <= 'z') return p - 'a'; else if (p == '?') return 26; else return 27; } void add_trie(char text[], int z) { int len = strlen(text); trieNode *t = &root; for (int i = 0; i < len; ++i) { int p = num(text[i]); if (t->next[p] == NULL) t->next[p] = new trieNode; t = t->next[p]; } if (int w = t->mark) { while (next[w]) w = next[w]; next[w] = z; } else { t->mark = z; } } void match_trie(trieNode *t, char text[], int r) { if (t->next[27] != NULL) { int i = 0; do { match_trie(t->next[27], &text[i], r); } while (text[i++] != '\0'); } if (text[0] != '\0') { if (t->next[num(text[0])] != NULL) match_trie(t->next[num(text[0])], &text[1], r); if (t->next[26] != NULL) match_trie(t->next[26], &text[1], r); } else if (int w = t->mark) { if (vis[w] < r) { vis[w] = r; do { ansarr[anslast++] = w; w = next[w]; } while (w); } } } int comp(int *x, int *y) { return *x - *y; } int main() { int n, m; char text[maxstrlen]; memset(next, 0, sizeof(next)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%s", text); delete_repeat(text); add_trie(text, i); } memset(vis, 0, sizeof(vis)); for (int i = 1; i <= m; ++i) { scanf("%s", text); anslast = 0; match_trie(&root, text, i); if (anslast) { qsort(ansarr, anslast, sizeof(int), (int(*) (const void*, const void*)) comp); for (int j = 0; j < anslast-1; ++j) printf("%d ", ansarr[j]-1); printf("%d\n", ansarr[anslast-1]-1); } else { printf("Not match\n"); } } return 0; } |