站外题求调
  • 板块题目总版
  • 楼主LiaoYF1
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/8/18 09:37
  • 上次更新2023/10/27 14:50:20
查看原帖
站外题求调
633466
LiaoYF1楼主2022/8/18 09:37

好像是leetcode上的题,不知道洛谷有没有。

给定一个字符串s,找到其中最长的回文子序列,并返回该序列的长度。

输入样例1: bbbab

输出样例1: 4

输入样例2: cbbd

输出样例2: 2

样例都过了,但是AC3个WA2个

#include<iostream>
#include<string>
using namespace std;
string s;
int f[1005][1005];
int main(){
	cin>>s;
	s=" "+s;
	int len=s.length()-1;
	for(int i=len;i>=1;i--){
		for(int j=i+1;j<=len;j++){
			if(s[i]==s[j]){
				f[i][j]=f[i+1][j-1]+2;
			}else{
				f[i][j]=max(f[i+1][j],f[i][j-1]);
			}
		}
	}
	cout<<f[1][len];
	return 0;
}
2022/8/18 09:37
加载中...