pku 1816Wild Words
2009-12-06 11:05
295 查看
http://acm.pku.edu.cn/JudgeOnline/problem?id=1816
Trie+搜索。Trie很简单,不过这个题有点怪,可能有相同的pattern,这个导致我wa了一次,所以我在trie里面加了个ID单链表,结果还是wa!!样例随便都过得了,后来自己写了组数据,结果答案就不对啦!有一组有个pattern 是4个*号的,结果不知道输出多少个相同的数字了。||—_—|| 因为我的DFS中对*的搜索会一直继续,所以一个pattern有很多*时,就会出现很多相同的ID。结果不是说每个数字都有空格吗?我不敢测了—_—//
一次CE(sort没加头文件),3次wa , 最后还是422ms猥琐的A了。
Trie+搜索。Trie很简单,不过这个题有点怪,可能有相同的pattern,这个导致我wa了一次,所以我在trie里面加了个ID单链表,结果还是wa!!样例随便都过得了,后来自己写了组数据,结果答案就不对啦!有一组有个pattern 是4个*号的,结果不知道输出多少个相同的数字了。||—_—|| 因为我的DFS中对*的搜索会一直继续,所以一个pattern有很多*时,就会出现很多相同的ID。结果不是说每个数字都有空格吗?我不敢测了—_—//
一次CE(sort没加头文件),3次wa , 最后还是422ms猥琐的A了。
#include<iostream> #include<algorithm> using namespace std; const int size = 100005; const int kind = 28; char Word[25]; int WordLen, pLen, Match[size]; int N, M; struct Id { int ID; Id *next; }; struct Node { Id *pID; int son[kind]; void Init() { if (pID!=NULL) delete pID; pID = NULL; memset(son, -1, sizeof(son)); } }node[size*7]; int Num = 0; int Index(char x) { if (x == '?') return 26; else if (x == '*') return 27; else return (x - 'a'); } void Insert(char *s, int id) { int r = 0, j; for (int i = 0; s[i]; i++) { j = Index(s[i]); if (node[r].son[j] == -1) { Num++; node[Num].Init(); node[r].son[j] = Num; } r = node[r].son[j]; } if (node[r].pID == NULL) { Id *x = new Id; x->ID = id; x->next = NULL; node[r].pID = x; } else { Id *x = new Id; x->ID = id; x->next =node[r].pID; node[r].pID = x; } } void Dfs(char *str, int pos, int cur)/*str单词,pos单词的下标,cur字典树的当前结点*/ { if (pos >= WordLen) { Id *x = node[cur].pID; while (x != NULL) { if (x->ID != Match[pLen-1])/*可以避免大部分相同的ID进来*/ Match[pLen++] = x->ID; x = x->next; } } else { int index = Index(str[pos]);/*字母的下标*/ if (node[cur].son[index] != -1) Dfs(str, pos+1, node[cur].son[index]); if (node[cur].son[26] != -1) Dfs(str, pos+1, node[cur].son[26]); } if (node[cur].son[27] != -1) { for (int i = pos; i <= WordLen; i++) Dfs(str, i, node[cur].son[27]); } } int main() { int i, j; while (scanf("%d%d",&N,&M) != EOF) { Num = 0; node[0].Init(); for (i = 0; i < N; i++) { scanf("%s", Word); Insert(Word, i); } Match[0] = -1254213;/**/ for (i = 0; i < M; i++) { scanf("%s", Word); pLen = 1; WordLen = strlen(Word); Dfs(Word,0,0); if (pLen == 1) printf("Not match/n"); else { sort(Match, Match+pLen); // printf("%d", Match[1]); for (j = 1; j < pLen; j++) if (Match[j]!=Match[j-1])/*不能输出相同的ID*/ printf("%d ", Match[j]);/*结果证明带空格也能AC 407MS*/ printf("/n"); } } } return 0; }
相关文章推荐
- POJ 1816 Wild Words(trie 树上的DFS)
- POJ 1816 Wild Words 字典树+DFS
- poj 1816 Wild Words(字典树+DFS)
- POJ 1816 Wild Words(字典树+dfs)
- POJ 1816 Wild Words
- POJ - 1816 Wild Words(字典树变题)
- UESTC 1214 divide && PKU 3390 Print Words in Lines [DP]
- pku 1816 Wild Words
- POJ1816--Wild Words(Trie+搜索)
- pku1816 Wild Words (trie)
- POJ1816:Wild Words——题解
- PKU 1386 Play on Words 判断图中是否存在欧拉回路、欧拉路
- pku2192(并查集+枚举)
- POJ 1004Financial Management解题报告——【PKU ACM】
- pku2001
- PKU 2418 字符串排序
- PKU 1191
- pku-3750 小孩报数问题
- istringstream。PKU2493 Rdeaalbe。
- pku acm题目分类 (1)