The Number of Palindromes

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2465    Accepted Submission(s): 841

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
/*
hdu 3948 后缀数组 problem:
给你一个字符串,问其中不同回文子串的个数 solve:
通过长度的奇偶性能够找出所有的回文子串(类似求最长回文子串),但是其中有很多重复的一直不知道怎么处理
有的较短的回文子串包含在一个长的里面,有的是两个回文有重复的部分。
最开始尝试的是通过枚举字符串所有的位置,类似于manacher求出所有位置的回文长度.但是发现完全没法解决重复的问题 参考别人的方法:
首先height数组可以知道两个回文子串的关系,所以枚举height.
通过ta记录当前位置与上一个回文串的公共前缀,当遇到下个回文串就能知道两个回文串的公共部分.即需要减去的重复部分
ta = min(ta,height[i]);
而且枚举height不知道当前回文串是否处理过,所以用vis记录一下 hhh-2016-08-11 13:51:03
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
typedef unsigned int ul;
const int INF = 0x3f3f3f3f;
const int maxn = 200000+10;
const int mod = 1e9+7; int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
return r[a]==r[b] &&r[l+a] == r[l+b];
} void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
n++;
int p,*x=t1,*y=t2;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[i] = str[i]]++;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
for(int j = 1; j <= n; j <<= 1)
{
p = 0;
for(int i = n-j; i < n; i++) y[p++] = i;
for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[y[i]]]++ ;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x,y);
p = 1;
x[sa[0]] = 0;
for(int i = 1; i < n; i++)
x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++;
if(p >= n) break;
m = p;
}
int k = 0;
n--;
for(int i = 0; i <= n; i++)
Rank[sa[i]] = i;
for(int i = 0; i < n; i++)
{
if(k) k--;
int j = sa[Rank[i]-1];
while(str[i+k] == str[j+k]) k++;
height[Rank[i]] = k;
} } int mm[maxn];
int dp[20][maxn]; int Rank[maxn],height[maxn];
int sa[maxn];
char str[maxn];
int r[maxn]; void ini_RMQ(int n)
{
mm[0] = -1;
for(int i = 1; i <= n; i++)
mm[i] = (((i & (i-1)) == 0) ? mm[i-1]+1:mm[i-1]); for(int i =1; i <= n; i++)
dp[0][i] = height[i];
for(int i = 1; i <= mm[n]; i++)
{
for(int j = 1; j+(1<<i)-1 <= n; j++)
{
int a = dp[i-1][j];
int b = dp[i-1][j+(1<<(i-1))];
dp[i][j] = min(a,b);
}
}
} int askRMQ(int a,int b)
{
int t = mm[b-a+1];
b -= (1<<t)-1;
return min(dp[t][a],dp[t][b]);
} int fin(int a,int b)
{
a = Rank[a],b = Rank[b];
if(a > b) swap(a,b);
return askRMQ(a+1,b);
}
int vis[maxn]; int main()
{
int T;
int cas = 1;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
int len = strlen(str);
for(int i= 0; i < len; i++)
r[i] = str[i];
r[len] = 1;
for(int i = 0; i < len; i++)
r[len+1+i] = str[len-1-i];
r[2*len+1] = 0;
get_sa(r,sa,Rank,height,len*2+1,128);
ini_RMQ(2*len+1);
int ans = 0;
int n = len+len+1;
int ta=0;
memset(vis,0,sizeof(vis));
for(int i = 2; i <= n; i++)
{
ta = min(height[i],ta);
if(!sa[i])
continue;
if(vis[n-sa[i]])
{
int t = fin(sa[i],n-sa[i]);
if(t > ta)
{
ans += t-ta;
ta = t;
}
}
else
vis[sa[i]] = 1;
}
ta= 0 ;
memset(vis,0,sizeof(vis));
for(int i = 2; i <= n; i++)
{
ta= min(ta,height[i]);
if(vis[n-sa[i]-1])
{
int t = fin(sa[i],n-sa[i]-1);
if(t > ta)
{
ans += t-ta;
ta = t;
}
}
else
vis[sa[i]] = 1;
}
printf("Case #%d: ",cas++);
printf("%d\n",ans);
}
}

  

hdu 3948 后缀数组的更多相关文章

  1. HDU - 3948 后缀数组+Manacher

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...

  2. hdu 3948(后缀数组+RMQ)

    题意:求一个串中有多少不同的回文串. 分析:这一题的关键是如何去重,我表示我现在还没理解为什么这样去重,先放这里过两天再看!! //不同回文子串数目 #include <iostream> ...

  3. HDU 5769 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...

  4. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

  5. hdu 3518(后缀数组)

    题意:容易理解... 分析:这是我做的后缀数组第一题,做这个题只需要知道后缀数组中height数组代表的是什么就差不多会做了,height[i]表示排名第i的后缀与排名第i-1的后缀的最长公共前缀,然 ...

  6. hdu 5442 (后缀数组)

    稍微学习了下第一次用后缀数组- - , 强行凑出答案 , 感觉现在最大的问题是很多算法都不知道 ,导致有的题一点头绪都没有(就像本题).  /*推荐 <后缀数组——处理字符串的有力工具>— ...

  7. HDU 5558 后缀数组

    思路: 这是一个错误的思路, 因为数据水才过= = 首先求出来后缀数组 把rank插到set里 每回差i两边离i近的rank值,更新 如果LCP相同,暴力左(右)继续更新sa的最小值 //By Sir ...

  8. HDU 4691 后缀数组+RMQ

    思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...

  9. K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)

    大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...

随机推荐

  1. django搭建web (二) urls.py

    URL模式: 在app下的urls.py中 urlpatterns=[ url(正则表达式,view函数,参数,别名,前缀)] urlpatterns=[ url(r'^hello/$',hello. ...

  2. Twisted UDP编程技术

    实战演练1:普通UDP UDP是一种无连接对等通信协议,没有服务器和客户端概念,通信的任何一方均可通过通信原语直接和其他方通信 1.相对于TCP,UDP编程只需定义DatagramProtocol子类 ...

  3. python 操作SQLAlchemy

    SQLAlchemy python链接 pymysql mysql+pymysql://<username>:<password>@<host>/<dbnam ...

  4. 12-TypeScript总结

    从前面的文章大家可以看出,TypeScript具有先天的优势,建议前端开发人员使用TypeScript进行开发,提升自己的面向对象开发思想与能力.: 1.微软开源的客户端脚本语言,是JavaScrip ...

  5. php析构方法

    析构方法说明: 1. 析构方法会自动调用 2. 析构方法主要用于销毁资源(比如释放数据库的链接,图片资源...销毁某个对象..); 析构函数会在到对象的所有的引用都被删除或者当对象被显示销毁时执行. ...

  6. 码农、黑客和2B程序员之间的区别

    码农: 黑客: 2B程序员: 求2的32次方: 码农: System.out.println(Math.pow(2, 32)); 黑客: System.out.println(1L<<32 ...

  7. C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域

    需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小 ...

  8. 泛型的 typeof

    static void Main(string[] args) { TestTypeOf<string>(); Console.ReadKey(); } static void TestT ...

  9. Spring知识点回顾(03)Bean的 Scope

    sigleton prototype request session globalsession stepscope

  10. 基于gin框架和jwt-go中间件实现小程序用户登陆和token验证

    本文核心内容是利用jwt-go中间件来开发golang webapi用户登陆模块的token下发和验证,小程序登陆功能只是一个切入点,这套逻辑同样适用于其他客户端的登陆处理. 小程序登陆逻辑 小程序的 ...