UVA 11732 strcmp() Anyone?(trie)
2013-10-07 16:54
441 查看
题目大意:给你那个 strcmp 函数的内部,现在给你n个字符串,要两两比较,问你strcmp里判断总共用了几次?
思路:trie,又是Submission error,算了,先存下吧。。
代码如下(不知道对不对。。 = =):
思路:trie,又是Submission error,算了,先存下吧。。
代码如下(不知道对不对。。 = =):
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long lld; const int MAXN = 4001; const int MAX_NODE = MAXN*1001; const int SIGMA_SIZE = 27; int ch[MAX_NODE][SIGMA_SIZE]; int val[MAX_NODE]; struct Trie { int sz; Trie() { sz = 1; val[0] = 0; memset(ch[0],0,sizeof(ch[0])); } int idx(char c) { if(c == '\0') return 0; else return c - 'a' + 1; } void insert(char *s) { int u = 0; int len = strlen(s); for(int i = 0;i<=len;i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } val[u]++; u = ch[u][c]; } } int count(char *s) { int u = 0; int len = strlen(s); int cnt = 0; for(int i = 0;i<=len;i++) { cnt += val[u]; int c = idx(s[i]); if(!ch[u][c]) return cnt; cnt += val[u]; u = ch[u][c]; } return cnt; } }; char str[MAXN]; int main() { int cas = 0; int n; while(~scanf("%d",&n)&&n) { Trie tr; lld ans = 0; for(int i = 1;i<=n;i++) { scanf("%s",str); if(i > 1) ans += tr.count(str); //printf("ans = %d\n",ans); tr.insert(str); } printf("Case %d: %lld\n",++cas,ans); } return 0; }
相关文章推荐
- UVA 11732 strcmp() Anyone (Trie+链表)
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
- UVA 11732-"strcmp()" Anyone?-trie(左儿子右兄弟表示法(省空间))
- uva 11732 "strcmp()" Anyone? (trie+左儿子右兄弟表示法)
- UVA 11732 strcmp() Anyone 左兄弟右孩子Trie
- Trie UVA 11732 "strcmp()" Anyone?
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
- UVA 11732 strcmp() Anyone?(Trie的性质)
- UVA 11732 strcmp() Anyone?(左儿子右兄弟Trie)
- UVA 11732 "strcmp()" Anyone? (Trie)
- UVA 11732("strcmp()" Anyone?-Trie与左兄弟右儿子)
- uva 11732 - strcmp() Anyone? 不错的Trie题
- UVA 11732 "strcmp()" Anyone? 前缀树(Trie)+简单搜索
- UVA 11732 strcmp() Anyone (Trie)
- uva 11732 - strcmp() Anyone? 不错的Trie题
- UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie
- Uva 11732 "strcmp()" Anyone? 左儿子右兄弟的trie
- UVA 11732 - strcmp() Anyone?(Trie)
- UVa11732 - "strcmp()" Anyone?(Trie【左儿右兄)
- (uva 11732) "strcmp()" Anyone? (trie+左儿子右兄弟表示法)