BZOJ1524: [POI2006]Pal
1524: [POI2006]Pal
Time Limit: 5 Sec Memory Limit: 357 MB
Submit: 308 Solved: 101
[Submit][Status]
Description
求如下二元组(i, j)的个数
si + sj 仍然是回文串
规模
输入串总长不超过2M bytes
Input
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
Sample Input
2 aa
3 aba
3 aaa
6 abaaba
5 aaaaa
4 abba
Sample Output
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的更多相关文章
- 【BZOJ】1524: [POI2006]Pal
题意 给出\(n\)个回文串\(s_i(\sum_{i=1}^{n} |s_i| \le 2000000)\)求如下二元组\((i, j)\)的个数\(s_i + s_j\)仍然是回文串. 分析 这道 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 第21/24周 性能监控(PAL工具)
大家好,欢迎来到性能调优培训的最后一个月.在过去的5个月里,我们谈了SQL Server的各种性能相关的话题,包括性能调优的技术和问题. 但当在你面前,SQL Server没有按你预想的运行时,你会怎 ...
- PAL/NTSC 制电视广播技术有关知识--FPGA
1.PAL和NTSC的区别 常见的电视信号制式是PAL和NTSC,另外还有SECAM等. NTSC即正交平衡调幅制,PAL为逐行倒像正交平衡调幅制. (1)PAL电视标准 PAL电视标准,每秒25帧 ...
- 【BZOJ】【1520】【POI2006】Szk-Schools
网络流/费用流 比较裸的一道题 依旧是二分图模型,由源点S连向每个学校 i (1,0),「注意是连向第 i 所学校,不是连向学校的标号m[i]……唉这里WA了一次」 然后对于每所学校 i 连接 j+n ...
- BZOJ1510: [POI2006]Kra-The Disks
1510: [POI2006]Kra-The Disks Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 265 Solved: 157[Submit][ ...
- bzoj 1513 [POI2006]Tet-Tetris 3D(二维线段树)
1513: [POI2006]Tet-Tetris 3D Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 540 Solved: 175[Submit ...
- BZOJ1511: [POI2006]OKR-Periods of Words
1511: [POI2006]OKR-Periods of Words Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 174 Solved: 92[Su ...
- Poi2006 Palindromes
2780: Poi2006 Palindromes Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 15 Solved: 5[Submit][Stat ...
随机推荐
- Jupyter Notebook 对LaTeX 的支持
Jupyter的Markdown模式比我原来想想的更加强大:它支持LaTeX! 支持的特性不多,应该能满足一般的需求了.
- HDU 4287 Intelligent IME
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- [转] C++中临时对象及返回值优化
http://www.cnblogs.com/xkfz007/articles/2506022.html 什么是临时对象? C++真正的临时对象是不可见的匿名对象,不会出现在你的源码中,但是程序在运行 ...
- codevs 2612 最有分解方案 (贪心)
/* 数字不重复 将一个正整数分解成若干的整数的和 数字不重复 且数字不相同 保证不重复的话 贪心策略是从2开始分 然后把最后剩下的数均匀分到后面 证明嘛 这里写的可能不是很严谨 对于一个n 如果我们 ...
- Jquery小东西收集
1. $(document).ready(),$(function(){}),$(window).load(),window.onload的关系与区别 $(document).ready(functi ...
- <c:if>判断参数是否同时为空
<c:if test="${empty str}"> str为空</c:if> <c:if test="${not empty str}& ...
- maven指定构建的编码格式
pom.xml文件添加如下内容: <properties> <project.build.sourceEncoding>UTF-8</project.build.s ...
- MySql Connector/Net Mysql like 搜索中文的问题(c#和asp.net连接mysql)
Connector/Net 6.9.8 选择.net/mono即可,不需要安装. 将对应版本的MySql.Data.dll复制到bin目录下即可使用 http://dev.mysql.com/down ...
- 在eclipse中新建Dynamic web project时选择2.5和3.0的区别(里面涉及servlet和tomcat的问题)
1.是指servlet的版本,是2.5的还是3.0的 servlet3.0以后支持异步 2.dynamic web module和对应的TOMCAT 版本 http://blog.sina.com.c ...
- 11_RHEL安装Maya2015
1. 解压 tar -xvf ./Autodesk_Maya_English_2015_Linux_64bit.tgz 2. 运行 ./setup 2.1补充 如果提示缺少 libpng12.so.0 ...