#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string s,t;
vector<vector<int> > dp;
int rec(int i,int j){
if(i==s.size()||j==s.size())
return 0;
if(dp[i][j]>-1)
return dp[i][j];
if(s[i]==t[j])
return 1+rec(i+1,j+1);
dp[i][j]=rec(i,j+1);
return dp[i][j]=max(dp[i][j],rec(i+1,j));
}
int main(){
int tst;
cin>>tst;
// ws(cin);
cin.ignore();
for(;tst--;){
getline(cin,s);
t=s;
reverse(t.begin(),t.end());
dp.assign(s.size(),vector<int>(s.size(),-1));
cout<<rec(0,0)<<endl;
}
}
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string s,t;
vector<vector<int> > dp;
int rec(int i,int j){
if(i==s.size()||j==s.size())
return 0;
if(dp[i][j]>-1)
return dp[i][j];
if(s[i]==t[j])
return 1+rec(i+1,j+1);
dp[i][j]=rec(i,j+1);
return dp[i][j]=max(dp[i][j],rec(i+1,j));
}
int main(){
int tst;
cin>>tst;
// ws(cin);
cin.ignore();
for(;tst--;){
getline(cin,s);
t=s;
reverse(t.begin(),t.end());
dp.assign(s.size(),vector<int>(s.size(),-1));
cout<<rec(0,0)<<endl;
}
}
Comments
Post a Comment