Codeforces 645E (构造 DP)

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 3
ac
output
8
input
0 2
aaba
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 2111111

char 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值