题目链接: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. identity server4

    dotnet new -i identityserver4.templates   添加模板

  2. Lock Free (无锁并发)

    CAS( compare and swap) 原子操作,保证了如果需要更新的地址没有被其他进程(线程)改动过,那么它可以安全的写入.而这也是我们对于某个数据或者数据结构加锁要保护的内容,保证读写的一致 ...

  3. Codeforces Round #604

    Beautiful Regional Contest 题意 题解 代码 Beautiful Sequence 题意 题解 代码 一个思路不够清晰的代码 Beautiful Mirrors with q ...

  4. 【基于onenet-edp的文件传输】1、调试上报数据点和端对端透传

    onenet-edp上报数据点和端对端透传 一.前言 edp是onenet用于tcp设备定制的一套协议,能够灵活地实现数据上报和透传: 二.准备工作 1.找到edp页面 进入工作台后,找到多协议接入, ...

  5. go语言浅析二叉树

    Hello,各位小伙伴大家好,我是小栈君,今天给大家带来的分享是关于关于二叉树相关的知识点,并用go语言实现一个二叉树和对二叉树进行遍历. 我们主要针对二叉树的概念,go实战实现二叉树的前序遍历.中序 ...

  6. java知识精要(二)

    java知识精要(一) 集合 Iterable v.s. Iterator 两者都是接口,在Collection继承的是Iterable. Iterable表达了集合具备迭代访问的能力,而Iterat ...

  7. golang(一)

    开篇先来个Go语言的吉祥物-金花鼠Gordon. golang是谷歌2009年发布的开源编程语言,截止目前go的release版本已经到了1.10.go语言的开发人员都是计算机界大神一般的存在: Th ...

  8. win10下jupyter修改默认路径的办法

    查了很多资料,发现都不管用,最后亲测找到一种方法. 实现的方法就是修改快捷方式标签的目标栏,如下图: 后面有一个%USERPROFILE% 将%USERPROFILE%改成你要的路径就可以了 然后应用 ...

  9. 如何删除mysql注释

    Linux命令删除注释 先把库表导出成一个.sql文件,然后使用sed命令删除注释.此种适用于mysql端口不开外网的情况. $ cat create_table.sql create table t ...

  10. iOS 12中获取WiFi的SSID

    开始搞智能家居,wifi获取不到了?? 小插曲 旧方法失效,19-12-15更新,ios13开始需要请求定位信息 SSID全称Service Set IDentifier, 即Wifi网络的公开名称. ...