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. 排序(5)---------高速排序(C语言实现)

    继shell发明了shell排序过后呢,各位计算机界的大牛们又開始不爽了,为什么他能发明.我就不能发明呢.于是又有个哥们蹦出来了.哎...那么多排序,就木有一个排序是中国人发明的.顺便吐槽一下,一百年 ...

  2. mybatis 与 ehcache 整合[转]

    1.简介 MyBatis 是支持普通SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC 代码和参数的手工设置以及结果集的检索. Ehcache 是现在最流行的纯 ...

  3. CoreText实现图文混排之点击事件

    今天呢,我们继续把CoreText图文混排的点击事件补充上,这样我们的图文混排也算是圆满了. 哦,上一篇的链接在这里 http://www.jianshu.com/p/6db3289fb05d Cor ...

  4. NSIndexPath初始化

    在UITableView中经常用到这个类,但一直不知道怎么初始化,网上抄录的代码如下,果然好用 NSIndexPath *index = [NSIndexPath indexPathForRow:0 ...

  5. TCP/IP协议原理与应用笔记08:对等层和对等实体

    1. 我们直接通过下面这个图,就可以直观了解: Outlook :收发邮件的软件组件. IE:浏览器. CutFTP:文件传输工具. 小结:因为为了完成不同的功能,所以会出现不同实体,这些不同的实体为 ...

  6. Android(java)学习笔记239:多媒体之撕衣服的案例

    1.撕衣服的案例逻辑:       是两者图片重叠在一起,上面我们看到的是美女穿衣服的图片,下面重叠(看不到的)是美女没有穿衣服的图片.当我们用手滑动画面,上面美女穿衣服的图片就会变成透明,这样的话下 ...

  7. DataTable数据与Excel表格的相互转换

    using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...

  8. HighCharts常用设置(摘抄笔录)

  9. python中的generator, iterator, iterabel

    先来看看如果遇到一个对象,如何判断其是否是这三种类型: from types import GeneratorType from collectiuons import Iterable, Itera ...

  10. MD5加密相关

    demo效果