标签:
Time Limit: 10000/5000 MS (Java/Others)
#include<cstdio> #include<algorithm> #include<cmath> #include<queue> #include<cstring> #include<iostream> using namespace std; const int MOD = 1e9+7; const int SZ = 26; const int N = 101005; int L[N], dp[N]; char str[N]; int pos[N][SZ];//pos[i][j]记录在i位置之后的sz字符的第一个位置 int main() { int T; int i, j, len; int cs = 0; while(T--){ scanf("%s",str+1); len = strlen(str+1); for(i=1; i<=len+1; ++i){ L[i] = len+1; } for(i=0; i<SZ; ++i) pos[len][i] = len+1; for(i=len; i>=1; --i){ for(j=0; j<SZ; ++j) pos[i-1][j] = pos[i][j]; pos[i-1][str[i]-'a'] = i; } L[0] = 0; dp[0] = 1; for(i=0; i<=len; ++i){ for(j=0; j<SZ; ++j){ int nex = pos[i][j]; if(L[i]+1 < L[nex]){ L[nex] = L[i]+1; dp[nex] = dp[i]; }else if(L[i]+1 == L[nex]){ dp[nex] += dp[i]; if(dp[nex] >= MOD) dp[nex] -= MOD; } } } printf("Case #%d:\n%d %d\n",++cs,L[len+1], dp[len+1]); } return 0; }
HDU5262 15年百度之星复赛1005 最强密码(神奇的dp)(好题)
标签:
原文地址:http://blog.csdn.net/kalilili/article/details/46391333