照抄别人的解法来的,以下是原作地址
http://blog.csdn.net/jeremy1149/article/details/54922312
还是有不理解的地方,写到自己的博客里以后方便查找
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define LL long long
#define INF 0x7fffffff
#define MOD 1000000007
using namespace std;
LL dp[1005];//存储长度为i时最大方案数
int MinSubNum[1005];//存储长度为i时最少分割的段数
int Letter[26];
char Str[1005];
bool Check( int Start, int End )
{
int Length = End-Start+1;
for( int i = Start; i <= End; i++ )
{
if( Letter[Str[i]-'a'] < Length )
return false;
}
return true;
}
int main()
{
int n;
ios::sync_with_stdio(false);
while( cin >> n )
{
int mmax = -1;
cin >> Str+1;
for( int i = 0; i < 26; i++ )
cin >> Letter[i];
memset( dp, 0, sizeof(dp) );
dp[0] = 1;
MinSubNum[0] = 0;
for( int i = 1; i <= n; i++ )
{
MinSubNum[i] = INF;
for( int j = 1; j <= i; j++ )
{
if( Check(i-j+1, i) )
{
dp[i] = (dp[i]+dp[i-j]) % MOD;
MinSubNum[i] = min( MinSubNum[i], MinSubNum[i-j]+1 );
mmax = max(mmax, j);
}
else
break;
}
}
cout << dp[n] << endl;
cout << mmax << endl;
cout << MinSubNum[n] << endl;
}
return 0;
}