Poi2006 Palindromes
2780: Poi2006 Palindromes
Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 15 Solved: 5
[Submit][Status][Web Board]
Description
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.
给出n个回文串s1, s2, …, sn
求如下二元组(i, j)的个数
si + sj 仍然是回文串
规模
输入串总长不超过2M bytes
Input
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
3
1 a
2 ab
2 ba
Sample Output
5
//The 5 palindromes are:aa aba aba abba baab
HINT
Source
题解:
这个怎么写呢,一脸懵比。。。。。。。
(龙老师说:)我们先把所有串插入一个trie树,然后统计每个串的hash,然后再枚举每个串,沿trie树向下走,枚举每一个前缀,判断他们俩连起来的字符串正着和 反着是否一样,
直接hash判断即可。
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define maxn 2000000
#define base 199
using namespace std;
int n,k;
ll hashmod,ans,tot;
ll h[maxn],ha[maxn];
int son[maxn][],sum[maxn],f[maxn],len[maxn];
string s[maxn],ch;
int main()
{
h[]=;
for (int i=; i<=maxn+; i++) h[i]=h[i-]*base;
cin>>n;
ans=-n;
for (int i=; i<=n; i++)
{
cin>>len[i];
cin>>ch;
s[i]=' '+ch;
int p=; hashmod=;
for (int j=; j<=len[i]; j++)
{
int x=s[i][j]-'a'+;
if (!son[p][x]) son[p][x]=++tot;
p=son[p][x];
hashmod=hashmod*base+x;
}
//cout<<hashmod<<endl;
ha[i]=hashmod;
f[p]=i; sum[p]++;
}
//cout<<tot;
for (int i=; i<=n; i++)
{
int p=;
for (int j=; j<=len[i]; j++)
{
int x=s[i][j]-'a'+;
p=son[p][x];
// cout<<p<<endl;
//if (sum[p] && ha[f[p]]*h[len[i]]+ha[i]==ha[i]*h[j+1]+ha[f[p]]) ans+=(ll)sum[p]*2;
if(son[p]&&ha[f[p]]*h[len[i]]+ha[i]==ha[i]*h[j]+ha[f[p]])ans+=(ll)sum[p]*;
}
}
printf("%lld\n",ans); return ;
}
Poi2006 Palindromes的更多相关文章
- BZOJ1524: [POI2006]Pal
1524: [POI2006]Pal Time Limit: 5 Sec Memory Limit: 357 MBSubmit: 308 Solved: 101[Submit][Status] D ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- hdu 1318 Palindromes
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Dual Palindromes
Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...
- ytu 1940:Palindromes _easy version(水题)
Palindromes _easy version Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 47 Solved: 27[Submit][Statu ...
- 回文串+回溯法 URAL 1635 Mnemonics and Palindromes
题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes
P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
随机推荐
- MySQL 5.7 for Windows 解压缩版配置安装
从MYSQL5.7.6开始,安装MYSQL提示“请键入 NET HELPMSG 3534 以获得更多的帮助”的解决办法 今天安装MySQL提示如下错误: ----------------------- ...
- jQuery两种扩展插件的方式
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 休眠唤醒不断开wifi.
文件: /home/mxy/code/v1/frameworks/base/services/java/com/auto/opandora/Opandora.java 屏蔽掉: 957 SetWifi ...
- DIV 浮动存不占空间
DIV 浮动存不占空间比如<div style="width:80px; border:1px solid #333"><div style="floa ...
- Server对象
Server是服务器对象,定义了一个与Web服务器相关的类,用于访问服务器上的资源. 属性 MachineName 获取服务器的计算机名. 返回本地计算机的名称 ScriptTimeout ...
- myeclipse连接hadoop集群编程及问题解决
原以为搭建一个本地编程测试hadoop程序的环境很简单,没想到还是做得焦头烂额,在此分享步骤和遇到的问题,希望大家顺利. 一.要实现连接hadoop集群并能够编码的目的需要做如下准备: 1.远程had ...
- 利用hibernate实现oracle(每张表的ID)自动增长
设置ID的增长策略是sequence,同时指定sequence的名字,最好每个表建一个sequence 例如创建一个序列:CREATE SEQUENCE DEPARTMENT_ID_SEQ MINVA ...
- HDU 3038 How Many Answers Are Wrong 很有意思的一道并查集问题
题目大意:TT 和 FF玩游戏(名字就值五毛),有一个数列,数列有N个元素,现在给出一系列个区间和该区间内各个元素的和,如果后出现的一行数据和前面一出现的数据有矛盾,则记录下来.求有矛盾数据的数量. ...
- Python 之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
一.Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负债.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...
- ILayer
ILayer http://127.0.0.1:47873/help/1-4452/ms.help?method=page&id=ESRICARTO-7E8C676F-000BCF&p ...