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
The 5 palindromes are:
a a a ba ab a ab ba ba ab


【题目大意】
给定n个字符串,共有n*n种组合的方式,那么产生了一个问题,总共有多少个是回文串,字符串的总长度不超过200w


【题目分析】
    好坑啊,总长不超过200w,是200w个长度为1还是一个长度为200w,所以只能用一个数组存所有的字符串了。
    然后再来考虑这道题目,如何减少计算的数量,假设有两个串cba和abcded,我们把第二个串插入到trie树里,然后就可以拿第一个字符串反向的去比较了。我们可以把第一个串反过来,然后比较,发现可以匹配前三个(trie树中进行转移十分方便)就可以剩下ded三个字符,而他们显然是一个回文串,所以答案+1.所以我们只需要标记出来trie树种所有节点的儿子中是回文串的数量,然后去反向匹配到终点,最后加入答案即可,如果到了一定位数失陪了,而且发现当前结点是一个字符串结束的位置,而且剩余串是回文串,这时候答案也需要增加,举个例子,我们先插入abc 然后遇到了一个串dedcba,我们反过来匹配的时候发现到了c的时候无法匹配,剩下的是一个回文串,而且恰好当前位置有一个字符串结束,答案+1。然后就可以了。至于一些细枝末节的东西就自己慢慢改好了。别人的代码好长,许多人都是用KMP的,用manacher明显会好些很多啊。


【代码】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n,trie[2000010][27],tot=1,root=0,st[2000010],l[2000010],cnt[2000010],r[4000010],en=0;
char s[2000010];
int end[2000010];
char a[4000010];
int li[4000010],nxt[4000010];
long long ans=0;
inline void insert()
{a[0]='$';a[1]='#';tot--;for (int i=1;i<=tot;++i){a[i*2]=s[i];a[i*2|1]='#';}a[tot*2+2]='@';int id=0,mx=0;for (int i=1;i<=tot*2+1;++i){if (i<mx) r[i]=min(r[2*id-i],mx-i); else r[i]=0;while (a[i-r[i]]==a[i+r[i]]) r[i]++;if (i+r[i]>mx) mx=i+r[i],id=i;}for (int i=1;i<=n;++i){int now=root;for (int j=st[i];j<=st[i]+l[i]-1;++j){if (!trie[now][s[j]-'a']) trie[now][s[j]-'a']=++en;now=trie[now][s[j]-'a'];int mid=((j+1)*2-1+(st[i]+l[i]-1)*2+1)/2;if (r[mid]>mid-(j+1)*2+1){cnt[now]++;}}cnt[now]--;end[now]++;}
}
inline void find()
{for (int i=1;i<=n;++i){int now=root;for (int j=st[i]+l[i]-1;j>=st[i];--j){if (!trie[now][s[j]-'a']) {now=-1;break;}now=trie[now][s[j]-'a'];if (end[now]){int mid=(st[i]*2-1+(j-1)*2+1)/2;if (r[mid]>mid-st[i]*2+1)ans+=end[now];}}if (now!=-1) ans+=(long long)cnt[now];}
}
int main()
{while (scanf("%d",&n)!=EOF){en=0,tot=1;ans=0;memset(trie,0,sizeof trie);memset(cnt,0,sizeof cnt);memset(end,0,sizeof end);for (int i=1;i<=n;++i){scanf("%d",&l[i]);scanf("%s",s+tot);st[i]=tot;tot+=l[i];}insert();find();cout<<ans<<endl;}
}

POJ 3376 Finding Palindromes相关推荐

  1. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  2. POJ 2049 Finding Nemo BFS

    题目大意:给你一个奇奇怪怪的迷宫, 这个迷宫包括墙和门.再给你一个起始坐标, 问你从迷宫内到外面至少要穿越多少的门. 题目分析: 穿越多少门等同于路过了多少个格子. 为此我们可以将整个地图中的格子,门 ...

  3. POJ3376 Finding Palindromes

    目录 知识点:Trie树(字典树).拓展kmp(manacher?).时间空间复杂度 题目 输入 输出 样例 输入 输出 提示 题意 思路 代码 知识点:Trie树(字典树).拓展kmp(manach ...

  4. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  5. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  6. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  7. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  8. 老鱼的-kuangbin专题题解

    kuangbin专题问题一览 专题一 简单搜索 POJ 1321 棋盘问题 POJ 2251 Dungeon Master POJ 3278 Catch That Cow POJ 3279 Flipt ...

  9. sqlserver查询当月的每一天_SQL生成一年每一天的时间列表的几种方法

    工作好几年了,一直没有写博客,准备捡起来... 以下脚本适用环境:SQL SERVER (starting with 2012) 1.构建序列: /*1-1:利用交叉连接,推荐下列这种写法*/ SEL ...

  10. [kuangbin]各种各样的题单

    [kuangbin]各种各样的题单 专题1 简单搜索 POJ 1321 POJ 2251 POJ 3278 POJ 3279 POJ 1426 POJ 3126 POJ 3087 POJ 3414 F ...

最新文章

  1. easy C语言,C语言easy..doc
  2. 学习C语言你是否思考过表达式11111*11111的值是多少?把5个1换成6个1呢?9个1呢?...
  3. 对讲机服务器信号不好怎么办呢,手机信号不好是什么原因?该怎么办?教你一招立马解决(绝对有效)...
  4. ORM中的Model与DDD中的DomainModel
  5. unity三维地图的经纬度如何在二维地图上表示_接入C++版本recastnavigation寻路库到Unity/服务端中...
  6. Microsoft Visual Studio.NET 2003 (VS2003) 简体中文企业级结构设计版+MSDN中文版
  7. 通过shell和redis来实现集群业务中日志的实时收集分析
  8. 1个超强的软件工具箱!100+个电脑必备工具,随意使用!盘姬
  9. 使用宏破解EXCEL工作表保护密码的方法
  10. Fences隐藏桌面图标快捷方式箭头
  11. 面试心得1之STAR法则
  12. Trying to access array offset on value of type int
  13. 不讲废话,全程硬核,处理结构化数据的终极解决方案
  14. win7计算机序列号在哪里,win7系统如何查看主板序列号?win7系统查看主板序列号的详细步骤图文教程...
  15. 阿里云服务器安装postgresql
  16. 基于RSA和AES混合加密实现的加解密小工具
  17. windows以兼容模式运行程序
  18. 使用pandas模块实现数据的标准化
  19. POJ 1700 经典过河问题(贪心)
  20. mcrypt php 加密解密,mcrypt启用 加密以及解密过程详细解析

热门文章

  1. [密码学复习]Cryptography
  2. 树莓派怎么切换输入法_树莓派 Raspberry Pi 设置显示中文方法安装输入法
  3. 需要管理员权限才能删除文件夹
  4. 图片格式WEBP全面解析
  5. 网课答案公众号制作教程
  6. 1068. Find More Coins (30)搜索题
  7. 快捷连接 残差_残差网络解决了什么问题
  8. eSpeak: Linux文本转语音工具
  9. 教师计算机西沃培训心得,希沃电子白板学习心得体会
  10. 深度学习中Dropout原理解析