The Number of Palindromes

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
http://acm.hdu.edu.cn/showproblem.php?pid=3948

Problem Description
Now, you are given a string S. We want to know how many
distinct substring of S which is palindrome.
 
Input
The first line of the input contains a single integer
T(T<=20), which indicates number of test cases.
Each test case consists of
a string S, whose length is less than 100000 and only contains lowercase
letters.
 
Output
For every test case, you should output "Case #k:" first
in a single line, where k indicates the case number and starts at 1. Then output
the number of distinct substring of S which is palindrome.
 
Sample Input
3
aaaa
abab
abcd
 
Sample Output
Case #1: 4
Case #2: 4
Case #3: 4
 
Source
 
Recommend
xubiao   |   We have carefully selected several similar
problems for you:  3946 3947 3949 3945 3944 
 
题意:统计不同回文串的个数
首先对原串跑一遍manacher算法,处理出最长回文半径
然后枚举每一个位置
从最长的回文串开始一点一点儿往里索,hash判断这个字符串是否出现过
如果出现过,那么比它更短的肯定也出现过,结束本位置的查找,到下一个位置
 
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define D1 28
#define D2 38
using namespace std;
const int MOD1=;
const int MOD2=;
const int MOD3=1e9+;
const int MOD4=1e9+;
char s[],a[];
int p[],len,l,ans;
int f1[],g1[];
int f2[],g2[];
int front1[MOD1+],front2[MOD2+];
int to1[],to2[];
int nxt1[],nxt2[];
int tot1,tot2;
void manacher()
{
memset(p,,sizeof(p));
int pos=,id=,x;
for(int i=;i<=len;i++)
{
if(i<pos) x=min(p[*id-i],pos-i);
else x=;
while(s[i+x]==s[i-x]) x++;
if(i+x>pos) { pos=i+x; id=i; }
p[i]=x;
}
}
int get_hash1(int l,int r)
{
int a=f1[r];
int b=1ll*f1[l-]*g1[r-l+]%MOD3;
return (b-a+MOD3)%MOD3;
}
int get_hash2(int l,int r)
{
int a=f2[r];
int b=1ll*f2[l-]*g2[r-l+]%MOD4;
return (b-a+MOD4)%MOD4;
}
void add1(int u,int v)
{
to1[++tot1]=v; nxt1[tot1]=front1[u]; front1[u]=tot1;
}
void add2(int u,int v)
{
to2[++tot2]=v; nxt2[tot2]=front2[u]; front2[u]=tot2;
}
bool find1(int u,int v)
{
for(int i=front1[u];i;i=nxt1[i])
if(to1[i]==v) return true;
return false;
}
bool find2(int u,int v)
{
for(int i=front2[u];i;i=nxt2[i])
if(to2[i]==v) return true;
return false;
}
void cal(int pos,int len)
{
for(int i=len;i>=;i--)
{
int hash1=get_hash1(pos-i+,pos+i-);
int hash2=get_hash2(pos-i+,pos+i-);
int t1=hash1%MOD1,t2=hash2%MOD2;
if(!(find1(t1,hash1)&&find2(t2,hash2)))
{
ans++;
add1(t1,hash1);
add2(t2,hash2);
}
else return;
}
}
void pre()
{
ans=tot1=tot2=;
memset(front1,,sizeof(front1));
memset(front2,,sizeof(front2));
memset(f1,,sizeof(f1));
memset(f2,,sizeof(f2));
g1[]=;
for(int i=;i<=len;i++) g1[i]=1ll*g1[i-]*D1%MOD3;
f1[]=s[];
for(int i=;i<=len;i++) f1[i]=(1LL*f1[i-]*D1+s[i]-'')%MOD3;
g2[]=;
for(int i=;i<=len;i++) g2[i]=1ll*g2[i-]*D2%MOD4;
f2[]=s[];
for(int i=;i<=len;i++) f2[i]=(1LL*f2[i-]*D2+s[i]-'')%MOD4;
}
int main()
{
int t;
scanf("%d",&t);
for(int tt=;tt<=t;tt++)
{
scanf("%s",a);
s[len=]='!';
l=strlen(a);
for(int i=;i<l;i++)
{
s[++len]='#';
s[++len]=a[i];
}
s[++len]='#';
s[len+]='&';
pre();
manacher();
for(int i=;i<=len;i++)
cal(i,p[i]);
printf("Case #%d: %d\n",tt,ans/);
}
}

hdu 3948 The Number of Palindromes的更多相关文章

  1. HDU 3948 The Number of Palindromes(Manacher+后缀数组)

    题意 求一个字符串中本质不同的回文子串的个数. $ 1\leq |string| \leq 100000$ 思路 好像是回文自动机的裸题,但是可以用 \(\text{Manacher}\) (马拉车) ...

  2. 【HDOJ】3948 The Number of Palindromes

    后缀数组求不重复回文子串数目.注意dp数组. /* 3948 */ #include <iostream> #include <sstream> #include <st ...

  3. hdu 3948 后缀数组

    The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  4. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

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

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

  6. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  7. 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)

    The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...

  8. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  9. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

随机推荐

  1. 进程间通信:命名管道FIFO(2)

    一.命名管道 如果我们想在不相关的进程之间交换数据,可以用FIFO文件来完成这项工作,它通常也被称为命名管道.命名管道是一种特殊类型的文件,它在文件系统中以文件名的形式存在,但是它的行为却和我们已经见 ...

  2. iOS-创建UIScrollerView(封装UIScrollerView)

    创建继承于UIView的类WJImageScrollView,代码实现如下: WJImageScrollView.h #import <UIKit/UIKit.h> /**点击图片bloc ...

  3. TCP系列03—连接管理—2、TCP连接的同时打开和同时关闭

    在前面的内容中我们介绍了TCP连接管理中最常见的三次握手方式和四次挥手的方式.但是有可能A和B两端同时执行主动打开并连接对方或者同时执行主动关闭连接(尽管发生这种情况的可能性比较低低),这个时候的流程 ...

  4. centOS 6.5命令方式配置静态IP

    想自己做个centOS玩一下,然后通过FTP访问操作,首先查看是否开启了SSH,命令如下: rpm -qa | grep ssh 这个时候看到的是centOS的ssh已经打开!要是通过FTP工具访问还 ...

  5. 如何彻底解决adb 5037端口被占用

    在进行安卓开发的时候是不是经常碰到adb端口被占用的情况? 解决这个问题的方法很简单,只需要设置一个系统环境变量就可以搞定. 设置方法: 增加系统环境变量变量名称:ADNROID_ADB_SERVER ...

  6. JDK版本Java SE、Java EE、Java ME的区别

    想在win7 X64上搭建JAVA开发环境来着(只是尝试下),打开JAVA 官网下载JDK,发现好多版本懵了,百度了下找到这些版本的区别,故有了下文 1.JAVA SE Java2平台标准版(Java ...

  7. CSS设计指南之一 HTML标记与文档结构

    HTML标记与文档结构 之所以从HTML讲起,是因为CSS的用途就是为HTML标记添加样式. 1.1 HTML标记基础 对于每个包含内容的元素,根据它所包含的内容是不是文本,有两种不同的方式给它们加标 ...

  8. HUAS 1483 mex(离线+线段树)

    实在是太弱了.... 考虑离线,从mex[l,r]向mex[l,r+1]转移,显然是没啥东西可以记录的... 从mex[l,r]向mex[l+1,r]转移,记x=mex[l,r],如果[l+1,r]不 ...

  9. P1955 [NOI2015]程序自动分析

    题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变 ...

  10. 【转】Unity+单例模式的依赖注入

    http://www.cnblogs.com/floyd/archive/2009/06/17/1505117.html