HDU 3948 不同回文子串个数
集训队论文中有求不同子串个数的做法,就是扫一遍height数组,过程中根据height数组进行去重。对于本题也是雷同的,只是每一次不是根据与排名在上一位的LCP去重,而是与上一次统计对答案有贡献的后缀进行比较去重。
几组数据
abacaba 7
abbacaa 7
baabcaa 5
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <cassert>
#include <sstream>
using namespace std; const int N=*; char s[N];
struct SuffixArray {
int wa[N], wb[N], cnt[N], wv[N];
int rk[N], height[N];
int sa[N];
bool cmp(int r[], int a, int b, int l) {
return r[a] == r[b] && r[a+l] == r[b+l];
}
void calcSA(char r[], int n, int m) {
int i, j, p, *x = wa, *y = wb;
for (i = ; i < m; ++i) cnt[i] = ;
for (i = ; i < n; ++i) cnt[x[i]=r[i]]++;
for (i = ; i < m; ++i) cnt[i] += cnt[i-];
for (i = n-; i >= ; --i) sa[--cnt[x[i]]] = i;
for (j = , p = ; p < n; j *= , m = p) {
for (p = , i = n - j; i < n; ++i) y[p++] = i;
for (i = ; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ; i < n; ++i) wv[i] = x[y[i]];
for (i = ; i < m; ++i) cnt[i] = ;
for (i = ; i < n; ++i) cnt[wv[i]]++;
for (i = ; i < m; ++i) cnt[i] += cnt[i-];
for (i = n-; i >= ; --i) sa[--cnt[wv[i]]] = y[i];
for (swap(x, y), p = , x[sa[]] = , i = ; i < n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p- : p++;
}
}
void calcHeight(char r[], int n) {
int i, j, k = ;
for (i = ; i <= n; ++i) rk[sa[i]] = i;
for (i = ; i < n; height[rk[i++]] = k)
for (k?k--:, j = sa[rk[i]-]; r[i+k] == r[j+k]; k++);
}
int lcp(int a,int b,int len) {
if (a==b) return len-a;
int ra=rk[a],rb=rk[b];
if (ra>rb) swap(ra,rb);
return queryST(ra+,rb);
}
int st[N][];
void initST(int n) {
for (int i=; i<=n; i++)
st[i][]=height[i];
for (int j=; (<<j)<=n; j++) {
int k=<<(j-);
for (int i=; i+k<=n; i++)
st[i][j]=min(st[i][j-],st[i+k][j-]);
}
}
int queryST(int a,int b) {
if (a>b) swap(a,b);
int dis=b-a+;
int k=log((double)dis)/log(2.0);
return min(st[a][k],st[b-(<<k)+][k]);
}
void solve(int cas) {
int n=strlen(s);
s[n]='#';
for (int i=;i<n;i++) {
s[n++i]=s[n--i];
}
int o=n;
n=*n+;
s[n]='\0';
calcSA(s,n+,);
calcHeight(s,n);
initST(n);
long long ret=;
int curLcp=;
for (int i=;i<=n;i++) { //odd
int pos=sa[i];
curLcp=min(curLcp,height[i]);
if (pos<o) {
int ops=n--pos;
int now=lcp(pos,ops,n);
ret+=max(,now-curLcp);
if (now>=curLcp)
curLcp=now;
}
}
curLcp=;
for (int i=;i<=n;i++) { //even
int pos=sa[i];
curLcp=min(curLcp,height[i]);
if (pos<o) {
int ops=n-pos;
int now=lcp(pos,ops,n);
ret+=max(,now-curLcp);
if (now>=curLcp)
curLcp=now;
}
}
printf("Case #%d: %I64d\n",cas,ret);
}
}suf; int main () {
//freopen("out.txt","r",stdin);
int T;
scanf("%d",&T);
int cas=;
while (T--) {
scanf("%s",s);
suf.solve(cas);
cas++;
}
return ;
}
HDU 3948 不同回文子串个数的更多相关文章
- HDU 1544 Palindromes(回文子串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...
- 马拉车算法——求回文子串个数zoj4110
zoj的测评姬好能卡时间.. 求回文子串的个数:只要把p[i]/2就行了: 如果s_new[i]是‘#’,算的是没有中心的偶回文串 反之是奇回文串 /* 给定两个字符串s,t 结论:s,t不相同的第一 ...
- zoj 2744 Palindromes(计算回文子串个数的优化策略)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2744 题目描述: A regular palindrome i ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- Best Reward HDU 3613(回文子串Manacher)
题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值. 分析:首先的明白这个最大价值有 ...
- URAL 2037 Richness of binary words (回文子串,找规律)
Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...
- CF 17E Palisection 求相交回文串个数
In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...
- [LeetCode] 647. 回文子串 ☆☆☆(最长子串、动态规划、中心扩展算法)
描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. 示例 1: 输入: "abc" ...
- 【HDU】4632 Palindrome subsequence(回文子串的个数)
思路:设dp[i][j] 为i到j内回文子串的个数.先枚举所有字符串区间.再依据容斥原理. 那么状态转移方程为 dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+ ...
随机推荐
- DOM基础(二)
在我之前写的DOM基础(一)的文章中提到过兼容性的问题,也就是在获取标签间文本信息的时候,早期的火狐版本是不支持innerText的,只支持textContent ,现在的火狐浏览器两者都支持.而IE ...
- 如何理解jQuery中的ajaxSubmit方法
版权声明:本文为博主原创文章,转载请标注:www.cnblogs.com/gdsblog 刚刚学习中,使用到了ajaxSubmit,犹豫以前没有接触㢧这个,所以刚开始是一脸懵逼状态,最后通过查找资料的 ...
- ThinkPhp框架:有条件的数据库查询、tp框架的其他知识
上一篇的随笔写的是基本操作,现在可以做一些高级操作,例如有条件的查询数据,有分页的条件查询数据 一.一个条件的查询数据 查询数据自然是先要显示出数据,然后根据条件进行查询数据 (1)显示出表的数据 这 ...
- 出现java.lang.reflect.UndeclaredThrowableException异常
解决方案:1.看导进来的项目是否有中文路径.2.看是否有get.set方法没写.3.和部署的环境有关.比如,是否写了构造函数.EJB需要.
- 基于Spring-WS的Restful API的集成测试
在很多Java企业级应用中,Spring占据了非常重要的位置,这就导致了基本上的技术选型都是围绕着Spring来, 比方说笔者最近的项目需要开发一个Restful的API接口,选型的时候就说,客户架构 ...
- js/jQuery中load()、onload()、ready()的区别
一.两大事件 load事件:指页面包含图片等文件在内的所有元素都加载完毕后执行的事件. ready事件:表示文档结构已加载完成(不包括图片等非文字媒体文件) 浏览器页面渲染的过程 - 寸寸君 - 博客 ...
- 使用mysql_Front链接mysql,出现警告access denied for user ''@'localhost'
刚刚安装好的mysql5.7,想来使用工具方便一下,却一直有报这个错误: 最后找出原因:我给root用户设置的密码神不知鬼不觉的消失了,无奈, 解决办法一: cmd->mysql -h loca ...
- python + selenium <一>
python 安装 python 下载地址: http://python.org/getit/ ez_setup.py 下载地址: https://pypi.python.org/packages/s ...
- JS调用APP
/* 500ms内,本机有应用程序能解析对应的协议并打开程序,调用该应用: 如果本机没有应用程序能解析该协议或者500ms内没有打开这个程序, 则执行setTimeout里面的function,跳转到 ...
- CSS3的新特性
CSS3中增加的新特性: (1)选择器的种类 (2)字体 font (3)text-overflow (4)文本渲染 text-decoration (5)多列布局 column-count (6)R ...