求最长回文子序列,将所给字符串反制后,求与原串的最长公共子序列
// File Name: UVa11151.cpp
// Author: Toy
// Created Time: 2013年05月05日 星期日 22时07分03秒
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cctype>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <utility>
#include <bitset>
#define L(x) x << 1
#define R(x) x << 1 | 1
using namespace std;
int Case, a[1000][1000];
string s1, s2;
int LCS ( const string s1, const string s2 ) {
int len = s1.length ( );
a[0][0] = 0;
for ( int i = 1; i <= len; a[i][0] = 0, ++i ) ;
for ( int j = 1; j <= len; a[0][j] = 0, ++j ) ;
for ( int i = 1; i <= len; ++i )
for ( int j = 1; j <= len; ++j ) {
if ( s1[i - 1] == s2[j - 1] ) a[i][j] = a[i - 1][j - 1] + 1;
else a[i][j] = max ( a[i][j - 1], a[i - 1][j] );
}
return a[len][len];
}
int main ( ) {
cin >> Case;
getchar ( );
while ( Case-- ) {
getline ( cin, s1 );
s2.clear ( );
for ( int i = s1.size ( ) - 1; i >= 0; s2 += s1[i], --i ) ;
cout << LCS ( s1, s2 ) << endl;
}
return 0;
}