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. Fragment 回退栈 传递参数,点击切换图片使用Fragment ListView

    Fragment回退栈  类似与Android系统为Activity维护一个任务栈,我们也可以通过Activity维护一个回退栈来保存每次Fragment事务发生的变化. 如果你将Fragment任务 ...

  2. C\C++代码优化的27个建议

    1. 记住阿姆达尔定律: funccost是函数func运行时间百分比,funcspeedup是你优化函数的运行的系数. 所以,如果你优化了函数TriangleIntersect执行40%的运行时间, ...

  3. SVN Checkout 不包括源文件夹根目录(转)

    SVN Checkout 不包括源文件夹根目录,比如我要checkout   trunk/ 下面的所有文件,但是不包括trunk 文件夹 我们可以在svn文件夹后面打个空格,在加个“.”就行了 eg: ...

  4. 让你的WizFi250适应各种气候

    这篇文章会具体描写叙述如何马上得到指定城市的天气状况(比方首尔).由OpenWeatherMap提供. 用JSON(由OpenWeatherMap提供),XML和一个以太网模块.使WIZnet-Wiz ...

  5. 使用EPEL和REMI第三方yum源

    http://dl.fedoraproject.org/pub/epel/ epel-release-latest-.noarch.rpm redhat5 epel-release-latest-.n ...

  6. hdu1258 Sum It Up (DFS)

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  7. [转] linux系统文件流、文件描述符与进程间关系详解

    http://blog.sina.com.cn/s/blog_67b74aea01018ycx.html linux(unix)进程与文件的关系错综复杂,本教程试图详细的阐述这个问题. 包括:     ...

  8. Android(java)学习笔记238:多媒体之图片画画板案例

    1.首先我们编写布局文件activity_main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/a ...

  9. codevs 1183 泥泞的道路 (二分+SPFA+差分约束)

    /* 二分答案(注意精度) 对于每一个答案 有(s1+s2+s3...)/(t1+t2+t3...)>=ans 时符合条件 这时ans有变大的空间 对于上述不等式如果枚举每一条路显得太暴力 化简 ...

  10. XML参数转换为Object,并转换为List或DataTable

    demo效果: