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 海底总动员.... 这个题開始 ...
随机推荐
- lvs+keepalived详解
常用软件安装及使用目录 资源链接:https://pan.baidu.com/s/15rFjO-EnTOyiTM7YRkbxuA 网盘分享的文件在此 官网:http://www.linuxvir ...
- JavaScript筑基篇(二)->JavaScript数据类型
说明 介绍JavaScript数据类型 目录 前言 参考来源 前置技术要求 JavaScript的6种数据类型 哪6种数据类型 undefined 类型 null 类型 boolean 类型 numb ...
- nginx 添加的配置信息
使用logrotate管理Nginx日志配置如下: [root@vm-10-129-93-51 nginx]# vi /etc/logrotate.d/nginx /letv/log/nginx/*. ...
- Java中I/O流之Print流
Java 中的 print 流: print 流用于做输出将会非常的方便,并且具有以下特点: 1. printWriter.printStream 都属于输出流,分别针对字符,字节. 2. print ...
- 20个实用的Linux命令
20个实用的Linux命令 2016-04-16 程序员之家 1. 命令:sl (蒸汽机车) 你可能了解 ‘ls’ 命令,并经常使用它来查看文件夹的内容.但是,有些时候你可能会拼写成 ‘sl’ ,这时 ...
- cacti 安装perl 和XML::Simple
一.安装perl #tar zxvf perl-5.20.1.tar.gz #cd perl-5.20.1 #./Configure -de #make #make test #make in ...
- Tiny4412 LED 程序
package cn.hyc.led; import android.os.Bundle; import android.app.Activity; import android.view.Menu; ...
- WPF绑定xaml中绑定对象需用属性表示,字段不可以绑定
在练习WPF绑定时发现对象属性可以在XAML中绑定,但字段是不可以绑定: 比如: private Person person{get;set;} 可以绑定到XAML中,<TextBox Nam ...
- 【bzoj1304】[CQOI2009]叶子的染色 树形dp
题目描述 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到每个叶子的简单路径上都至少包含一个有色结点( ...
- BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)
一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...