1524: [poi2006] paltime limit: 5 sec memory limit: 357 MB
Submit: 308 solved: 101
[Submit] [Status] Description provides N input strings S1, S2 ,..., Perform the following Binary Group (I, j) for the SN) the number of Si + SJ is still the size of the input string. The total length of the input string cannot exceed 2 M bytes inputthe first line of input file contains the number of strings n. the following n lines describe each string: The I + 1-th line contains the length of the I-th string Li, then a single space and a string of Li small letters of English alphabet. you can assume that the total length of all strings will not exceed 2,000,000. two strings in different line may be the same. outputprint out only one integer, the number of palindromessample input6
2 AA
3 ABA
3 aaa
6 abaaba
5 aaaaa
4 Abba
Sample output14
Hintsource
Question:
Let's talk about the difficult process of doing this...
At first I saw the question drawing and found that the short string must be the prefix of the long string. Then I started to program happily and then wa... It is found that a and ABA obviously cannot form a text string...
Then I want to find that the short string does not need to overlap and overwrite the long string. Then I started to write the program, and then wa again. Then I found that AA and AAA can form the echo string...
In desperation, I consulted vfleaking and found this algorithm:
Lexicographic hash.
We first insert all strings into a trie tree, then calculate the hash of each string, and then enumerate each string, go down along the trie tree, and enumerate each prefix, determine whether the strings they are connected are the same as the strings they are connected,
You can use the hash function to determine whether to use the hash function.
The code is not easy to write, so my code is coming to the bottom...
Code:
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm> 10 11 #include<iostream> 12 13 #include<vector> 14 15 #include<map> 16 17 #include<set> 18 19 #include<queue> 20 21 #include<string> 22 23 #define inf 1000000000 24 25 #define maxn 2000000+10 26 27 #define maxm 2000000 28 29 #define eps 1e-10 30 31 #define ll long long 32 33 #define pa pair<int,int> 34 35 #define for0(i,n) for(int i=0;i<=(n);i++) 36 37 #define for1(i,n) for(int i=1;i<=(n);i++) 38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42 43 #define mod 1000000007 44 #define base 131 45 46 using namespace std; 47 48 inline int read() 49 50 { 51 52 int x=0,f=1;char ch=getchar(); 53 54 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 55 56 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 57 58 return x*f; 59 60 } 61 int n,tot,t[maxn][27],g[maxn],len[maxm]; 62 ll f[maxn],h[maxn],ans,ha[maxm]; 63 char s[maxn]; 64 string st[maxm]; 65 66 int main() 67 68 { 69 70 freopen("input.txt","r",stdin); 71 72 freopen("output.txt","w",stdout); 73 h[0]=1; 74 for1(i,maxn)h[i]=h[i-1]*base; 75 76 n=read();ans=-n; 77 for1(k,n) 78 { 79 len[k]=read(); 80 scanf("%s",s+1);st[k]=s+1; 81 int now=0;ll hash=0; 82 for1(i,len[k]) 83 { 84 int x=s[i]-‘a‘+1; 85 if(!t[now][x])t[now][x]=++tot; 86 now=t[now][x]; 87 hash=hash*base+x; 88 } 89 ha[k]=hash; 90 f[now]=k;g[now]++; 91 } 92 for1(k,n) 93 { 94 int now=0; 95 for0(i,len[k]-1) 96 { 97 int x=st[k][i]-‘a‘+1; 98 now=t[now][x]; 99 if(g[now]&&ha[f[now]]*h[len[k]]+ha[k]==ha[k]*h[i+1]+ha[f[now]])ans+=(ll)g[now]*2;100 }101 }102 printf("%lld\n",ans);103 104 return 0;105 106 }
View code
Bzoj1524: [poi2006] pal