Output: Standard Output
Time Limit: 2 Seconds
A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, Z, TOT and MADAM are palindromes, but ADAM is not.
Given a sequence S of N capital latin letters. How many ways can one score out a few symbols (maybe 0) that the rest of sequence become a palidrome. Varints that are only different by an order of scoring out should be considered the same.
The input file contains several test cases (less than 15). The first line contains an integer T that indicates how many test cases are to follow.
Each of the T lines contains a sequence S (1≤N≤60). So actually each of these lines is a test case.
For each test case output in a single line an integer – the number of ways.
3
BAOBAB
AAAA
ABA
22
15
5
#include <iostream> #include <string> #include <cstdio> #include <cstring> using namespace std; const int maxn=65; string str; long long dp[maxn][maxn]; int len; void initial() { for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) { if(i==j) dp[i][j]=1; else dp[i][j]=0; } } void input() { cin>>str; len=str.length(); } void solve() { for(int i=2; i<=len; i++) for(int j=0,k=i-1; k<len; j++,k++) { dp[j][k]=dp[j+1][k]+dp[j][k-1]; if(str[j]==str[k]) dp[j][k]+=1; else dp[j][k]-=dp[j+1][k-1]; } printf("%lld\n",dp[0][len-1]); } int main() { int T; scanf("%d",&T); while(T--) { initial(); input(); solve(); } return 0; }
Uva 10617 Again Palindrome (DP+回文串)
原文:http://blog.csdn.net/u012596172/article/details/41080697