码迷,mamicode.com
首页 > 其他好文 > 详细

编程之美-资格赛-回文字符序列

时间:2015-04-19 22:50:14      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

动态规划

#include <iostream>
#include <iostream>
#include <map>
#include <cstring>

using namespace std;

char a[1005];
const int INF = 100007;
int num[1005];
int dp[1005][1005];

int main(){
	int n;
	cin >> n;
	for(int cases = 1; cases <= n; ++cases){
        cin >> a;
        int len = strlen(a);
        map<char,int> ma;
        for(int i = 0; i < len; ++i){
            if(ma.find(a[i]) == ma.end()){
                ma[a[i]] = i;
                num[i] = ma[a[i]];
            }else{
                num[i] = ma[a[i]];
                ma[a[i]] = i;
            }
        }
        memset(dp,0,sizeof(dp));
        for(int distance = 0; distance < len; ++distance){
            for(int source = 0; source < len; ++source){
                int end = distance + source;
                if(end >= len) break;
                if(distance == 0)
                    dp[source][end] = 1;
                else{
                    int realEnd = end;
                    while(num[end] >= source && num[end] != end){
                        dp[source][realEnd] += dp[num[end]+1][realEnd-1] + 1;
                        dp[source][realEnd] %= INF;
                        end = num[end];
                    }
                    dp[source][realEnd] += dp[source][realEnd-1] + 1;
                }
            }
        }
         cout << "Case #" << cases << ": " << dp[0][len-1] << endl;
	}
	return 0;
}


编程之美-资格赛-回文字符序列

标签:

原文地址:http://blog.csdn.net/lane_l/article/details/45133255

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!