地址: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. .NET开发过程中的全文索引使用技巧之Solr

        前言:相信许多人都听说过.net开发过程中基于Lucene.net实现的全文索引,而Solr是一个高性能,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查 ...

  2. Shell脚本编程与文件系统修复

    导读 Linux基金会发起了LFCS认证(Linux 基金会认证系统管理员)Linux Foundation Certified Sysadmin,这是一个全新的认证体系,旨在让世界各地的人能够参与到 ...

  3. fly

    购物车飞入效果 核心: 1,购物车与飞入圆点(或者图标)的定位关系 完整源码: <!doctype html> <html lang="zh"> <h ...

  4. pure

    Pure也是一款很出色的CSS框架,之前分享的Bootstrap是由Twitter出品的,而Pure是来自雅虎的.尽管从UI界面效果上来说,Pure没有Bootstrap那样精美,但Pure是纯CSS ...

  5. globalToLocal和localToGlobal

    官方API: groupOut全局坐标(50,50) gourpIn全局坐标(100,100),并嵌套在groupOut里 btn全局坐标(150,150),并嵌套在groupIn里 获取组件全局坐标 ...

  6. 【BZOJ3033】太鼓达人 暴力+欧拉回路

    [BZOJ3033]太鼓达人 Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员 ...

  7. SpringMvc三大组件详解

    SpringMvc框架结构图 处理器映射器:用户请求路径到Controller方法的映射 处理器适配器:根据handler(controlelr类)的开发方式(注解开发/其他开发) 方式的不同区寻找不 ...

  8. python--base64

    import base64import os # base64,参数为文件路径名def file_base64(filepath): if os.path.isfile(filepath): with ...

  9. 报错分析---->jsp自定义标签:Unable to load tag handler class

    Unable to load tag handler class 无法加载标签处理程序类 处理自定义标签的类中如下: 调用自定义标签的jsp中如下:

  10. NavagationBar 设置颜色和状态栏设置白色

    ios7以下的版本设置导航栏背景颜色可以使用 [[UINavigationBar appearance] setTintColor:[UIColor orangeColor]]; ios7以后: [[ ...