HDU 3294 (Manacher) Girls' research
变形的求最大回文子串,要求输出两个端点。
我觉得把'b'定义为真正的'a'是件很无聊的事,因为这并不会影响到最大回文子串的长度和位置,只是在输出的时候设置了一些不必要的障碍。
另外要注意一下原字符串s1中的字符在预处理以后的字符串s2中对应的坐标关系,这样输出的时候就可以照着这个关系转化。
轻松1A,嘿嘿
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
char s1[maxn], s2[maxn * ], real;
int p[maxn * ]; void init(char s1[], char s2[])
{
int j = ;
s2[] = '$', s2[] = '#';
for(int i = ; s1[i] != '\0'; ++i)
{
s2[j++] = s1[i];
s2[j++] = '#';
}
s2[j] = '\0';
} void manacher(char s[])
{
p[] = ;
int id, mx = ;
for(int i = ; s[i] != '\0'; ++i)
{
if(mx > i)
p[i] = min(p[id*-i], mx-i);
else
p[i] = ;
while(s[i + p[i]] == s[i - p[i]])
++p[i];
if(i + p[i] > mx)
{
id = i;
mx = i + p[i];
}
}
} char change(char c)
{
c -= real - 'a';
if(c < 'a') c += ;
if(c > 'z') c -= ;
return c;
} void getans(void)
{
int mlen = , pos;
for(int i = ; s2[i] != '\0'; ++i)
{
if(p[i] - > mlen)
{
mlen = p[i] - ;
pos = i;
}
}
if(mlen == )
printf("No solution!\n");
else
{
int L = (pos - (p[pos] - )) / - ;
int R = (pos + (p[pos] - )) / - ;
printf("%d %d\n", L, R);
for(int i = L; i <= R; ++i)
printf("%c", change(s1[i]));
printf("\n");
}
} int main(void)
{
#ifdef LOCAL
freopen("3294in.txt", "r", stdin);
#endif while(scanf("%c", &real) == )
{
scanf("%s", s1);
getchar();
init(s1, s2);
manacher(s2);
getans();
}
return ;
}
代码君
HDU 3294 (Manacher) Girls' research的更多相关文章
- HDU 3294 Manacher模版题
点击打开链接 题意:求最长回文子串所在的区间,然后第一个字符代表a,以后的顺推,最后输出这个区间顺推后的串 思路:Manacher轻松水过.记录下最长回文串的位置和长度即可了,然后输出时自己处理一下, ...
- hdu 3294 manacher 求回文串
感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...
- (回文串 Manacher )Girls' research -- hdu -- 3294
http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit:1000MS Memory Limit:32 ...
- Hdu 3294 Girls' research (manacher 最长回文串)
题目链接: Hdu 3294 Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...
- Girls' research(马拉车算法) hdu 3294
文章目录 思路如下 Manachar代码注释 题解如下 Problem Description One day, sailormoon girls are so delighted that they ...
- HDU 3294 Girls' research(manachar模板题)
Girls' researchTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- Manacher 算法(hdu 3068 && hdu 3294)
今天打算补前晚 BC 的第二题,发现要用到能在 O(n) 时间求最大回文子串长度的 Manacher 算法,第一次听,于是便去百度了下,看了大半天,总算能看懂了其思想,至于他给出的代码模板我没能完全看 ...
- HDU----(3294)Girls' research(manacher)
Girls' research Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)T ...
- Girls' research(manacher)
Girls' research Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) ...
随机推荐
- codeforces 430A Points and Segments (easy)(理解能力有待提高……)
题目 //终于看懂题目了,,,, //一条线段里面不是每个坐标上都有要染色的点,所以为了满足条件,只能考虑那些给出坐标的点 //所以就要排序一下了,不能直接根据坐标0 1 0 1…… #include ...
- codeforces 463D Gargari and Permutations(dp)
题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...
- ZOJ 1115 Digital Roots(简单,字符串与数)
题目 //好一道水水题,可是我居然也错了那么多次,后来百度来发现是因为数据数位可能很长很长,要用字符串数组... //简单 //有坑啊——数据可能很大很大,要用字符串表示! #include<s ...
- 关于iOS7 上下黑条解决办法
解决办法 找到工程的targest->General->Launch Image Sourse OK 运行问题解决
- JAVA容器
JAVA容器 一.容器体系结构 java.util 二.迭代器Iterator<E> 迭代器是一种设计模式,可以遍历并选择序列中的对象,而开发人员并不需要了解该序列的底层结构.迭代器通常被 ...
- Windows 回调监控 <一>
在x86的体系结构中,我们常用hook关键的系统调用来达到对系统的监控,但是对于x64的结构,因为有PatchGuard的存在,对于一些系统关键点进行hook是很不稳定的,在很大几率上会导致蓝屏的发生 ...
- spring_150804_controller
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- Shell练习 行列转换
原题:https://leetcode.com/problems/transpose-file/Given a text file file.txt, transpose its content. Y ...
- C语言运算符优先级表
优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 () 圆括号 (表达式)/函数名(形参表) . 成员选择(对象) 对象.成员名 -& ...
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等
http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...