Linear thinking dp + 60 C ox-off practice match title operating highlights

Operating Highlights

Title Description

There is a move away from the 26 kinds operation, each operation we use a, b, c, d, ..., x, y, z instead of symbols.
Now there is a beef operation sequence of length n, he can now from the inside out certain operations to be combined into a video operation, for example, the operation sequence is abcdabcd, then the operation of the video will have a, b, c, d, ab, ac, ad , etc. (i.e. the operation of the sub-sequence). he wondered length k and the different nature of the operation video how many.
for example, for abab, 2 different length and nature of the results have ab, aa, ba, bb.
Taking into account the answer may be very large, you just need to output at 1e9 + 7 significance die answer it.

Enter a description:

The first line of two integers n, K.
The second line of a string of length n, to ensure the presence of lowercase letters.

Output Description:

It indicates the length of a line is the number of integer k and the different nature of the operation of the video.


Praying number sequence, dp obvious to use, and can apparently launched dp equation:

dp[i][j]=dp[i-1][j-1]+dp[i-1][j];

The problem is that there is repetitive, how to re-do?

You can record the position of the last occurrence of each letter, and this equation is similar prefix, so a final value of the position by subtracting the current letter appears before the line;

dp[i][j] -=dp[ pre[s[i]-‘a’]-1 ][ j-1];

It is noted here from zero length;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100010;
const int M=2000100;
const LL mod=1e9+7;
LL dp[1100][1100];
char s[1100];
int pre[30];
int main(){
	int n,k;
	cin>>n>>k;
	scanf("%s",s+1);
	dp[0][0]=1;
	for(int i=1;i<=n;i++){//位置 
		for(int j=0;j<=i;j++){//长度 
			dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
			if(pre[s[i]-'a']) dp[i][j]-=dp[pre[s[i]-'a']-1][j-1];
			dp[i][j]%=mod;
		}
		pre[s[i]-'a']=i;
	}
	if(k==0){
		cout<<1<<endl;
		return 0;
	}
	if(dp[n][k]<0) dp[n][k]+=mod;
	cout<<dp[n][k]<<endl;
	return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105161258