Palindromic String

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/contest/show/61

Description

秋实大哥喜欢探索新鲜事物,最近他发明了一种新型回文串,叫K重回文串!今天他想用它来考考小朋友们。

秋实大哥给出了与K重回文串有关的信息

任何字符串都属于0重回文串,包括空字符串。
    一个长度为N的字符串S,S是K(k≥1)重回文串,当且仅当S是回文串,且其长度为⌊N2⌋的前缀和长度为⌊N2⌋的后缀是K−1重回文串。
    如果一个字符串是K重回文串,则称该字符串有一个回文值为K。一个字符串可以有多个回文值,比如S=abaaba,其回文值可为0,1,2,3。
    字符串的最大回文值是该字符串所有回文值的最大值。
    若字符串S的最大回文值≥1,则S一定是回文串。
    一个字符串S,如果其正着读和反着读是一样的,则称S是回文串,比如aabaa,aba,a。但abc,abab,aacba就不是回文串。
    一个长度为N的字符串S,其有N+1个前缀和N+1个后缀(不一定非空),比如abcde,有6个前缀,分别是空字符串,a,ab,abc,abcd,abcde;有6个后缀,分别是空字符串,e,de,cde,bcde,abcde。

秋实大哥给你一个字符串S,他想问问你,S所有前缀的最大回文A的某一段完全重合,或者能够经过上下左右平移与折线A的某一段完全重合,则表示秋实大哥吹出了妹子的一部分旋律。

Input

第一行输入一个字符串S(0<|S|≤2⋅106),S包含 大写英文字母(A-Z),小写英文字母(a-z),数字(0-9)

Output

输出一个整数,表示S所有前缀的最大回文值之和。

Sample Input

CCeCeCCCee

Sample Output

4

HINT

题意

题解:

首先跑一发马拉车算法,然后得到所有字符的P数组,表示回文串的长度
然后我们再递归求解
然后就可以知道每一个回文串是几重
(虽然我觉得复杂度可能会很高,但是跑的很快,不知道为什么……

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000010
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** char s[maxn];
char str[maxn*];
int l;
int p[maxn*];
int manacher(char s[],int l)
{
int i,j,k,ans=;
for(i=;i<=l;++i)str[i<<]=s[i],str[(i<<)+]='#';
str[]='#';str[l*+]='#';str[]='&';str[l*+]='$';
l=l*+;j=;
for(i=;i<=l;)
{
while(str[i-j-]==str[i+j+])++j;
p[i]=j;if(j>ans)ans=j;
for(k=;k<=j&&p[i]-k!=p[i-k];++k)p[i+k]=min(p[i-k],p[i]-k);
i+=k;j=max(j-k,);
}
return ans;
} int main()
{
scanf("%s",s+);
l=strlen(s+);
manacher(s,l);
l=l*+;
int ans=;
for(int i=;i<=(l+)/;i++)
{
int tmp=p[i];
int pos=i;
while(tmp+>=pos&&tmp!=)
{
ans++;
pos=(pos+)/;
tmp=p[pos];
}
//cout<<ans<<endl;
}
cout<<ans<<endl;
}

2015 UESTC 搜索专题M题 Palindromic String 马拉车算法的更多相关文章

  1. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  2. 2015 UESTC 搜索专题N题 韩爷的梦 hash

    韩爷的梦 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...

  3. 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

    秋实大哥の恋爱物语 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...

  4. 2015 UESTC 搜索专题E题 吴队长征婚 爆搜

    吴队长征婚 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...

  5. 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS

    基爷与加法等式 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  6. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  7. 2015 UESTC 搜索专题A题 王之迷宫 三维bfs

    A - 王之迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  8. 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp

    全都是秋实大哥 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  9. 2015 UESTC 搜索专题D题 基爷的中位数 二分

    基爷的中位数 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descr ...

随机推荐

  1. 笔记本自开wifi设置

    笔记本自开wifi设置 是这样的有些笔记本他自身就可以放出热点供其他的小伙伴们连接,不用非得去下专门的工具有些笔记本的网卡是自带支持双收发的(这里注意我指的是有些笔记本不是全部) 命令我已经写出来了  ...

  2. Linux中等待队列的实现

    1.       等待队列数据结构 等待队列由双向链表实现,其元素包括指向进程描述符的指针.每个等待队列都有一个等待队列头(wait queue head),等待队列头是一个类型为wait_quequ ...

  3. springboot + swagger2 生成api文档

    直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  4. aws rds

    1.还原快照,注意设置安全组的问题:不然会导致还原后连接不上:

  5. Prime

    #include<iostream>#include<cstdio>#include<cstring>using namespace std; const int ...

  6. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  7. 进程自我保护 适用于WIN7 X64

    //进程自我保护,注意只有X64 WIN7可用 #include <ntddk.h> #define PROCESS_TERMINATE 1 typedef struct _LDR_DAT ...

  8. 【58沈剑架构系列】细聊分布式ID生成方法

    一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id (2)订单标识:order-id (3)帖子标识:tiezi-id 这个记录标识往往就是数据 ...

  9. Visual Studio 中如何同时注释多行和取消注释多行

    注释多行:先按 Ctrl - K 组合键,再按 Ctrl - C 组合键 取消注释多行:先按 Ctrl - K 组合键,再按 Ctrl - U 组合键

  10. Loadrunner报错“Too many local variablesAction.c”解决方法

    问题描述,在Action.c里定义数组时如果数组长度过长,如char a[1024*1024]运行时即会报错: 意思为:太多的局部变量 问题原因及解决方法如下: 1. VuGen对于局部变量可以分配的 ...