poj 3376 Finding Palindromes
| Time Limit: 10000MS | Memory Limit: 262144K | |
| Case Time Limit: 2000MS | ||
Description
A word is called a palindrome if we read from right to left is as same as we read from left to right. For example, "dad", "eye" and "racecar" are all palindromes, but "odd", "see" and "orange" are not palindromes.
Given n strings, you can generate n × n pairs of them and concatenate the pairs into single words. The task is to count how many of the so generated words are palindromes.
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
3
1 a
2 ab
2 ba
Sample Output
5
Hint
aa aba aba abba baab
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 2000008
using namespace std;
bool f[][N];
long long ans;
char T[N],S[N];
int len,tot,root,id;
int st[N],ed[N],cnt;
int nxt[N],expand[N];
int trie[N][],mark[N],sum[N];
void getnxt(char *s,int ll,int rr)
{
int a=ll;
nxt[]=rr-ll+;
while(a+<=rr && s[a]==s[a+]) a++;
nxt[]=a-ll;
a=+ll;
int p,l,j;
for(int k=+ll;k<=rr;k++)
{
p=a-ll+nxt[a-ll]-; l=nxt[k-a];
if(k-ll+l->=p)
{
j=p-k+ll+> ? p-k+ll+ : ;
while(k+j<=rr && s[k+j]==s[j+ll]) j++;
nxt[k-ll]=j;
a=k;
}
else nxt[k-ll]=l;
}
}
void exkmp(char *s,char *t,int ll,int rr,int w)
{
getnxt(t,ll,rr);
int a=ll;
while(a<=rr && s[a]==t[a]) a++;
expand[]=a-ll;
a=ll;
int p,l,j;
for(int k=ll+;k<=rr;k++)
{
p=a-ll+expand[a-ll]-; l=nxt[k-a];
if(k-ll+l->=p)
{
j=p-k+ll+> ? p-k+ll+ : ;
while(k+j<=rr && s[k+j]==t[j+ll]) j++;
expand[k-ll]=j;
a=k;
}
else expand[k-ll]=l;
}
for(int i=ll-ll;i<=rr-ll;i++)
if(i+expand[i]==rr-ll+) f[w][i+ll]=true;
}
void insert(int ll,int rr)
{
root=;
for(int i=ll;i<=rr;i++)
{
id=S[i]-'a';
sum[root]+=f[][i];
if(!trie[root][id]) trie[root][id]=++tot;
root=trie[root][id];
}
mark[root]++;
}
void find(int ll,int rr)
{
root=;
for(int i=ll;i<=rr;i++)
{
id=T[i]-'a';
root=trie[root][id];
if(!root) return;
if(i!=rr&&f[][i+] || i==rr) ans+=mark[root];
}
ans+=sum[root];
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%s",&len,S+cnt);
for(int j=;j<len;j++) T[cnt+j]=S[cnt+len-j-];
st[i]=cnt;
ed[i]=cnt+len-;
exkmp(S,T,st[i],ed[i],);
exkmp(T,S,st[i],ed[i],);
cnt+=len;
insert(st[i],ed[i]);
}
for(int i=;i<=n;i++)
find(st[i],ed[i]);
printf("%lld\n",ans);
}
poj 3376 Finding Palindromes的更多相关文章
- POJ - 3376 Finding Palindromes(拓展kmp+trie)
传送门:POJ - 3376 题意:给你n个字符串,两两结合,问有多少个是回文的: 题解:这个题真的恶心,我直接经历了5种错误类型 : ) ... 因为卡内存,所以又把字典树改成了指针版本的. 字符串 ...
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...
- POJ 3376 Finding Palindromes(manacher求前后缀回文串+trie)
题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...
- POJ 3376 Finding Palindromes EX-KMP+字典树
题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...
- POJ 3376 Finding Palindromes (tire树+扩展kmp)
很不错的一个题(注意string会超时) 题意:给你n串字符串,问你两两匹配形成n*n串字符串中有多少个回文串 题解:我们首先需要想到多串字符串存储需要trie树(关键),然后我们正序插入倒序匹配就可 ...
- POJ - 3376 Finding Palindromes manacher+字典树
题意 给n个字符串,两两拼接,问拼接后的\(n\times n\)个字符串中有多少个回文串. 分析 将所有正串插入字典树中,马拉车跑出所有串哪些前缀和后缀为回文串,记录位置,用反串去字典树中查询,两字 ...
- poj3376 Finding Palindromes【exKMP】【Trie】
Finding Palindromes Time Limit: 10000MS Memory Limit: 262144K Total Submissions:4710 Accepted: 8 ...
- POJ3376 Finding Palindromes —— 扩展KMP + Trie树
题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS Memory Limit: 262 ...
- POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
随机推荐
- 使用Response.Write实现在页面的生命周期中前后台的交互
Response.Write()方法非常的常见,也很普通,就是向http output中输出一string.其输出的内容位于页面的最顶端,常用来实现显示一些页面消息框等逻辑. 一般来说,在页面的整个生 ...
- NIO初探
NIO的前世今生 NIO又叫NonBlockingI/O,即非阻塞I/O.以此对应的,有一个更常见的IO(BIO),又叫Blocking I/O,即阻塞IO,两种都为Java的IO实现方案. NIO/ ...
- 自学系列--git的基础简介
上学期第一次接触git,感觉挺难的,我们都知道这个非常重要,自己对git也自学了一段时间,下面这是对自学内容的总结,拿出来和大家一块交流一下,让我们一起成长吧! 一 git简介 Git是一个开源的分布 ...
- Beta完结--感想及吐槽
Beta冲刺结束啦!!! Beta冲刺结束啦!!! Beta冲刺结束啦!!! 这时候每个人的心情肯定都是非常激动的.随着Beta冲刺的结束,折磨了我们一整个学期的软工实践也差不多结束了.(实在是太不容 ...
- Alpha冲刺——第二天
Alpha第二天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- java定时执行任务(一)
需求: 经常遇到这样的需求:要求每天执行一次任务,执行任务时间是凌晨3点 实现: 为了便于检测,我假设的是下一分钟执行任务,每10秒重复执行.(对应现实项目:每天3点执行任务.那么就是下一个3点执行任 ...
- LintCode-8.旋转字符串
旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdefg&qu ...
- 分布式系统理论-terms
Distributed programming is the art of solving the same problem that you can solve on a single comput ...
- PowerMock用法[转]
转:http://agiledon.github.io/blog/2013/11/21/play-trick-with-powermock/ 当我们面对一个遗留系统时,常见的问题是没有测试.正如Mic ...
- html 怎么去掉网页的滚动条
<style type="text/css"> body{ overflow:scroll; overflow-x:hidden; } </style> 这 ...