Making Huge Palindromes LightOJ - 1258
题目链接:LightOJ - 1258
| 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的更多相关文章
- Generating Palindromes LightOJ - 1033
Generating Palindromes LightOJ - 1033 题意:添加最少的字符使得给出的字符串成为回文串.输出添加的字符数. 方法:常规区间dp.ans[i][j]表示使得ans[i ...
- lightOJ 1258 Making Huge Palindromes(KMP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1258 就是求逆串和原串的匹配长度 答案就是原串长度的2倍减去匹配长度即可 第一次我将原 ...
- LightOJ 1258 Making Huge Palindromes(KMP)
题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...
- LightOJ 1258 Making Huge Palindromes (Manacher)
题意:给定上一个串,让你在后面添加一些字符,使得这个串成为一个回文串. 析:先用manacher算法进行处理如果发现有字符匹配超过最长的了,结束匹配,答案就是该字符前面那个长度加上该串原来的长度. 代 ...
- D - 楼下水题(kmp+Manacher)
D - 楼下水题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Statu ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- Codeforces Round #360 (Div. 2) B. Lovely Palindromes 水题
B. Lovely Palindromes 题目连接: http://www.codeforces.com/contest/688/problem/B Description Pari has a f ...
- LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)
http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS ...
- cf688B-Lovely Palindromes
http://codeforces.com/problemset/problem/688/B B. Lovely Palindromes time limit per test 1 second me ...
随机推荐
- vue中ref在input中详解
当我们在项目中遇见文本输入框的时候,获取时刻输入框中的值 1.v-model <template> <input type="text" v-model=&quo ...
- vps建站施工预告
作为一个小白,最近几天自己用vps搭了个站点,用来发发博客,偶尔还可以去外面看看.后面几章就来记一下过程吧! 结构极为简单,建站用的WordPress,目前也就只有最基础的发文章功能.不过由于习惯了m ...
- php的json_encode第二个参数学习及应用
php5.4以上: json_encode($data, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); p ...
- Python yield与实现(源码分析 转)
转自:https://www.cnblogs.com/coder2012/p/4990834.html
- spring cloud各个版本之间的区别
最近公司在使用spring cloud进行开发,对于spring cloud版本号一直有疑惑. 那个版本在前?那个版本在后? 那个版本是最新的? 一.常见版本号说明 举个瓜:2.0.3 RELEASE ...
- ASP.Net Jquery 随机验证码 文本框判断
// 登陆验证 $(function () { var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'a', 'B' ...
- Django:实现读写分离
库的配置 1.读写分离 settings配置 #settings.py 配置库信息,生成2个库 DATABASES = { 'default': { 'ENGINE': 'django.db.back ...
- JQuery+Bootstrap 自定义全屏Loading插件
/** * 自定义Loading插件 * @param {Object} config * { * content[加载显示文本], * time[自动关闭等待时间(ms)] * } * @param ...
- Java项目部分总结
一.数据库sql操作: 1.三表查询的时候,最后的条件由于当前字段必须判断是属于哪个表,所以需要注明根据哪个表中的字段进行判断: 并且再在后面加上limit的时候,需要注意先进行添加,避免系统不能识别 ...
- EF执行存储过程(转载)
https://blog.csdn.net/xiaouncle/article/details/82914255 相关文章: https://www.cnblogs.com/Coder-ru/arch ...