uva 10617 Again Palindromes(DP)

来源:互联网 发布:mastercamx9编程技巧 编辑:程序博客网 时间:2024/05/29 03:41

Problem I

Again Palindromes

Input: Standard Input

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, butADAM 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.

Input

The input file contains several test cases (less than 15). The first line contains an integerT 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.

Output

For each test case output in a single line an integer – the number of ways.

Sample Input Output for Sample Input

3

BAOBAB

AAAA

ABA

22

15

5


#include <iostream>#include <cstdio>#include <string>using namespace std;const int maxn = 100;long long dp[maxn][maxn];string str;void initial(){for(int i = 0;i < maxn;i++){for(int j = 0;j < maxn;j++){dp[i][j] = 0;}}str.clear();}void DP(int i , int j){if(i == j){dp[i][j] = 1;return;}if(i > j){return;}if(dp[i+1][j-1] == 0){DP(i+1 , j-1);}if(dp[i][j-1] == 0){DP(i , j-1);}if(dp[i+1][j] == 0){DP(i+1 , j);}dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];if(str[i] == str[j]){dp[i][j] += dp[i+1][j-1]+1;}}void computing(){cin >> str;int len = str.length();DP(0 , len-1);cout << dp[0][len-1] << endl;}int main(){int t;cin >> t;while(t--){initial();computing();}return 0;}


0 0
原创粉丝点击