【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匹配 ...
随机推荐
- linux 的real time 、user time、 sys time
<APUE>上提到了三个时间Real time, User time和Sys time.这三者是什么关系呢?在使用time(1)命令的时候,为什么real time < user t ...
- 多线程-Thread和ThreadPool
多线程原理 多线程都是基于委托的. 多线程优缺点 缺点: 1.导致程序复杂,开发调试维护困难,特别是线程交互. 2.线程过多导致服务器卡死,因为占用CPU 内存等资源. 优点: 1.良好的交互,特别对 ...
- CORTEX-M3中断的现场保护问题
在<Cortex-M3 Devices Generic User Guide.pdf>中介绍了异常入栈和出栈的情况,详见2.3 Exception model.Cortex-M3内核的寄存 ...
- React拾遗(下)
reconciliation(协调算法) react用于更新DOM的算法.基于两点假设,实现了一个启发的O(n)算法: 两个不同类型的元素将产生不同的树. 通过渲染器附带key属性,开发者可以示意哪些 ...
- python 设计模式之中介者模式
#先啰嗦一下 至少半个多月的样子没写博客了,月初去了趟黄山,赏了美景,自然没时间也没条件敲博客了,一个多星期就这么过去了.返回深圳后,工作积压了一堆,然后白天就马不停蹄的忙工作,晚上回家伺候小娃,又想 ...
- keras Dense 层
文档地址:https://keras.io/layers/core/#dense keras.layers.Dense(units, activation=None, use_bias=True, k ...
- OS X环境下如何搭建编译Cocos2D-X v3.x的Android Studio工程
Cocos2D-X官网已经简单介绍了如何在OS X环境下搭建Cocos2D-X v2.x和v3.x的指南.具体链接为:http://www.cocos.com/doc/article/index?ty ...
- delete 删除指针
危险的代码: int* p=new int(1); delete p; delete p; 安全的代码: int* p=new int(1); delete p; p = NULL; ...
- MySQL数据库之多线程备份工具mydumper
Mydumper介绍: 1)Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具 2)特性: 轻量级C语言编写 执行速度比mysqldump快10倍 快速的文件压缩 支持导 ...
- web手工项目03-登录功能测试用例及缺陷编写-流程图画法-前后台下单及发货流程图-流程图设计测试用例方法-功能测试涉及到的四种数据库场景
回顾 注册功能测试(步骤,需求分析(输入分析,处理分析,输出分析),数据构造(有效等价类,无效等价类,有效数据,无效数据),编写用例,执行用例,缺陷报告) 轮播图功能测试(步骤,需求分析拆分测试点,测 ...