标签:
动态规划
#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