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. [leetcode-676-Implement Magic Dictionary]

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  2. “Hello World”团队第一周博客汇总

    时间:2017-10-13——2017-10-19 Scrum会议: 会议要求博客:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1 ...

  3. 总结get和post区别

    参考博文: 浅谈HTTP中Get与Post的区别 1. 数据传递方向: Get是向服务器发索取数据的一种请求,Post是向服务器提交数据的一种请求 (都是请求,并不是一个取一个发) Get:①用于获取 ...

  4. PagedDataSource数据绑定控件和AspNetPager分页控件结合使用列表分页

    1.引用AspNetPager.dll. 2.放置Repeater数据绑定控件. <asp:Repeater ID="Repeater1" runat="serve ...

  5. setsockopt 设置socket 详细用法

    1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BOOL bReuseaddr=TRUE;setsockopt(s,SOL_SOCKET ,SO ...

  6. str.substring(beginIndex,endIndex)-008

    // 将字符串str前n位放在后面,返回新的字符串 public String headToTail(String str,int n){ if(n==0){ System.out.println(s ...

  7. Redis Cluster实现原理

    一.Redis Cluster主要特性和设计     集群目标 1)高性能和线性扩展,最大可以支撑到1000个节点:Cluster架构中无Proxy层,Master与slave之间使用异步replic ...

  8. 【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流

    题目描述 FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain m ...

  9. 整合常用功能的JS小组件库-v1.0

    function Alex() { //给予video.js的页面滚动到视频元素范围内自动播放/出范围暂停播放-----01 this.video_autoplay = function (box) ...

  10. P1483 序列变换

    题目描述 给定一个由n个整数构成的序列,你需要对它进行如下操作: 操作1:输入格式“1 x y”,表示把所有a[kx](k为正整数,kx<=n)都加上y. 操作2:输入格式“2 j”,表示输出a ...