E - Intellectual Inquiry

Posted caijiaming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E - Intellectual Inquiry相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/problemset/problem/655/E

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 000, 1 ≤ 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 k lowercase 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

Note

In the first sample, the optimal labeling gives 8 different subsequences: "" (the empty string), "a", "c", "b", "ac", "ab", "cb", and "acb".

技术图片

In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: "" (the empty string), "a", "b", "aa", "ab", "ba", "aaa", "aab", "aba", and "aaba". Note that some strings, including "aa", can be obtained with multiple sequences of tiles, but are only counted once.

题目大意:

给一个长为 m的字符串 S ,你需要用第 1 到 k 个小写字母构造一个长为 n 的字符串 S,使得 S+S本质不同的子串个数最多

输出子串个数 (mod109+7)

n, m106, k26

思路:全在代码里了 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<stack>
using namespace std;
typedef long long LL;
#define lson rt<<1
#define rson rt<<1|1
const int maxn=2e6+5;
const int maxm=1e3+5;
const int INF=1e9+7;
const int mod=1e9+7;
/**
定义dp[i]表示前i位所构成的子串个数
试想转化为01背包问题
在dp[i-1]的情况下,加上第i位 和 不加第i位  所以dp[i]=dp[i-1]*2
当然 这是不考虑重复的情况下,那么考虑重复呢?  怎么去掉重复的子序列
假设第i位的字符为a[i] 假设上一个出现a[i]的位置是last,那么仔细想想
是不是重复算的就是dp[last-1]个呢? 我们在dp[last-1]的基础上加上一个s[i] 就完全重复了
知道了这个 还有一个贪心的思想。这个大家肯定都会,肯定优先last小的
*/
priority_queue<int,vector<int>,greater<int> >q;
LL dp[maxn];
LL last[30];
int main()
{
    LL N,K;scanf("%lld%lld",&N,&K);
    char s[maxn];scanf("%s",s+1);
    int len=strlen(s+1);
    dp[0]=1;
    for(int i=1;i<=len;i++)
    {
        int pre=last[s[i]-a]-1;
        dp[i]=2*dp[i-1]%mod;
        if(pre!=-1) //出现过
            dp[i]=(dp[i]-dp[pre]+mod)%mod;
        last[s[i]-a]=i;
    }
    for(int i=0;i<K;i++) q.push(last[i]);
    for(int i=len+1;i<=N+len;i++)
    {
        int pre=q.top()-1;q.pop();
        dp[i]=2*dp[i-1]%mod;
        if(pre!=-1) //出现过
            dp[i]=(dp[i]-dp[pre]+mod)%mod;
        q.push(i);
    }
    printf("%lld
",dp[N+len]);
    return 0;
}

 

以上是关于E - Intellectual Inquiry的主要内容,如果未能解决你的问题,请参考以下文章

CODEFORCES 645E Intellectual Inquiry

CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp

CodeForces 645E Intellectual Inquiry

UVA 10526 - Intellectual Property (后缀数组)

Guideline 5.2.1 - Legal - Intellectual Property 解决方案

02:Integer Inquiry