POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.
Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
Sample Input
5 Ab3bd
Sample Output
2
题意:给出n,表示接下去给出的字符串长度为n,求最少插入几个字符可以使该字符串变成回文字串。
思路:设原字符串为a,反转该字符串设为b,用字符串长度减去a、b的最长公共子序列即可。
注意:
- 若用%c输入,记得getchar
- 即使利用了最长公共子序列中后,dp数组开[5050][5050]会造成内存超限,这时候需要利用滚动数组进行处理(结合奇偶性进行重复利用),由于我们只需要求出dp[n][n],所以前面部分的空间其实是可以重复利用的,比如说1->2存完之后,我们需要存3了,但是没有必要再去开辟空间,因为前面1所占的空间之后已经不需要再用了,所以我们可以将3存到1的位置,即3->1,以此类推 4->2。dp只需开到[2][5050]即可,即对每一部分i%2。
- 顺带注意区别一下子序列和字串,子序列是不要求满足连续性的,而字串是需要连续的。

1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 char a[5050],b[5050]; 8 int dp[2][5050]; 9 int main() 10 { 11 int n; 12 cin>>n; 13 memset(dp,0,sizeof(dp)); 14 memset(a,'\0',sizeof(a)); 15 memset(b,'\0',sizeof(b)); 16 17 scanf("%s",a); 18 19 int p=0; 20 for(int i=n-1; i>=0; i--) 21 b[p++]=a[i]; 22 23 for(int i=0; i<n; i++) 24 { 25 for(int j=0; j<n; j++) 26 { 27 if(a[i]==b[j]) 28 dp[(i+1)%2][j+1]=dp[i%2][j]+1; 29 else 30 dp[(i+1)%2][j+1]=max(dp[i%2][j+1],dp[(i+1)%2][j]); 31 } 32 } 33 cout<<n-dp[n%2][p]<<endl; 34 return 0; 35 }
分类:
字符串
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」