Light OJ:1033 Generating Palindromes(LCS+回文字符串)

1033 - Generating Palindromes
Time Limit: 2 second(s)Memory Limit: 32 MB

By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.

Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length n, no more than (n - 1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome "abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are only allowed to insert characters at any position of the string.

Input

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

Each case contains a string of lowercase letters denoting the string for which we want to generate a palindrome. You may safely assume that the length of the string will be positive and no more than 100.

Output

For each case, print the case number and the minimum number of characters required to make string to a palindrome.

Sample Input

Output for Sample Input

6

abcd

aaaa

abc

aab

abababaabababa

pqrsabcdpqrs

Case 1: 3

Case 2: 0

Case 3: 2

Case 4: 1

Case 5: 0

Case 6: 9

 


PROBLEM SETTER: MD. KAMRUZZAMAN

SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)


题目大意:给你一个字符串,问你将其变成一个回文字符串最少需要添加多少个字符。

解题思路:答案=所给字符串的长度-所给字符串与它的反转字符串的LCS长度。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; 
int main()
{
	int t;
	scanf("%d",&t);
	char s1[110];
	char s2[110];
	int dp[110][110];
	int kcase=1;
	while(t--)
	{
		memset(dp,0,sizeof(dp));
		scanf("%s",s1);
		int size1=strlen(s1);
		int size2=0;
		for(int i=size1-1;i>=0;i--)//反转字符串 
		{
			s2[size2]=s1[i];
			size2++;
		}
		s2[size2]='\0';//别忘了这个 
		int ans=0;
		for(int i=1;i<=size1;i++)//求LCS长度 
		{
			for(int j=1;j<=size2;j++)
			{
				if(s1[i-1]==s2[j-1])
				{
					dp[i][j]=dp[i-1][j-1]+1;
				}
				else
				{
					dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
				}
				ans=max(ans,dp[i][j]);
			} 
		}
		printf("Case %d: %d\n",kcase++,size1-ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Crypto++库中的RSA类来导入RSA公钥/私钥的字符串表示。下面是一个简单的示例代码: ```c++ #include <iostream> #include <string> #include <cryptopp/rsa.h> #include <cryptopp/base64.h> using namespace CryptoPP; int main() { // RSA keys in PEM format std::string publicKeyPEM = "-----BEGIN PUBLIC KEY-----\n" "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvRJ9YmJ9A0seV0bSf+I6\n" "n5i6dF6qXl5Hf8b0KJvJ7eN0yTnE4t5rKQYdM+DLg1yXz+4kWv8xgJxJW9Q2gY7L\n" "wNqROnFJZrOq6zZVrWXfR4gWVv8grl2PZl6vQfGbdzPd2BhLW1QOyPZfGy1CCoMg\n" "FQd6ZqBx1UJHsUSx/PnV0i35S8zO7Zq9UxOYbRr9SiCsTJ0FNLwPz7CkC5u3VJ5D\n" "z5z2K7U0fGkO+8fOc5Qv2JUqKd6sN2oT+18+eVf/lvtlQ4U3aXyKZB7u0k8T3xKx\n" "3J2r3x4y+o9B3fz1aV0R/5N5Iz2Kt6gJyJmJyNfh0T+D5JLZL8o+Oq8rD8/8mLgV\n" "XQIDAQAB\n" "-----END PUBLIC KEY-----\n"; std::string privateKeyPEM = "-----BEGIN PRIVATE KEY-----\n" "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCuN1Qa1iRQ9ZvZ\n" "iFfZjzKfKXk9X5Rq8Dp4CCVt1LJbXG8oU1Kd7UeJb+JyQrK+D1hViDUKvC4pDvZL\n" "7Wv8wzZ1mJN+3M+Vc7dqSaTr0z+U4d3ZKQKj1UjF9/3Ud/HXw6Ez84Zx5fX1U7P\n" "3u9Qo+6g0cva0tYcZ8E4m8osjzOZn3n5Z+qo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fXf4aFh2U+ZQzXG7yUa5KdNf7xvP8QXa6omQY0\n" "sQO2e1+Vc1r7h2e0HNY0U6E0g+7l7MBZv5lZP6XG7HdY5k8n1UdbvR9uGkECgYEA\n" "xAIlWc+0qLQUjY7t2sqHjzJ3nV2GxMvW0G9fZgD6loQWtKX2Y7r/x6WLZ9Smc5ZQ\n" "4QJ1l0sT1q2YKqCwR1u+2J9lX8Pp17FZK8Xvq8kPT9lQK9o3+JvUqPvX6xrmJ2mK\n" "R7uxvYv+ZzL2R9gkt/6g1QH0aRZzZj0+6+J5PfWen9sCgYEA7fQxJYJdQdUgJvZL\n" "gH2Y1o5LJ5W1N3v9zQJLJGJ1Tt7TfB3gXzOzL/hsW68Uzr+1JqP6e9LTk5OuWVQG\n" "yR7zFv3p6K5tqT2Z0OJdS4xVw/2zB5YzJiK6I6b5aMZr9o2ZtW/0A1m5RcU6bJSo\n" "Vz5Q5O1ZoD7JdS3d+6iNq+f8dQ==\n" "-----END PRIVATE KEY-----\n"; // Base64 decode the PEM strings std::string publicKeyStr, privateKeyStr; StringSource(publicKeyPEM, true, new Base64Decoder(new StringSink(publicKeyStr))); StringSource(privateKeyPEM, true, new Base64Decoder(new StringSink(privateKeyStr))); // Load the keys from the decoded strings RSA::PublicKey publicKey; RSA::PrivateKey privateKey; StringSource(publicKeyStr, true, new PEMDecoder(new StringSink(publicKey))); StringSource(privateKeyStr, true, new PEMDecoder(new StringSink(privateKey))); return 0; } ``` 在这个示例中,我们将PEM格式的RSA公钥/私钥字符串进行Base64解码,然后使用Crypto++库中的PEMDecoder类将它们加载到RSA::PublicKey和RSA::PrivateKey对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值