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 ...
随机推荐
- 20155236 《Java程序设计》实验五(网络编程与安全)实验报告
20155236 <Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上 ...
- 在Linux中安装JDK和IDEA
前言 寒假安装虚拟机的时候我就没有安装好,到学校之后,因为时间紧加上更习惯Windows的操作习惯,我只在Windows上安装了JDK和IDEA,但是随着学习的深入,我发现用虚拟机写命令行.新建jav ...
- 【CF527C】Glass Carving
[CF527C]Glass Carving 题面 洛谷 题解 因为横着切与纵切无关 所以开\(set\)维护横着的最大值和纵着的最大值即可 #include <iostream> #inc ...
- Maven学习(十一)-----使用Maven创建Web应用程序项目
使用Maven创建Web应用程序项目 用到的技术/工具: Maven 3.3.3 Eclipse 4.3 JDK 8 Spring 4.1.1.RELEASED Tomcat 7 Logback 1. ...
- 面试时让你说一个印象最深的bug,该怎么回答
其实,面试官并不关心你描述的这个bug是否真的有价值,或有多曲折离奇?他只是: * 了解你平时工作中的测试能力 所以,这就要求的你平时工作中遇到bug时试着自己去定位,定位bug的过程远比你的单纯的执 ...
- i3wm随笔 1
快捷键 mod+0 退出 mod+v 垂直分割 mod+h 水平风格
- nginx 日志模块
ngx_http_log_module.c 数据结构 typedef struct { void **main_conf; void **srv_conf; void **loc_conf;} ngx ...
- Twitter推广消息可使品牌线下销售额增长三成
新浪科技讯 北京时间8月9日上午消息,Twitter周四宣布,该公司的推广消息(Promoted Tweet)可以让品牌的线下销售增长29%. 此外,Twitter当天还推出了一个新项目,让品牌可以追 ...
- Fulfilling Work: The Shippers More entrepreneurs hire 'fulfillment' outfits to store and ship their products
By Stu Woo June 23, 2011 Brett Teper faced a logistical problem when he and a partner founded ModPro ...
- 用 Python 3 的 async / await 做异步编程
前年我曾写过一篇<初探 Python 3 的异步 IO 编程>,当时只是初步接触了一下 yield from 语法和 asyncio 标准库.前些日子我在 V2EX 看到一篇<为什么 ...