Making Huge Palindromes LightOJ - 1258

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 would be "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

4
bababababa
pqrs
madamimadam
anncbaaababaaa

Sample Output

Case 1: 11
Case 2: 7
Case 3: 11
Case 4: 19

Note

Dataset is huge, use faster I/O methods.

题目大意:给定一个字符串,一次操作可以在这个字符串的右边增加任意一个字符。求操作之后的最短字符串,满足操作结束后的字符串是回文。

思路:遇到求回文串二话不说先将原来的字符串反过来,通过这个题的数据量来看暴力显然是不行滴,所以考虑字符串匹配可以用什么算法来优化,我第一时间想到了KMP(今天下午数据结构老师刚讲了......,虽然之前就会了......废话一大片......),我们可以想到将字符串求反之后得到的串作为模式串去原串当中去匹配,做一遍KMP之后我们可以得到目前模式串匹配的长度,这个长度就是原串中与后缀相匹配的长度,那最终的回文串的长度就是原串的长度+未匹配的长度,具体在代码开头有说明。注:一开始一直以为只有一个字符的字符串不能算是回文串,查了查发现它好像确实是回文串。

/*
原串|____________________|
模式串       |____________________| 
2个串中间重叠的部分就是作为回文串可以相互匹配的部分,
那我们就只需要在原串长度的基础上再加上原串前面那一部分未重叠部分就可以构造出一个最短的回文串了*/ 
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N =1e6+10;
char s1[N],s2[N];
int ne[N];
int main()
{
	int t;
	cin>>t;
	int cnt=0;
	while(t--)
	{
		scanf("%s",s1+1);
		int len=strlen(s1+1);
		memset(ne,0,sizeof ne); 
		for(int i=len;i>=1;i--)
		{
			s2[i]=s1[len-i+1];
		}
		for(int i=2,j=0;i<=len;i++)
		{
			while(j&&s2[i]!=s2[j+1])
			j=ne[j];
			if(s2[i]==s2[j+1])
			j++;
			ne[i]=j;
		}
		int j=0,i=1;
		for(;i<=len;i++)
		{
			while(j&&s1[i]!=s2[j+1])
			j=ne[j];
			if(s1[i]==s2[j+1])
			j++;	
		}
		printf("Case %d: ",++cnt);
		cout<<len*2-j<<endl;
	}
	return 0;
}

做这个题的时候突然想到之前好像也做过题意非常相似的一道题,又把它扒拉了出来。

直接上题目代码吧

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
char s1[30],s2[30];
int dp[30][30];
int main()
{
	cin>>n;
	scanf("%s",s1+1);
	for(int i=1;i<=n;i++)
	s2[n+1-i]=s1[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			if(s1[i]==s2[j])
			dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
		}
		if(dp[n][n]==0)
		cout<<"-1"<<endl;
		else
		cout<<n-dp[n][n]<<endl;
		return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值