问题描述:
1025 Keep at Most 100 Characters (35 分)
Given a string which contains only lower case letters, how many different non-empty strings you can get if you can keep AT MOST 100 characters in the original order? (Note: The string obtained is a "sub-sequence" of the original string.)
Input Specification:
Each input file contains one test case, which is only one line containing the string whose length is no larger than 1000.
Output Specification:
Output the number of different non-empty strings if you can only keep at most 100 characters. Since the answer might be super large, you only need to output the answer modulo 1000000007.
Sample Input:
aabac
Sample Output:
19
Hint:
The strings are:
a
, b
, c
, aa
, ab
, ac
, ba
, bc
, aab
, aaa
, aac
, aba
, abc
, bac
, aaba
, aabc
, abac
, aaac
, and aabac
.
这一题很明显是一道动态规划的题目。设置一个二维数组v[s][100],其中的v[i][j]记录下以第i个字符为结尾的长度为j的子串总数。原始递推关系是:v[i][j]=v[0][j-1]+v[1][j-1]+...+v[i-1][j-1],初值条件是:v[i][1]=1,v[i][i]=1;再剔除重复的子串,即对于下标k1和k2(k1<k2),若在原始串中有,s[k1]=s[k2],则在v[i][j]的递推式中,应该只有v[k2][j-1],而没有v[k1][j-1]。
注意到本题的数值是取模数的,较大的数取模数之后可能会变小。所以,在使用数组v[i][j]时,1.不能够使用比较符号;2.相加时要取模数;3.相减时要加模数再取模数。
(p.s.博主也想发视频啊,可是OBS实在是太难用了,等博主学会了一定发。。。)
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007u
int main()
{
// freopen("data.txt","r",stdin);
char a[1001];
scanf("%s",a);
int asl=strlen(a)+1;
long long op=0;
int masl=min(asl,101);
vector<long long> v(asl*masl,1);
for(int i=1;i<masl;i++)
{
vector<long long> maxre(26,0);
maxre[a[i-1]-'a']=1;
for(int j=i+1;j<asl;j++)
{
v[i*asl+j]=(v[i*asl+j-1]+v[(i-1)*asl+j-1]-maxre[a[j-1]-'a']+mod)%mod;
maxre[a[j-1]-'a']=v[(i-1)*asl+j-1];
}
}
for(int i=1;i<masl;i++)
op=(op+v[(i+1)*asl-1])%mod;
printf("%lld",op);
return 0;
}