Codeforces 645E (构造 DP)

来源:互联网 发布:gitlab for mac客户端 编辑:程序博客网 时间:2024/05/19 05:02
E. Intellectual Inquiry
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first k English letters.

Each morning, Bessie travels to school along a sidewalk consisting of m + n tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first m sidewalk tiles with one of the first k lowercase English letters, spelling out a string t. Mr. Moozing, impressed by Bessie's extensive knowledge of farm animals, plans to let her finish labeling the last n tiles of the sidewalk by herself.

Consider the resulting string s (|s| = m + n) consisting of letters labeled on tiles in order from home to school. For any sequence of indices p1 < p2 < ... < pq we can define subsequence of the string s as string sp1sp2... spq. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn't even finished learning the alphabet, she needs your help!

Note that empty subsequence also counts.

Input

The first line of the input contains two integers n and k (0 ≤ n ≤ 1 000 0001 ≤ k ≤ 26).

The second line contains a string t (|t| = m, 1 ≤ m ≤ 1 000 000) consisting of only first k lowercase English letters.

Output

Determine the maximum number of distinct subsequences Bessie can form after labeling the last n sidewalk tiles each with one of the first klowercase English letters. Since this number can be rather large, you should print it modulo 109 + 7.

Please note, that you are not asked to maximize the remainder modulo 109 + 7! The goal is to maximize the initial value and then print the remainder.

Examples
input
1 3ac
output
8
input
0 2aaba
output
10

设dp[i]表示以字母i结尾的不同子序列个数,dp[j] = sigma (dp[i],i!=j)+1;

构造可以在末尾加上一个上次出现的最前面的字母;

#include <bits/stdc++.h>using namespace std;#define mod 1000000007#define  maxn 2111111char s[maxn];int pre[33];//字母的前一个位置long long dp[33]; //以i结尾的子序列int main () {    int n, m, k;    scanf ("%d%d", &n, &k);    scanf ("%s", s);    m = strlen (s);    memset (pre, -1, sizeof pre);    for (int i = 0; i < m; i++) {        pre[s[i]-'a'] = i;    }    for (int i = m; i < n+m; i++) {//构造        int pos = maxn;        char ch;        for (int j = 0; j < k; j++) {            if (pos >  pre[j])                pos = pre[j], ch = 'a'+j;        }        s[i] = ch;        pre[ch-'a'] = i;    }    //s[n+m] = '\n'; cout << s << endl;    memset (dp, 0, sizeof dp);    dp[s[0]-'a'] = 1;    for (int i = 1; i < n+m; i++) {        dp[s[i]-'a'] += 1;        for (int j = 0; j < k; j++) {            if (s[i]-'a' == j)                continue;            dp[s[i]-'a'] += dp[j];            dp[s[i]-'a'] %= mod;        }    }    long long ans = 0;    for (int i = 0; i < k; i++) {        ans += dp[i];        ans %= mod;    }    cout << (ans+1)%mod << endl;    return 0;}


0 0