BZOJ1524: [POI2006]Pal

1524: [POI2006]Pal

Time Limit: 5 Sec  Memory Limit: 357 MB
Submit: 308  Solved: 101
[Submit][Status]

Description

给出n个回文串s1, s2, …, sn 求如下二元组(i, j)的个数 si + sj 仍然是回文串 规模 输入串总长不超过2M bytes

Input

The 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.

Output

Print out only one integer, the number of palindromes

Sample Input

6
2 aa
3 aba
3 aaa
6 abaaba
5 aaaaa
4 abba

Sample Output

14

HINT

Source

题解:
说一下做这题的艰辛过程。。。
刚开始看见题画了画图发现好像短串必须是长串的前缀,然后就开开心心的打程序,然后就WA了。。。发现 a 和 aba  显然不能构成回文串。。。
然后又想  发现短串好像需要不重叠覆盖长串,然后又开始写程序,然后又开始WA,后来发现 aa 和 aaa 能构成回文串。。。
无奈之下请教vfleaking,然后发现了这样的算法:
字典序hash。
我们先把所有串插入一个trie树,然后统计每个串的hash,然后再枚举每个串,沿trie树向下走,枚举每一个前缀,判断他们俩连起来的字符串正着和反着是否一样,
直接hash判断即可。
代码写起来不容易,我的代码快垫底了。。。
代码:


  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

 

posted @ 2014-10-12 16:15  ZYF-ZYF  Views(299)  Comments(0Edit  收藏  举报