The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551
地址:http://acm.uestc.edu.cn/#/problem/show/1551
题目:
Hesty Str1ng
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
A chrysanthemum was painted on the second page, and we tried to use the magic power learned just now.
The beautiful flower was compacted to a colorful string SS representing by characters for some simplifying reasons.
As known, if we try to choose a substring AA of SS and concatenate it with an arbitrary non-empty string BB whose length is less than AA, we can get a new string TT.
The poetry told us the key to the eternity living secret is the number of the distinct palindrome strings made by the method above.
Two strings are considered different if and only if there are different characters in the same position of two strings.
Input
Only one line contains the string SS.
SS is composed by lowercase English characters, 1≤|S|≤1000001≤|S|≤100000.
Output
The key to the eternity living secret.
Sample input and output
| Sample Input | Sample Output | 
|---|---|
| abc | 3 | 
Hint
The palindrome strings can be made are "aba", "bcb", "abcba".
思路:
对于一个长度为n的子串,如果在其后连接一个长度为x(1<=x<n)的字符串形成新串T,并且T为回文串。
n=1时:形成的T的数量=0
n>1时:形成的T的数量=1+sum.(sum:子串含有的不同回文后缀的数量)
回顾下计算不同子串个数的后缀数组做法:
  
下面先给出一个结论:
sum[x]:表示后缀s[x....n-1]中s[k]==s[k+1]的个数
ans=∑(n-sa[i]-height[i]+sum[sa[i]+height[i]]) (字符串下标从0开始。)
并且当
height[i]==0时 ans-=1;
height[i]>=2&&ss[sa[i]+height[i]-1]==ss[sa[i]+height[i]]时 ans+=1;
!height[i]&&ss[sa[i]+height[i]]==ss[sa[i]+height[i]+1]时 ans-=1;
上面对应的三种情况分别是:
1. 此时有排序后的后缀abbb,ba.
2. 此时有排序后的后缀abb,abbba
3. 此时有排序后的后缀a,bba
具体证明过程我就不写了(PS:其实是我也不太会)
参考自校队另一位dalao的博文:http://blog.csdn.net/prolightsfxjh/article/details/66970491
具体见代码
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm> const int N = ;
int wa[N], wb[N], ws[N], wv[N];
int s[N],sa[N],rank[N], height[N];
char ss[N];
int sum[N];
bool cmp(int r[], int a, int b, int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
} void da(int r[], int sa[], int n, int m)
{
int i, j, p, *x = wa, *y = wb;
for (i = ; i < m; ++i) ws[i] = ;
for (i = ; i < n; ++i) ws[x[i]=r[i]]++;
for (i = ; i < m; ++i) ws[i] += ws[i-];
for (i = n-; i >= ; --i) sa[--ws[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) ws[i] = ;
for (i = ; i < n; ++i) ws[wv[i]]++;
for (i = ; i < m; ++i) ws[i] += ws[i-];
for (i = n-; i >= ; --i) sa[--ws[wv[i]]] = y[i];
for (std::swap(x, y), p = , x[sa[]] = , i = ; i < n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p- : p++;
}
} void calheight(int r[], int sa[], int n)
{
int i, j, k = ;
for (i = ; i <= n; ++i) rank[sa[i]] = i;
for (i = ; i < n; height[rank[i++]] = k)
for (k?k--:, j = sa[rank[i]-]; r[i+k] == r[j+k]; k++);
} int main()
{
int len;
long long ans=;
scanf("%s",ss);
len=strlen(ss);
for(int i=;i<len;i++)
s[i]=ss[i]-'a'+;
s[len]=;
da(s,sa,len+,);
calheight(s,sa,len);
for(int i=len-;i>=;i--)
if(ss[i]==ss[i+]) sum[i]=sum[i+]+;
else sum[i]=sum[i+];
for(int i=;i<=len;i++)
{
if(height[i]==)ans--;
ans+=sum[sa[i]+height[i]]+len-sa[i]-height[i];
if(height[i]>=&&ss[sa[i]+height[i]-]==ss[sa[i]+height[i]])
ans++;
if(!height[i]&&ss[sa[i]+height[i]]==ss[sa[i]+height[i]+])
ans--;
//printf("x==%d %lld\n",i,ans);
}
printf("%lld\n",ans);
return ;
}
The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551的更多相关文章
- The 15th UESTC Programming Contest Preliminary B - B0n0 Path cdoj1559
		地址:http://acm.uestc.edu.cn/#/problem/show/1559 题目: B0n0 Path Time Limit: 1500/500MS (Java/Others) ... 
- The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567
		地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ... 
- The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554
		地址:http://acm.uestc.edu.cn/#/problem/show/1554 题目: C0ins Time Limit: 3000/1000MS (Java/Others) M ... 
- The 15th UESTC Programming Contest Preliminary K - Kidd1ng Me? cdoj1565
		地址:http://acm.uestc.edu.cn/#/problem/show/1565 题目: Kidd1ng Me? Time Limit: 3000/1000MS (Java/Others) ... 
- The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557
		地址:http://acm.uestc.edu.cn/#/problem/show/1557 题目: Minimum C0st Time Limit: 3000/1000MS (Java/Others ... 
- The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564
		地址:http://acm.uestc.edu.cn/#/problem/show/1564 题目: G - GC?(X,Y) Time Limit: 3000/1000MS (Java/Others ... 
- The 15th UESTC Programming Contest Preliminary D - Destr0y City cdoj1558
		地址:http://acm.uestc.edu.cn/#/problem/show/1558 题目: D - Destr0y City Time Limit: 3000/1000MS (Java/Ot ... 
- 【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken
		题意:You are given an array A of N non-negative integers and an integer M. Find the number of pair(i,j ... 
- 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
		题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ... 
随机推荐
- VC++ LoadLibrary失败,错误127(找不到指定的程序)
			该原因一般是由于DLL或其依赖的DLL使用了高版本的API,而运行时找不到dll中的函数导致的错误. 使用依赖工具查看可能是这样的情况:第一个依赖dll文件小方块中有红色 正常情况下,应该是这样: 解 ... 
- hdu 4685(匹配+强连通分量)
			题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 思路:想了好久,终于想明白了,懒得写了,直接copy大牛的思路了,写的非常好! 做法是先求一次最 ... 
- ReactNative iOS源码解析
			http://awhisper.github.io/2016/06/24/ReactNative%E6%B5%81%E7%A8%8B%E6%BA%90%E7%A0%81%E5%88%86%E6%9E% ... 
- C语言位运算+实例讲解(转)
			按位或 按位与 按位异或 按位取反 左移右移 C语言位运算 有6种: &, | , ^(亦或), >(右移). 注意:参与位运算的元素必须是int型或者char型,以补码形式出现. 按位 ... 
- SQL ALTER TABLE 命令
			SQL ALTER TABLE 命令 SQL ALTER TABLE 命令用于添加.删除或者更改现有数据表中的列. 你还可以用 ALTER TABLE 命令来添加或者删除现有数据表上的约束. 语法: ... 
- gulp 报错'wacth' errord
			gulp.wacth(...).watch is not a function 如图: 检查了gulpfile.js文件中的wacth事件:发现这样的写法出错: gulp.task('watch', ... 
- ios 如何对UITableView中的内容进行排序
			- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSI ... 
- JDK的图文安装教程
			JDK的安装 什么是JDK? JDK就是Java开发工具包,即Java Development Kit.就是做Java开发所需要的最基本的工具.包括Java编译器(把人使用的Java语言变成JVM能运 ... 
- Zabbix高可用
			上一篇:Zabbix数据库表结构 安装两台Zabbix-server 两台均安装MySQL数据库 数据库做双主互相同步 keepalive做vip偏移 to_master.sh脚本 两边都要安装ssh ... 
- HDU2842—Chinese Rings
			题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目意思:一把一个n连环的前n个拿下来,一个个n连环,要把第k个拿下来,需要把前n-2个拿下来, ... 
