Trie 字典树(附数据生成程序)
题意非常好懂。就问一堆单词两两比較要多少次。
只是非常忧伤的是卡优化了。写了个数据生成,然后我跟别人程序比。用时大概是别人的一倍。
别人635ms 过了。我就TLE…… 问题是2000ms啊。
给跪了。
附数据生成程序,各位能够生成数据看看。愿不要TLE。
我的TLE了……:
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<cmath> #define INF 0x7fffffff #define eps 1e-6 #define LL long long using namespace std; int n,m; LL ans; struct Trie { int word[4001*1001][26*2+11]; int sz,cot; int ex[4001*1001]; int fin[4001*1001]; void intTrie() { sz=1; cot=1; memset(word[0],0,sizeof(word[0])); ex[0]=0; fin[0]=0; } int shift(char s) { int c=0; if(s>='a'&&s<='z') c=s-'a'; else if(s>='A'&&s<='Z') c=s-'A'+26; else if(s>='0'&&s<='9') c=s-'0'+52; return c; } int insert(char *s) { int u=0,c; ans+=ex[0]; ex[0]++; for(int i=0; s[i]!='