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 3ac

Output

8

Input

0 2aaba

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, m≤106, k≤26

思路:全在代码里了

#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\n",dp[N+len]);
    return 0;
}

原文地址:https://www.cnblogs.com/caijiaming/p/12008258.html

时间: 2024-10-17 16:03:58

E - Intellectual Inquiry的相关文章

CODEFORCES 645E Intellectual Inquiry

// CODEFORCES 645E Intellectual Inquiry http://codeforces.com/problemset/problem/645/E 字母表只有前面 k 个小写字母.给出一个字符串 s,你可以往 s 后添加 n 个字母,使得,总的字符串的不同子序列最多. Input n k s 1 ≤ n ≤ 1,000,000 1 ≤ k ≤ 26 1 ≤ |s| ≤ 1,000,000 Output 输出答案模 1,000,000,007. Solution 看了题解

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

E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同子序列就是从0号点出发能走出多少条 不同点的路径. dp[ i ]表示是到 i 这个点的不同路径数, 构成的图显然是个DAG,把这个dp出来就好啦.最后补 n个就是贪贪心. 网上的另外两种方法. dp[ i ] 表示[1, i] 的字符串有多少不同子序列. dp[ i ] = dp[i - 1] *

16.05.25-16.06.10 题集

继2016.05.24续: codeforces 651B. Beautiful Paintings-简单 http://codeforces.com/problemset/problem/651/B 大意:给出一个序列,求解其任意排列中满足ai?+?1?>?ai 的元素个数最大和. 分析:理想情况下,无重复元素的0从小到大的排列,满足条件的元素个数最多,是n-1. 非理想情况下还有重复元素,只要不断提取出重复的这些元素归到另一集合再这样讨论即可. #include <iostream>

RE写作Issue问题题库分析与提纲

RE写作Issue问题题库分析与提纲 GRE写作Issue问题题库分析与提纲 第一类 社会 2. "Competition is ultimately more beneficial than detrimental to society." 归根结底,竞争对于社会是利多弊少. Generally speaking, competition contributes to progress in society. 1.        Generally speaking, competi

uva 10526 - Intellectual Property(后缀数组)

题目链接:uva 10526 - Intellectual Property 题目大意:给定两个文本,问说下面一个文本中在哪些位置上抄袭了上面个一个文本的,输出n个抄袭位置(不足n个情况全部输出),按照长度优先输出,长度相同的输出位置靠前的. 注意:空格,回车都算一个字符:一段字符只能是抄袭上面的一部分,比如上:NSB*SB 下:NSB 答案:NSB. 解题思路:将两个文本连接在一起,中间用没有出现的字符分割,然后处理处后缀数组,根据height数组的性质,求出哪些位置匹配的长度不为0(注意匹配

hdu1047 Integer Inquiry

/* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15874 Accepted Submission(s): 4079 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He exte

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he

杭电1407--Integer Inquiry

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15255    Accepted Submission(s): 3917 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex