【Manacher】Colorful String
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
【Manacher】Colorful String的更多相关文章
- 【CF827E】Rusty String 调和级数+FFT
[CF827E]Rusty String 题意:给你一个01串,其中部分字符是'?',?可以是0或1,求所有可能的d,满足存在一种可能得到的01串,在向右移动d格后与自己相同. $n\le 5\tim ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【题解】Rusty String [CF827E]
[题解]Rusty String [CF827E] 传送门:\(\text{Rusty String}\) \(\text{[CF827E]}\) [题目描述] 多组数据,每组数据给出一个由 \(V, ...
- 【manacher】HDU3068-最长回文
[题目大意] 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. [manacher知识点] ①mx - i > P[j] 的时候,以S[j]为中心的回文子串 ...
- 【manacher】HDU4513-吉哥系列故事——完美队形II
[题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...
- hdu5157 Harry and magic string【manacher】
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 2018ACM-ICPC南京区域赛M---Mediocre String Problem【exKMP】【Manacher】
这题就单独写个题解吧.想了两天了,刚刚问了一个大佬思路基本上有了. 题意: 一个串$S$,一个串$T$,在$S$中选一段子串$S[i,j]$,在$T$中选一段前缀$T[1,k]$使得$S[i,j]T[ ...
- BZOJ2160 拉拉队排练【Manacher】
Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训 ...
- BZOJ4755 [JSOI2016]扭动的回文串 【后缀数组】【manacher】
题目分析: 我写了史上最丑的后缀数组,怎么办? 首先manacher一遍两个串,这样只用考虑第三问.用$作为间隔符拼接两个串,把第一个串翻转.枚举回文中心,取最长的回文串,对于剩下的部分利用LCP匹配 ...
随机推荐
- ICEM-三棱锥的一种画法(2D转3D)
原视频下载地址:https://yunpan.cn/cqwC3eZHn5AvJ 访问密码 9456
- Jenkins自动化打包(Gitlab)并上传蒲公英
整个过程详见:https://www.jianshu.com/p/91e8f571fc2b 以下是遇到的问题及解决过程 一.安装homebrew(网速很慢很慢……被墙了) /usr/bin/ruby ...
- 走进JavaWeb技术世界8:浅析Tomcat9请求处理流程与启动部署过程
谈谈 Tomcat 请求处理流程 转自:https://github.com/c-rainstorm/blog/blob/tomcat-request-process/reading-notes &l ...
- java权限管理与用户角色权限设计
java权限管理与用户角色权限设计 实现业务系统中的用户权限管理 B/S系统中的权限比C/S中的更显的重要,C/S系统因为具有特殊的客户端,所以访问用户的权限检测可以通过客户端实现或通过客户端+服务器 ...
- jmeter 常用插件
一.下载安装及使用 下载地址:https://jmeter-plugins.org/install/Install/ 安装:下载后文件为plugins-manager.jar格式,将其放入jmeter ...
- qt 2D绘图技巧
2D绘图 Qt4中的2D绘图部分称为Arthur绘图系统.它由3个类支撑整个框架,QPainter,QPainterDevice和QPainterEngine.QPainter用来执行具体的绘图相关操 ...
- Tomcat中配置URIEncoding="UTF-8"来处理中文的处理
Tomcat中配置URIEncoding="UTF-8"来处理中文的处理 打开 server.xml 文件,更改两个地方. 配置一:添加 URIEncoding="UTF ...
- 几种主流浏览器内置http抓包工具软件使用方
对于学习网站的人或者相关编程人员,经常需要用到http抓包工具来跟踪网页,但主流抓包软件如httpwatch.httpanalyzerstdv都是收费的,破解版往往也不稳定.实际上现在很多浏览器都内置 ...
- 006-多线程-集合-Set-ConcurrentSkipListSet
一.简介 ConcurrentSkipListSet是线程安全的有序的集合,适用于高并发的场景.ConcurrentSkipListSet和TreeSet,它们虽然都是有序的集合.但是,第一,它们的线 ...
- osgOcean编译
E:\Visual Studio 2015\install\VC>e: E:\Visual Studio 2015\install\VC>E:\Visual Studio 2015\ins ...