地址: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的更多相关文章

  1. 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)    ...

  2. The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567

    地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...

  3. 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 ...

  4. 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) ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem

    题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...

随机推荐

  1. 【转】C++ Incorrect Memory Usage and Corrupted Memory(模拟C++程序内存使用崩溃问题)

    http://www.bogotobogo.com/cplusplus/CppCrashDebuggingMemoryLeak.php Incorrect Memory Usage and Corru ...

  2. LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  3. warning: Now you can provide attr "wx:key" for a "wx:for" to improve performance.

    小程序开发过程中在写for循环的时候会出现如下报错 warning: Now you can provide attr "wx:key" for a "wx:for&qu ...

  4. CSS3制作旋转导航

    慕课网学习CSS3时,遇到个习题,觉得有必要总结学习下:CSS3制作旋转导航 慕课网习题地址:http://www.imooc.com/code/1883 示例及源码地址:http://codepen ...

  5. Windows 2012 Server评估版本安装问题处理

    由于工作需要,在微软官方网站下载了一个Windows2012评估版本,地址:http://technet.microsoft.com/zh-cn/evalcenter/hh670538.aspx 在通 ...

  6. 使用RODBC连接MySQL数据库(R-3.4.3)

    1.安装RODBC包 install.packages("RODBC") 2.配置ODBC 首先查看是否有mysqlODBC5.1Driver  如果没有mysqlODBC5.1D ...

  7. echart使用总结

    以下参数都是写在option配置对象内,没有提及的配置参数欢迎查阅读echart参考手册. 一. 修改主标题和副标题 title : { text: '未来一周气温变化',//写入主标题 subtex ...

  8. EntityFramework.DynamicFilters 实现软删除和租户过滤

    EntityFramework.DynamicFilters 实现软删除和租户过滤

  9. Vim 字符集问题

     使用CentOS中的Vim 文本编辑器出现中文乱码的问题. 凡是字符乱码的问题,都是字符集不匹配的问题引起的.这里的字符集不匹配只的是文件的编码和解码方式不匹配,同时可能涉及到不只一次的解码过程. ...

  10. 牛B三人组-快速排序-堆排序-归并排序

    快速排序 随便取个数,作为标志值,这里就默认为索引位置为0的值 记录左索引和右索引,从右往左找比标志值小的,小值和左索引值交换,右索引变化,然后从左往右找比标志值大的,大值和右索引值交换,左索引变化 ...