The value of a string s is equal to the number of different letters which appear in this string.

Your task is to calculate the total value of all the palindrome substring.

Input
The input consists of a single string |s|(≤∣s∣≤×^ ). The string s only contains lowercase letters. Output
Output an integer that denotes the answer. 样例输入
abac 样例输出 样例解释
abac has palindrome substrings a,b,a,c,abaa,b,a,c,aba,ans the total value is equal to ++++=。

【题解】

  Manacher,先预处理一波,然后找出每一个位置的26个字母下一个位置,存在一个vector里面,然后最后找的时候就是差值 × 对应的个数。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+;
char S[N],T[N<<];
int Len[N*];
int nxt[N][];
vector<int>V[N];
void Manacher(char *s){
int L=strlen(s);
int po = , mx= ; for(int i=;i<=L*;i++){
T[i] = i&? '#' : s[i/-] ;
} T[] = '@';
T[*L+] = '#';
T[*L+] = '\0'; ll tmp = ;
for(int i=;i<=*L;i++){
if( i<mx ){
Len[i]=min( mx-i , Len[*po-i] );
}else{
Len[i] = ;
} while( T[i+Len[i]]==T[i-Len[i]] ){
Len[i]++;
}
if( i + Len[i] > mx ){
po = i;
mx = i + Len[i];
}
}
}
int main()
{
scanf("%s",S);
Manacher(S);
int len1 = strlen( S ) ;
int len2 = strlen( T ) ; for( int j = ; j < ; j++ ){
int pos = INT_MAX ;
for( int i = len1 - ; i >= ; i-- ){
if( S[i] == j + 'a' ) pos = i ;
nxt[i][j] = pos ;
}
} for( int i = ; i < len1 ; i++ ){
for( int j = ; j < ; j++ ){
V[i].push_back( nxt[i][j] ) ;
}
sort( V[i].begin() , V[i].end() );
}
/*
for( int i = 1 ; i < len2 ; i++ ){
printf("%d : %c , Len %d \n",i , T[i] , Len[i] );
}
*/
ll ans = ;
for( int i = ; i < len2 ; i++ ){
int L = Len[i] / ;
if( L == ) continue ; int Id = i / - ;
if( i & ) Id ++ ; int RR = Id + L - ;
int Last = V[Id][] ;
int cnt = ;
for( auto x : V[Id] ){
if( x > RR ) break ;
int r = x - ;
ans = ans + (ll)( cnt ++ ) * (ll) ( r - Last + );
Last = x ;
}
if( RR >= Last ){
ans = ans + (ll) cnt * (ll) ( RR - Last + );
}
}
printf("%lld\n",ans);
return ;
} //jswbutgecnmqnuagqnvxfljfffzvudcfvygpro

最新文章

  1. php桥接设计模式
  2. SQL Server 2000:提示“未与信任SQL SERVER连接相关连”错误
  3. Linux下安装PHP
  4. 【转载】理解OAuth 2.0
  5. Windows Server 2008下共享资源访问走捷径 (不用用户名 和 密码 访问共享)
  6. STM32实验非正式报告之DMA
  7. [CSS]cursor鼠标样式
  8. JAVA并发实现二(线程中止)
  9. [置顶] ./build_native 时出现please define NDK_ROOT
  10. GIT 版本控制命令学习
  11. “幸福企业”定义-参观“MES项目”有感
  12. 十余年软件开发经历,经验总结和程序一览(涉及Socket、WPF、vc++、CAD、图像、GIS)
  13. python - 用类写装饰器
  14. 凭据管理 API
  15. 获取Oracle数据库awr报告方法
  16. Docker容器的原理与实践 (下)
  17. eclipse3.3插件更新攻略
  18. python练习笔记——模拟双色球随机输出情况
  19. 最短路径 - 弗洛伊德(Floyd)算法
  20. 思维题--code forces round# 551 div.2

热门文章

  1. ICEM-带肋圆柱
  2. 火狐调试工具-DevTools
  3. [Ubuntu] 14.04版本安装JDK8失败
  4. Java 面向对象(十四)
  5. Mybatis 中的转义字符(转帖)
  6. 002-创建型-02-抽象工厂模式(Abstract Factory)
  7. Spring Cloud(7):事件驱动(Stream)分布式缓存(Redis)及消息队列(Kafka)
  8. iOS-KVO(转)
  9. maven:手动上传jar私服
  10. 从源码角度解析Netty的React模式是如何工作的