Harry and magic string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 576    Accepted Submission(s): 287

Problem Description

Harry got a string T, he wanted to know the number of T’s disjoint palindrome substring pairs. A string is considered to be palindrome if and only if it reads the same backward or forward. For two substrings of T:x=T[a1…b1],y=T[a2…b2](where a1 is the beginning index of x,b1 is the ending index of x. a2,b2 as the same of y), if both x and y are palindromes and b1<a2 or b2<a1 then we consider (x, y) to be a disjoint palindrome substring pair of T.

Input

There are several cases.
For each test case, there is a string T in the first line, which is composed by lowercase characters. The length of T is in the range of [1,100000].

Output

For each test case, output one number in a line, indecates the answer.

Sample Input

aca aaaa

Sample Output

3 15

Hint

For the first test case there are 4 palindrome substrings of T. They are: S1=T[0,0] S2=T[0,2] S3=T[1,1] S4=T[2,2] And there are 3 disjoint palindrome substring pairs. They are: (S1,S3) (S1,S4) (S3,S4). So the answer is 3.

Source

BestCoder Round #25

Recommend

heyang   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443

题意:

在一个S串中找两个回文串,两个回文串没有重叠的部分。问有多少个这样的回文串对。

思路:

用manacher我们可以求出以$i$为中心的回文串的个数。

$pre[i]$表示以$i$为开头的回文串的个数,$suf[i]$表示以$i$为结尾的回文串的个数。

那么我们要求的答案其实就是$\sum_{i = 1}^{len} pre[i] * (\sum_{j=1}^{i-1} suf[j])$

所以我们再用一个数组$sum$来存$suf$数组的前缀和。

那么$pre$和$suf$怎么求呢?

我们manacher处理出的$p[i] /2$表示的是以$i$为中心的回文的个数。

那么对于$i$之前的半径内的字符,他们的$pre$可以$+1$。同样他之后的半径内的字符,$suf$可以$-1$

暴力统计肯定是不行的,我们可以使用差分的思想。

对于$[i,j]$区间内的每一个数都更新$k$的话,我们可以用一个数组$add[i] += k$,add[j+1] -=k$

然后从前往后求前缀和得到的就是对区间的全部更新了。求$suf$和$pre$也是同样的道理。

$pre$是后面影响前面的所以从后往前求“#”向前靠,$suf$从前往后求“#”向后靠。

 #include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
//#include<cstdlib>
#include<cstring>
#include<algorithm>
//#include<queue>
#include<vector>
//#include<set>
//#include<climits>
//#include<map>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e5 + ;
char s[maxn], ss[maxn * ];
LL pre[maxn * ], suf[maxn * ], sum[maxn * ];
int lens, p[maxn * ]; int init()
{
ss[] = '$';
ss[] = '#';
int lenss = ;
for(int i = ; i < lens; i++){
ss[lenss++] = s[i];
ss[lenss++] = '#';
}
ss[lenss] = '\0';
return lenss;
} void manacher()
{
int lenss = init();
int id, mx = ;
for(int i = ; i < lenss; i++){
if(i < mx){
p[i] = min(p[ * id - i], mx - i);
}
else{
p[i] = ;
}
while(ss[i - p[i]] == ss[i + p[i]])p[i]++;
if(mx < i + p[i]){
id = i;
mx = i + p[i];
} }
} int main()
{
while(scanf("%s", s) != EOF){
lens = strlen(s);
/*for(int i = 0; i < lens * 2 + 3; i++){
p[i] = 0;
}*/
for(int i = ; i < lens + ; i++){
suf[i] = pre[i] = sum[i] = ;
} manacher(); for(int i = ; i <= lens * ; i++){
int x = (i + ) / ;//向上取整
suf[x]++, suf[x + (p[i] / )]--;
}
for(int i = lens * ; i >= ; i--){
int x = i / ;
pre[x]++, pre[x - (p[i] / )]--;
} for(int i = lens; i >= ; i--){
pre[i] += pre[i + ];
}
for(int i = ; i <= lens; i++){
suf[i] += suf[i - ];
sum[i] += sum[i - ] + suf[i];
}
LL ans = ;
for(int i = ; i <= lens; i++){
ans += (LL)pre[i] * sum[i - ];
}
printf("%I64d\n", ans);
}
return ;
}

hdu5157 Harry and magic string【manacher】的更多相关文章

  1. Switch选择语句能否作用在String【字符串】上,也就是能否这么写:Switch(一个字符串变量)?

    Switch选择语句能否作用在String[字符串]上,也就是能否这么写:Switch(一个字符串变量)? 解答:不可以,只能处理int,byte,short,char,(其实是只能处理int,其它三 ...

  2. 345. Reverse Vowels of a String【easy】

    345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...

  3. 344. Reverse String【easy】

    344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...

  4. 【Manacher】Colorful String

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

  5. 【manacher】HDU3068-最长回文

    [题目大意] 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. [manacher知识点] ①mx - i > P[j] 的时候,以S[j]为中心的回文子串 ...

  6. Codeforces Round #445 D. Restoration of string【字符串】

    D. Restoration of string time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. 【manacher】HDU4513-吉哥系列故事——完美队形II

    [题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...

  8. 2018ACM-ICPC南京区域赛M---Mediocre String Problem【exKMP】【Manacher】

    这题就单独写个题解吧.想了两天了,刚刚问了一个大佬思路基本上有了. 题意: 一个串$S$,一个串$T$,在$S$中选一段子串$S[i,j]$,在$T$中选一段前缀$T[1,k]$使得$S[i,j]T[ ...

  9. 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

随机推荐

  1. 如何生成唯一的server Id,server_id为何不能重复?

    我们都知道MySQL用server-id来唯一的标识某个数据库实例,并在链式或双主复制结构中用它来避免sql语句的无限循环.这篇文章分享下我对server-id的理解,然后比较和权衡生成唯一serve ...

  2. 百度「Web 前端研发部」面试过程和常见问题 可能会采用哪些方法来面试 STAR 面试法 喜欢什么样的面试者 喜欢问的问题

    http://segmentfault.com/a/1190000002498800 在他们的github上看到的,收藏一下备用.看完觉得还有很多要努力的地方. FEX 的面试过程 我们一般会有 3 ...

  3. Socket网络编程--聊天程序(2)

    上一节简单如何通过Socket创建一个连接,然后进行通信.只是每个人只能说一句话.而且还是必须说完才会接收到信息,总之是很不方便的事情.所以这一小节我们将对上一次的程序进行修改,修改成每个人可以多说话 ...

  4. 2. Attention Is All You Need(Transformer)算法原理解析

    1. 语言模型 2. Attention Is All You Need(Transformer)算法原理解析 3. ELMo算法原理解析 4. OpenAI GPT算法原理解析 5. BERT算法原 ...

  5. YouTube上最火的十个大数据视频

    http://blog.jobbole.com/84148/ YouTube上最火的十个大数据视频

  6. java反编译工具(Java Decompiler)

    1,下载地址,包括GUI,Eclipse插件 http://jd.benow.ca/ 2,Eclipse插件的安装参看 https://blog.csdn.net/yh_zeng2/article/d ...

  7. 根据IP获取国家

    国外的还算比较权威的IP地址库,而且免费,每天调用次数不超过1000免费.超过另收费. public string Ip2Country(string ip) { try { string url = ...

  8. Gradle依赖的统一管理,解决依赖冲突

    看见别人在用implementation rootProject.ext.dependencies["xxxx"]不知道是什么意思,上网查了一下,原来是为了解决或者说预防gradl ...

  9. 假期小结 BIO, NIO, AIO

    虽然忙碌,但仍小有收获,开心. 引子 BIO: Blocking IO,阻塞式IO NIO: Non-blocking IO,非阻塞式IO AIO: Async IO,异步IO 问题 什么是阻塞式IO ...

  10. wvblk 把 xp、2003、win7(32位) 装入 VHD

    关键1:是[预安装]阶段F6加载wvblk驱动: or 在还原ghost镜像后,导入wvblk驱动. 关键1.5:对于 win7(32位)来说,还可以在设备管理器内,通过添加“过时”硬件的方式导入wv ...