题目链接:LightOJ - 1258

1258 - Making Huge Palindromes
Time Limit: 1 second(s) Memory Limit: 32 MB

A string is said to be a palindrome if it remains same when read backwards. So, 'abba', 'madam' both are palindromes, but 'adam' is not.

Now you are given a non-empty string S, containing only lowercase English letters. The given string may or may not be palindrome. Your task is to make it a palindrome. But you are only allowed to add characters at the right side of the string. And of course you can add any character you want, but the resulting string has to be a palindrome, and the length of the palindrome should be as small as possible.

For example, the string is 'bababa'. You can make many palindromes including

bababababab

babababab

bababab

Since we want a palindrome with minimum length, the solution is 'bababab' cause its length is minimum.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.

Output

For each case, print the case number and the length of the shortest palindrome you can make with S.

Sample Input

Output for Sample Input

4

bababababa

pqrs

madamimadam

anncbaaababaaa

Case 1: 11

Case 2: 7

Case 3: 11

Case 4: 19

Note

Dataset is huge, use faster I/O methods.

写个博客证明我学过马拉车...

题意:只能在串末尾加字母,问能形成的最短的回文串的长度。

思路:因为只能在末尾加字母,那么可以知道最后添加的那个字母肯定和第一个字母一样,倒数第二个添加的和第二个字母一样...最坏情况下答案是$2len$

能影响答案的只有末尾段,就是如果末尾有一段是回文的,那么我们不需要去添字母和这一段匹配了,所以求个最长的回文串并且到了最后一个字母的长度$x$。

答案就是$2len-x$

#include <bits/stdc++.h>
using namespace std; const int N = 2e6 + ;
char s[N], mp[N];
int ma[N]; int main() {
int T;
int kase = ;
scanf("%d", &T);
while (T--) {
scanf("%s", s);
int len = strlen(s);
int l = ;
mp[l++] = '$'; mp[l++] = '#';
for (int i = ; i < len; i++) {
mp[l++] = s[i];
mp[l++] = '#';
}
mp[l] = ;
int mx = , id = ;
int ans = ;
for (int i = ; i < l; i++) {
ma[i] = mx > i ? min(mx - i, ma[ * id - i]) : ;
while (i - ma[i] >= && mp[i + ma[i]] == mp[i - ma[i]]) ma[i]++;
if (i + ma[i] > mx) {
mx = i + ma[i];
id = i;
}
if (ma[i] + i == l) ans = max(ans, ma[i] - );
}
printf("Case %d: %d\n", ++kase, * len - ans);
}
return ;
}

Making Huge Palindromes LightOJ - 1258的更多相关文章

  1. Generating Palindromes LightOJ - 1033

    Generating Palindromes LightOJ - 1033 题意:添加最少的字符使得给出的字符串成为回文串.输出添加的字符数. 方法:常规区间dp.ans[i][j]表示使得ans[i ...

  2. lightOJ 1258 Making Huge Palindromes(KMP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1258 就是求逆串和原串的匹配长度 答案就是原串长度的2倍减去匹配长度即可 第一次我将原 ...

  3. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  4. LightOJ 1258 Making Huge Palindromes (Manacher)

    题意:给定上一个串,让你在后面添加一些字符,使得这个串成为一个回文串. 析:先用manacher算法进行处理如果发现有字符匹配超过最长的了,结束匹配,答案就是该字符前面那个长度加上该串原来的长度. 代 ...

  5. D - 楼下水题(kmp+Manacher)

    D - 楼下水题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Statu ...

  6. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  7. Codeforces Round #360 (Div. 2) B. Lovely Palindromes 水题

    B. Lovely Palindromes 题目连接: http://www.codeforces.com/contest/688/problem/B Description Pari has a f ...

  8. LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

    http://lightoj.com/volume_showproblem.php?problem=1213  Fantasy of a Summation Time Limit:2000MS     ...

  9. cf688B-Lovely Palindromes

    http://codeforces.com/problemset/problem/688/B B. Lovely Palindromes time limit per test 1 second me ...

随机推荐

  1. 实现一个 web 服务器

    在 system1 上配置一个站点 http://system1.group8.example.com/,然后执行下述步骤: 1.从 http://server.group8.example.com/ ...

  2. Docker Swarm部署集群

    一.Swarm简介 Swarm是Docker的一个编排工具,参考官网:https://docs.docker.com/engine/swarm/ Swarm 模式简介 要在Swarm模式下运行dock ...

  3. Golang/Go goroutine调度器原理/实现【原】

    Go语言在2016年再次拿下TIBOE年度编程语言称号,这充分证明了Go语言这几年在全世界范围内的受欢迎程度.如果要对世界范围内的gopher发起一次“你究竟喜欢Go的哪一点”的调查,我相信很多Gop ...

  4. Q-Q图和P-P图

     一. QQ图      分位数图示法(Quantile Quantile Plot,简称 Q-Q 图)       统计学里Q-Q图(Q代表分位数)是一个概率图,用图形的方式比较两个概率分布,把他们 ...

  5. MSP---企业上云需要考虑的问题

    一.评估 1.应用是否可以上云: 2.时间:规划时间,迁移时间 2.成本:人力成本,资源成本 二.上云 1.如何上云:选择云厂商,选择MSP 2.云选择:公有云,私有云,混合云,多云(不要最贵的,也不 ...

  6. centos7#yum install ffmpeg

    yum install ffmpeg rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro rpm -Uvh http://li. ...

  7. 一文读懂内网、公网和NAT

    我们做弱电监控系统的时候,都避免不了要跟IP地址打交道,比如摄像头.NVR.服务器等这些设备安装好之后,就需要给它们配上IP,那这个IP地址你了解嘛?今天我们就一起来聊聊什么是内网.公网和NAT地址转 ...

  8. Laravel入门及实践,快速上手ThinkSNS+二次开发

    温馨提示: l 本文纯干货,文字和代码居多,且适合零基础Laravel学习者: l 本文会新建一个名为 blog 的 Laravel 程序,这是一个非常简单的博客. l  欢迎随时关注ThinkSNS ...

  9. 又一个秘密如何让浏览器访问最新的js,css等外部引用

    在引用文件末尾加上一个参数,让浏览器知道这个文件跟上一个文件是不同的,让浏览器去服务器重新加载最新的,例如:<script type="text/javascript" sr ...

  10. BFC 到底是什么?

    MDN 对 BFC 的描述: 块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视化CSS渲染的一部分,是布局过程中生成块级盒子的区域,也是浮动元素与其他元素 ...