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 ...
随机推荐
- pytest_05_fixture之conftest.py
前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然 ...
- react-router的BrowserHistory 和 HashHistory 的区别,如何解决使用BrowserHistory 引起的访问路径问题
一,使用createBrowserHistory 和 createHashHistory 的 区别体现 1. 使用createBrowserHistory () // 使用createBrowserH ...
- 全栈项目|小书架|服务器端-NodeJS+Koa2 实现搜索功能
搜索功能会包含:热搜.搜索列表. 热搜功能在电商的搜索中经常看到,热搜数据的来源有两种 用户真实的搜索数据,根据算法进行排序 人为推送的数据 想想微博热搜是可以买的就知道热搜功能多么重要了. 我采用第 ...
- VMware 网络介绍
3.1 网卡介绍 如图所示,你的机器有两块网卡,一个是有线,一个是无线. 装完VM之后,会出现两块虚拟网卡,如图 VM有四种连接方式,我们着重介绍前三种 3.2 桥接 选择桥接模式,说明V ...
- .net core 读取、修改配置文件appsettings.json
.net core 设置读取JSON配置文件 appsettings.json Startup.cs 中 public class Startup { public Startup(IHostingE ...
- java基础 Math
package cn.mantishell.day08.demo04; /** * java.util.Math类是数学相关的工具类,里面提供类大量的静态方法,完成与数学运算相关的操作 * * pub ...
- 【转载】sqlserver中小数类型float和deciaml类型比较
在sqlserver数据库中,float类型和double类型都可以用来表示小数类型,float类型是一种近似数值的小数类型,而decimal类型则是精确数值的小数类型.如果需要在sqlserver数 ...
- Java 之 Session 包含验证码登录案例
需求: 1. 访问带有验证码的登录页面login.jsp 2. 用户输入用户名,密码以及验证码. 如果用户名和密码输入有误,跳转登录页面,提示:用户名或密码错误 如果验证码输入有误,跳转登录页面, ...
- js设置页面全屏
html代码 <!-- 全屏按钮 --> <img id="alarm-fullscreen-toggler" src="/public/index/i ...
- CentOS8-在hyper-V安装选项
安装选项select Server with a GUI.后重启卡在黑屏无法启动. 后改选: Select software to be installed. Choose the Workstat ...