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判断即可。
代码写起来不容易,我的代码快垫底了。。。
代码:

 #include<cstdio>

 #include<cstdlib>

 #include<cmath>

 #include<cstring>

 #include<algorithm>

 #include<iostream>

 #include<vector>

 #include<map>

 #include<set>

 #include<queue>

 #include<string>

 #define inf 1000000000

 #define maxn 2000000+10

 #define maxm 2000000

 #define eps 1e-10

 #define ll long long

 #define pa pair<int,int>

 #define for0(i,n) for(int i=0;i<=(n);i++)

 #define for1(i,n) for(int i=1;i<=(n);i++)

 #define for2(i,x,y) for(int i=(x);i<=(y);i++)

 #define for3(i,x,y) for(int i=(x);i>=(y);i--)

 #define mod 1000000007
#define base 131 using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; }
int n,tot,t[maxn][],g[maxn],len[maxm];
ll f[maxn],h[maxn],ans,ha[maxm];
char s[maxn];
string st[maxm]; int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
h[]=;
for1(i,maxn)h[i]=h[i-]*base; n=read();ans=-n;
for1(k,n)
{
len[k]=read();
scanf("%s",s+);st[k]=s+;
int now=;ll hash=;
for1(i,len[k])
{
int x=s[i]-'a'+;
if(!t[now][x])t[now][x]=++tot;
now=t[now][x];
hash=hash*base+x;
}
ha[k]=hash;
f[now]=k;g[now]++;
}
for1(k,n)
{
int now=;
for0(i,len[k]-)
{
int x=st[k][i]-'a'+;
now=t[now][x];
if(g[now]&&ha[f[now]]*h[len[k]]+ha[k]==ha[k]*h[i+]+ha[f[now]])ans+=(ll)g[now]*;
}
}
printf("%lld\n",ans); return ; }

BZOJ1524: [POI2006]Pal的更多相关文章

  1. 【BZOJ】1524: [POI2006]Pal

    题意 给出\(n\)个回文串\(s_i(\sum_{i=1}^{n} |s_i| \le 2000000)\)求如下二元组\((i, j)\)的个数\(s_i + s_j\)仍然是回文串. 分析 这道 ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. 第21/24周 性能监控(PAL工具)

    大家好,欢迎来到性能调优培训的最后一个月.在过去的5个月里,我们谈了SQL Server的各种性能相关的话题,包括性能调优的技术和问题. 但当在你面前,SQL Server没有按你预想的运行时,你会怎 ...

  4. PAL/NTSC 制电视广播技术有关知识--FPGA

    1.PAL和NTSC的区别 常见的电视信号制式是PAL和NTSC,另外还有SECAM等. NTSC即正交平衡调幅制,PAL为逐行倒像正交平衡调幅制. (1)PAL电视标准  PAL电视标准,每秒25帧 ...

  5. 【BZOJ】【1520】【POI2006】Szk-Schools

    网络流/费用流 比较裸的一道题 依旧是二分图模型,由源点S连向每个学校 i (1,0),「注意是连向第 i 所学校,不是连向学校的标号m[i]……唉这里WA了一次」 然后对于每所学校 i 连接 j+n ...

  6. BZOJ1510: [POI2006]Kra-The Disks

    1510: [POI2006]Kra-The Disks Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 265  Solved: 157[Submit][ ...

  7. bzoj 1513 [POI2006]Tet-Tetris 3D(二维线段树)

    1513: [POI2006]Tet-Tetris 3D Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 540  Solved: 175[Submit ...

  8. BZOJ1511: [POI2006]OKR-Periods of Words

    1511: [POI2006]OKR-Periods of Words Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 174  Solved: 92[Su ...

  9. Poi2006 Palindromes

    2780: Poi2006 Palindromes Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 15  Solved: 5[Submit][Stat ...

随机推荐

  1. 安装 Panda3D 并使用原有的Python

    Part 1:什么是Panda3D [原始网站] [中文版本] Part 2:注意事项 Panda3D的版本必须与Python相匹配 Part 3:使用已安装的Python 将‘C:\Panda3D- ...

  2. [转] Form表单中method="post/get'的区别

    Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...

  3. 挖掘微信Web版通信的全过程

    昨天是周末,在家闲得无聊,于是去weiphone.com逛了一圈,偶然发现有人发了一帖叫<微信 for Mac>,这勾起了我的好奇心,国内做Mac开发的人确实很少,对于那些能够独自开发一些 ...

  4. HTML5之部分显示

  5. HTML5之兴趣爱好

  6. Python之路,Day8 - Socket编程进阶

    Python之路,Day8 - Socket编程进阶   本节内容: Socket语法及相关 SocketServer实现多并发 Socket语法及相关 socket概念 socket本质上就是在2台 ...

  7. C#学习第四天

    今天主要学习了结构方面的知识,首先是定义,代码: struct<typeName> { <memberDeclarations> } struct route { public ...

  8. MySQL存储过程(一)

    1.1 CREATE  PROCEDURE (创建) CREATE PROCEDURE存储过程名 (参数列表) BEGIN SQL语句代码块 END 注意: 由括号包围的参数列必须总是存在.如果没有参 ...

  9. git 的记住用户名和密码和一些常用

    git config --global core.autocrlf falsegit config --global color.ui truegit config --global credenti ...

  10. Animation动画(一)

    Android的animation由四种类型组成:alpha(渐变透明度动画效果).scale(渐变尺寸伸缩动画效果).translate(画面转换位置移动动画效果).rotate(画面转移旋转动画效 ...