地址: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. 注解-->Spring配置

    有必要对JDK 5.0新增的注解(Annotation)技术进行简单的学习,因为Spring 支持@AspectJ,而@AspectJ本身就是基于JDK 5.0的注解技术.所以学习JDK 5.0的注解 ...

  2. 运动目标检测ViBe算法

    一.运动目标检测简介   视频中的运动目标检测这一块现在的方法实在是太多了.运动目标检测的算法依照目标与摄像机之间的关系可以分为静态背景下运动检测和动态背景下运动检测.先简单从视频中的背景类型来讨论. ...

  3. ios开发之 -- 5分钟集成融云的客服功能

    最近项目中遇到了客服的功能,首先想到的就是使用融云的功能,因为以前做的即时通讯的项目,用的都是融云的sdk,花了点时间研究了下,希望能帮到大家! 废话不多说,步骤如下: 一.申请融云账号 二.创建应用 ...

  4. Fragment遇到的坑

    Fragment不要通过构造传参,要么就是bundle,要么就通过activity临时存一下,不然debug编译没问题release编译不过

  5. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

    [BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...

  6. 【BZOJ4636】蒟蒻的数列 STL

    [BZOJ4636]蒟蒻的数列 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个 ...

  7. netty接收大文件的方法

    参考:http://blog.csdn.net/linuu/article/details/51371595 https://www.jianshu.com/p/a0a51fd79f62 netty默 ...

  8. Configuring Logging 配置日志

    NGINX Docs | Configuring Logging https://docs.nginx.com/nginx/admin-guide/monitoring/logging/[ 在上层设置 ...

  9. 购物车 cookie session

    0-服务器识别用户的目的:服务器存有不同用户的信息,而对这些信息,服务器自身.网站开发管理者.网站访问者会对其读写: 1-暂且存入服务器数据库,购物车分为2种表:购物车入车表和购物车下单表: 2-单个 ...

  10. activate mod_rewrite How To Set Up mod_rewrite for Apache on Ubuntu 14.04 Apache Rewrite url重定向功能的简单配置

    https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-0 ...