poj 1159 Palindrome NYOJ 37 迴文字串(DP)
阿新 • • 發佈:2018-12-27
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXM=1001;
short int f[MAXM][MAXM];
char s[1001];
int LCS()
{
scanf("%s",s);
memset(f,0,sizeof(f));
int len=strlen(s);
for(int i=0;i<len;++i)
{
for(int j=i;j>=0;--j)
{
if (s[i]==s[j])
{
f[i+1][j+1]=f[i][j+2];
}
else
{
f[i+1][j+1]=min(f[i][j+1],f[i+1][j+2])+1;
}
}
}
return f[len][1];
}
int main()
{
int n;
cin>>n;
while(n--)
{
printf("%d\n",LCS());
}
return 0;
}
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXM=1001;
short int f[MAXM][MAXM];
char s[1001];
int LCS()
{
scanf("%s",s);
memset(f,0,sizeof(f));
int len=strlen(s);
for(int i=0;i<len;++i)
{
for(int j=i;j>=0;--j)
{
if
{
f[i+1][j+1]=f[i][j+2];
}
else
{
f[i+1][j+1]=min(f[i][j+1],f[i+1][j+2])+1;
}
}
}
return f[len][1];
}
int main()
{
int n;
cin>>n;
while(n--)
{
printf("%d\n",LCS());
}
return
}