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 ...
随机推荐
- 20155310 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
20155310 <Java程序设计>实验三(敏捷开发与XP实践)实验报告 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验步骤 (一)敏捷开发与XP 1.敏捷开发 敏捷开发( ...
- rman的基于窗口的备份保留策略学习
例如: rman>configure retention policy to recovery window of 7 days; 那么就是说,至少要使得保留下来的备份,可以支持恢复到从当前回溯 ...
- day 1 异常基本功能
1.什么是异常?程序出现的错误 In [1]: open('xxx.txt') ------------------------------------------------------------ ...
- day4 基础
1.变量 2.打印名片 3.raw_input() 和 input() 4.变量类型 type 5. if-else 6.标示符的规则 7.关键字 8.运算符 9.print打印多个值 1.变量 sc ...
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 的解决方法
解决方法如下: 方法 1.在线修改提高允许的max_connection_errors数量: A. 登录Mysql数据库查看max_connection_errors: mysql>show ...
- sql异常 获取数据失败的原因及解决方案
使用dbutils工具类时 不能使用char作为sql的字段类型 报错提示不能转换 所以替换成别的(一般是String)即可
- 用php实现简单的自制计算器
存档: <!DOCTYPE html> <html> <head> <title>PHP实现计算器</title> </head> ...
- Linux TCP/IP调优参数 /proc/sys/net/目录
所有的TCP/IP调优参数都位于/proc/sys/net/目录. 例如, 下面是最重要的一些调优参数,后面是它们的含义: /proc/sys/net/core/rmem_default " ...
- php+MySQL的对用户表分表,使用户均匀分布
假如说我们目前已有一亿个注册用户,要把这些用户平均分配到100张表中,并且后续注册的用户也要均匀分配到这100张表 首先当用户注册时,如用户名为“username”,用php的crc32()函数处理用 ...
- 跟浩哥学自动化测试Selenium -- Selenium简介 (1)
Selenium 简介 Selenium 是一款开源的web自动化测试工具,用来模拟对浏览器的操作(主要是对页面元素的操作),简单来讲,其实就是一个jar包.Selenium早期的版本比如1.0市场占 ...