【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匹配 ...
随机推荐
- 2018-2019-2 20165234 《网络对抗技术》 Exp7 网络欺诈防范
Exp7 网络欺诈防范 实验内容 1. 简单应用SET工具建立冒名网站 2. ettercap DNS spoof 3. 结合应用两种技术,用DNS spoof引导特定访问到冒名网站 4. 请勿使用 ...
- this和super的用法
this关键字的使用: 1.成员变量和局部变量重名时,在方法中调用成员变量,需要使用this.调用. 2.把这个类自己的实例化对象当做参数进行传递时,使用this. 3.内部类中,调用外部类的方法或变 ...
- 分布式文件上传 spring boot + fastdfs + dropzone
1.首先安装fastDFS 参考链接: https://www.funtl.com/zh/spring-cloud-itoken-codeing/%E5%88%86%E5%B8%83%E5%BC%8F ...
- 会话技术cookie与session
目录 会话技术cookie 会话技术 cookie 服务器怎样把Cookie写 给客户端 服务器如何获取客户端携带的cookie session session简介 Session如何办到在一个ser ...
- fcntl 函数
设置文件的flags,阻塞设置成非阻塞,非阻塞设置成阻塞(这连个在server开发中可以封装为基本函数) 线程引入 pthread_self 和 pthread_equal 原因 ——解决不同平台的问 ...
- idea备忘
1.idea 最近打开的文件个数 File->Settings->Editor->General->Editor Tabs->Tab Closing Policy-> ...
- android: ListView设置emptyView 误区
使用ListVIew 来设置EmptyView的时候须注意: ListView listview = (ListView) findViewById(R.id.list); View emptyVie ...
- IOS APP开发入门案例
1.创建新项目 2.设计布局,main.storyboard中,在控件库中 3.布局控件关联控制器 4.设置事件或者显示模式 5.编写代码: import UIKit class ViewContro ...
- Ubuntu16.04 + cuda9.0 +cudnn7.1(转载)
转载一个详细可用的ubuntu16.04+cuda9.0+cudnn7.1教程. 0 - 参考材料 https://blog.csdn.net/Umi_you/article/details/8026 ...
- vue+php接口
php: <?php header('Access-Control-Allow-Origin:*'); $date = $_POST['data'];$cars=array("Volv ...