poj3376 KMP+字典树求回文串数量(n*n)
| Time Limit: 10000MS | Memory Limit: 262144K | |
| Total Submissions: 4043 | Accepted: 746 | |
| 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<vector>
typedef long long LL;
using namespace std;
const int N=;
struct node{
int sum,son[],cnt;
}no[N];
struct str{
int start,en;
str(int s,int e):start(s),en(e){};
str(){};
};
vector<str>ve;
int fla[N][],nexta[N],tot=,n,pre,l;
char s[N],t[N];
void getNext(char *a,int la){
int i=,j=-;
nexta[]=-;
while(i<la){
if(j==-||a[i]==a[j]) nexta[++i]=++j;
else j=nexta[j];
}
}
void kmp(int flag,char *a,char *b,int la,int start){
int i=,j=;
while(i<la){
if(j==-||a[j]==b[i]) ++i,++j;
else j=nexta[j];
}
int pre=j;
if(!flag) {
while(pre){
fla[start+pre-][]=;
pre=nexta[pre];
}
}
else {
while(pre){
fla[start+l-pre][]=;
pre=nexta[pre];
}
}
}
void insertTire(int pre,char *a,int start,int la)//储存前缀
{
for(int i=;i<l;i++)
{
if(!no[pre].son[a[i]-'a'])
{
tot++;
no[pre].son[a[i]-'a'] = tot;
pre = tot;
}
else pre = no[pre].son[a[i]-'a'];
if(i+<la)no[pre].cnt+=fla[start+i+][];
}
no[pre].sum++;
}
LL findTrie(int start,int en,int pre){
int sym=true;
LL ans=;
int l=en-start;
for(int i=start;i<en;++i) {
if(no[pre].son[t[i]-'a']) {
pre=no[pre].son[t[i]-'a'];
if(fla[start+l-(i-start+)-][]||i==en-) ans+=no[pre].sum;
}
else {
sym=;break;
}
}
if(sym) ans+=no[pre].cnt;
return ans;
}
int main(){
pre=;
LL ans=;
for(scanf("%d",&n);n--;){
scanf("%d %s",&l,s+pre);
ve.push_back(str(pre,pre+l));
for(int i=pre;i<pre+l;++i) t[i]=s[pre+l-(i-pre+)];
getNext(s+pre,l);
kmp(,s+pre,t+pre,l,pre);
getNext(t+pre,l);
kmp(,t+pre,s+pre,l,pre);
insertTire(,s+pre,pre,l);
pre+=l;
}
for(int i=;i<(int)ve.size();++i) ans+=findTrie(ve[i].start,ve[i].en,);
printf("%I64d\n",ans);
}
poj3376 KMP+字典树求回文串数量(n*n)的更多相关文章
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- 拓展KMP求回文串
题目:hdu3613: 题意:有26字母对应的价值,然后给出以个串,把它分成两段字串,如果字串是回文串,串的价值就是每个字符和,不是就为0.求最大价值. 博客 分析:拓展KMP的应用求回文字串. #i ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- Palindrome - URAL - 1297(求回文串)
题目大意:RT 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
- manacher算法,求回文串
用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...
- 马拉车,O(n)求回文串
马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- 3676: [Apio2014]回文串 求回文串长度与出现次数的最大值
「BZOJ3676」[Apio2014] 回文串 Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所 ...
- hdu 3294 manacher 求回文串
感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...
随机推荐
- Python封装应用程序的最佳项目结构是什么?
Python封装应用程序的最佳项目结构是什么? 转载来源于stackoverflow:https://stackoverflow.com/questions/193161/what-is-the-be ...
- SSH 超时设置
在阿里云买了一台乞丐版服务器,搭了一个博客,安装了java,mysql,redis等服务,把以前写的知乎爬虫部署上去,看看爬取效果.程序运行一段时间后,发现cmder上的日志不打了,我原以为爬虫挂了, ...
- 2016年全球IC设计大厂营收排名:高通稳居龙头
TrendForce旗下拓墣产业研究所最新研究统计,2016年全球前十大无晶圆IC设计业者营收中,高通(QCT)仍然稳居龙头宝座.而前三大业者高通.新博通(Broadcom)与联发科合计营收占前十名营 ...
- Extmail邮件过滤和杀毒
前面整合好了extmail,不过没有测试使用foxmail这种客户端去测试收发邮件功能,今天测试的时候发现了蛮多问题,大部分和/etc/authmysqlrc这个文件的配置和权限相关,都是小问题,折腾 ...
- USACO Training Section 1.2 挤牛奶Milking Cows
题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开 ...
- Servlet --启动时创建、配置url、ServlectContext、初始化参数、获取资源
servlet的版本的区别 2.5版本, Servlet的配置只支持在xml文件中的配置 3.0版本: Servlet的配置支持在xml文件中的配置, 也可以使用注解的方式, 默认使用注解 让服务器在 ...
- E - 梦幻岛宝珠 HYSBZ - 1190 变形01背包 难
E - 梦幻岛宝珠 HYSBZ - 1190 这个题目我觉得很难,看题解都看了很久. 首先可以得到一个大概的思路就是分组,每一个数都可以分成 a*2^b 所以把b相同的数都分成一个组. 在每一组内部 ...
- Linux内核驱动学习(十)Input子系统详解
文章目录 前言 框架 如何实现`input device` 设备驱动? 头文件 注册input_dev设备 上报按键值 dev->open()和dev->close() 其他事件类型,处理 ...
- C# 数据操作系列 - 3. ADO.NET 离线查询
0. 前言 在上一篇中,我故意留下了查询的示范没讲.虽然说可以通过以下代码获取一个DataReader: IDataReader reader = command.ExecuteReader(); 然 ...
- 谈谈DDD
从战略到战术,领域驱动设计(Domain Driven Design,DDD)给出了诸多关于软件架构.设计.建模与编码的方法和模式,以用于应对业务复杂度.然而,许多开发人员对于 DDD 的价值仍然心存 ...