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次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
随机推荐
- Linux命令之乐--test
官方文档: help test File operators: -a FILE True if file exists. -b FILE True if file is block special. ...
- DOM API querySelector与querySelectorAll的用法
DOM API querySelector与querySelectorAll的用法: http://www.qttc.net/201309371.html querySelectorAll与quer ...
- vue 二级列表折叠面板
请求出来的数据是二级列表,需要点击一级列表展开相应的二级列表 data(){ return { info: [ {name:'一级菜单1',lists:[{price:1},{price:2}]}, ...
- Block Formatting Contexts (块级格式化上下文) 使用参考
转自:http://kayosite.com/block-formatting-contexts-in-detail.html 在上文<详说清除浮动>中,Kayo 较为详细地介绍了 BFC ...
- CentOS7部署Haproxy 1.7.2
一.环境准备 1.操作系统 CentOS-7-x86_64-1611 2.Haproxy版本1.7.2 3.Haproxy服务器IP 192.168.186.131.web1服务器安装并启动Nginx ...
- Net 常用资源
opensource: http://www.dotnetfoundation.org/projects https://github.com/dotnet/corefx Enterprise Lib ...
- Ubuntu 下 mysql 卸载后重安装时遇到的问题
卸载mysql报错解决方法1 dpkg: error processing mysql-server (--configure): dependency problems - leaving unco ...
- 剑指Offer——删除链表中重复的结点
题目描述: 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理 ...
- SonarQube+jenkins+maven平台搭建
SonarQube代码质量管理 1.配置jdk和maven jenkins - 系统管理 - 全局工具配置 jdk安装: # which java# /usr/local/java/jdk1.8.0. ...
- linux 这是定时任务
1. 编写shell脚本:vim test.sh #/bin/bash echo "hello world" 2.crontab任务配置基本格式: * * * * * comm ...