UVALive - 4975_Casting Spells
题意很简单,给你一个字符串,要求你求出一个最长的形似于w(wr)w(wr)的最长连续子串的长度。wr表示w的逆序串。
在这里大家很容易就能想到Manacher算法求回文串。没有错,就是这个。
算法的详细过程我就不说了,直接说后面的实现吧。
通过manacher算法我们可以在O(N)的时间复杂度以内求出一每两个字符空缺处为对称轴的最长回文串的长度。
这样我们可以对于每一个空缺位置逐一枚举,然后分别对匹配求出一个最长的回文串就可以了。
中间有好多细节要优化哦,我wa了好几发。还有在写的时候一定要写出各种优化。代码不要写挫了。
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 1000100
using namespace std; char tep[maxn],s[*maxn];
int a[*maxn],n,ans,now; void init()
{
now=;
s[]='@';
s[]='#';
for (int i=; tep[i]; i++)
{
s[now++]=tep[i];
s[now++]='#';
}
s[now]='$';
s[now+]=;
} void deal()
{
memset(a,,sizeof a);
int mx_c=,mx_r=;
a[]=;
for (int i=; i<now; i++)
{
if (mx_r>i)
{
a[i]=min(mx_r-i+,a[*mx_c-i]);
}
else a[i]=;
while (s[i-a[i]]==s[i+a[i]]) a[i]++;
if (i+a[i]>mx_r) mx_r=i+a[i]-,mx_c=i;
}
} int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%s",tep);
for (int i=; tep[i]; i++)
if (tep[i]>='A' && tep[i]<='Z')
tep[i]=tep[i]-'A'+'a';
ans=;
init();
deal();
for (int i=; s[i]; i+=)//这里一定是+2,只有空缺处才是满足条件的。
{
int cur=a[i]-;
while (cur%!= || cur/>=i) cur--;
for (; cur>ans; cur-=)
{
if (a[i+cur/]>=cur/+ && a[i-cur/]>=cur/+)
{
ans=cur;
break;
}
}
}
printf("%d\n",ans);
}
return ;
}
UVALive - 4975_Casting Spells的更多相关文章
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
随机推荐
- 20155229 2016-2017-2 《Java程序设计》第二周学习总结
20155229 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 布尔:boolean类型可表示true和false %符号被用来作为控制符号前置,所以规定用 ...
- 20155306 2006-2007-2 《Java程序设计》第4周学习总结
20155306 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承:面向对象中,为避免多个类间重复定义共同行为. 运 ...
- 20145226夏艺华 《Java程序设计》实验报告一
实验一 Java开发环境的熟悉(Linux + IDEA) 实验内容 使用JDK编译.运行简单的Java程序: 使用Eclipse 编辑.编译.运行.调试Java程序. 实验步骤 (一)命令行下Jav ...
- centos安装smokeping
本文摘自网友博客,并亲自验证 博客地址:https://blog.csdn.net/erica_yue/article/details/78455101 1.安装依赖包: yum install -y ...
- day 1 异常基本功能
1.什么是异常?程序出现的错误 In [1]: open('xxx.txt') ------------------------------------------------------------ ...
- VBA_常用VBA代码
'批量替换字符 Sub Test() Dim i As Integer ).Value = "已激活" Then Cells(i, ).Value = "Active&q ...
- python简介、第一个python程序、变量、字符编码、用户交互程序、if...else、while、for
也愿大家永葆初心-- 已识乾坤大,犹怜草木青. 一.python简介 首先,我们普及一下编程语言的基础知识.用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个MP3,编写一个文档等等,而计算 ...
- Tensorflow基本开发架构
Tensorflow基本开发架构 先说句题外话, 这段时间一直研究爬虫技术,主要目的是为将来爬取训练数据做准备,同时学习python编程.这一研究才发现,python的开发资源实在是太丰富了,所有你能 ...
- halcon中关于文本的创建以及写入
原文链接:http://blog.sina.com.cn/s/blog_61cc743001017nxr.html#FileName 1.open_file( : : FileName, FileTy ...
- SICP读书笔记 1.3
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...