2780: Poi2006 Palindromes

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 15  Solved: 5
[Submit][Status][Web Board]

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. 
给出n个回文串s1, s2, …, sn 
求如下二元组(i, j)的个数 
si + sj 仍然是回文串 
规模 
输入串总长不超过2M bytes 

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
//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的更多相关文章

  1. BZOJ1524: [POI2006]Pal

    1524: [POI2006]Pal Time Limit: 5 Sec  Memory Limit: 357 MBSubmit: 308  Solved: 101[Submit][Status] D ...

  2. 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 ...

  3. hdu 1318 Palindromes

    Palindromes Time Limit:3000MS     Memory Limit:0KB     64bit                                         ...

  4. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

  5. Dual Palindromes

    Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...

  6. ytu 1940:Palindromes _easy version(水题)

    Palindromes _easy version Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 47  Solved: 27[Submit][Statu ...

  7. 回文串+回溯法 URAL 1635 Mnemonics and Palindromes

    题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...

  8. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  9. 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

    P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 ...

随机推荐

  1. 判断iPhone/android手机

    JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接. var browser = {versions: function () {var u = navigator ...

  2. JAVA和C++区别

    1.指针 JAVA语言让编程者无法找到指针来直接访问内存无指针,并且增添了自动的内存管理功能,从而有效地防止了c/c++语言中指针操作失误,如野指针所造成的系统崩溃.但也不是说JAVA没有指针,虚拟机 ...

  3. 安装tomcat过程中出现问题小结

    报错信息如下:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these ...

  4. Android复习--广播

    广播有两种方式,一种静态广播,一种动态广播. 静态广播-->静态广播接收器在配置文件里面注册. 动态广播-->而动态广播接收器在代码里面注册. 广播的发送: Context.sendBro ...

  5. c# 动态产生控件 注册动态控件事件

    用CheckEdit演示 其他控件类推 CheckEdit AllSele = new CheckEdit(); AllSele.Location = new System.Drawing.Point ...

  6. git版本控制(一)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. ecshop二次开发添加快递

    以天天快递为例:步骤1.打开includes\modules\shipping文件夹,把sto_express.php复制多一份,重名为tt_express.php:步骤2.打开tt_express. ...

  8. SpringMVC文件上传报错org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

    错误信息: java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to or ...

  9. js - object的属性操作

    视频学习地址: http://www.imooc.com/video/6002 原文PPT下载地址: http://img.mukewang.com/down/54c5ec1a000141f10000 ...

  10. Quick Cocos2dx MVC初步

    今天看到了自己之前两年前写的一个地图编辑器, 写了不到一半就放弃了, 但是还是github上的小伙伴fork了, 真的感觉对不起那位伙计, 同时也鄙视一下一直以来懒得要死的自己, 希望这个demo不要 ...