E. Intellectual Inquiry

题目连接:

http://www.codeforces.com/contest/655/problem/E

Description

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.

Sample Input

1 3

ac

Sample Output

8

Hint

题意

现在给你n,k和长度为m的串

你需要构造一个长度为n+m的串,使得其中不同的子序列最多,输出数量。

题解:

构造题

假设我们构造出来了,我们怎么去统计呢?

dp[i]表示以i结尾的不同子序列数量,显然dp[i]=sigma(dp[j])+1,i!=j

观察可以知道其实dp[i]=前面所有不同子串数量-以i结尾的子串

统计知道了,我们怎么去构造呢?

贪心去构造就好了,我们可以简单的发现前面所有的不同的子串数量这个是相同的,我们取最少的以i结尾的那个字母就好了

这个显然就是最前面的那个字符,取最早出现的那个字符就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+7;
const int mod = 1e9+7;
string s;
int n,k,m,last[1005],dp[1005];
int main()
{
cin>>n>>k>>s;m=s.size();
for(int i=0;i<s.size();i++)
{
int x = s[i];
for(int j='a';j<'a'+k;j++)
{
if(x==j)continue;
dp[x]=(dp[x]+dp[j])%mod;
}
dp[x]=(dp[x]+1)%mod;
last[x]=i+1;
}
for(int i=1;i<=n;i++)
{
int x = 'a';
for(int j='a';j<'a'+k;j++)
if(last[j]<last[x])x=j;
for(int j='a';j<'a'+k;j++)
{
if(j==x)continue;
dp[x]=(dp[x]+dp[j])%mod;
}
dp[x]=(dp[x]+1)%mod;
last[x]=i+m;
}
long long ans = 0;
for(int i='a';i<'a'+k;i++)ans=(ans+dp[i])%mod;
cout<<(ans+1)%mod<<endl;
}

最新文章

  1. Spring配置文件详解
  2. centos 安装maven
  3. JQ实现右下角scrollTop()事件
  4. node 的express 如何接受以一个网站的url作为参数的路由
  5. Python的平凡之路(7)
  6. Scala 深入浅出实战经典 第46讲: ClassTag 、Manifest、ClasMainifest TagType实战
  7. 面试题中遇到的SQL题目
  8. Win软件私家珍藏-常用软件工具使用总结
  9. 《c程序设计语言》读书笔记--每行一个单词打印输入的字符,除去空符
  10. 反射实体自动生成EasyUi DataGrid模板 第二版--附项目源码
  11. C++朝花夕拾【更新】
  12. HDOJ 1197 Specialized Four-Digit Numbers
  13. 单例模式(Singleton)详解——转载
  14. Python自学笔记-logging模块详解
  15. windows服务管理操作
  16. mapdb的一些性能测试
  17. Web前端文件上传进度的显示
  18. frameset基础了解
  19. Dockerfile的alpine时区设置
  20. dom4j 通过 org.dom4j.XPath 设置命名空间来支持 带namespace 的 xpath

热门文章

  1. C++ STL标准入门
  2. JAVA 之 Tomcat知识框架【转】
  3. 微信access_token和refresh_token保存于redis
  4. oracle日期格式转换 to_date()
  5. 系统日志查看logrotate 工具
  6. WireShark出现The NPF driver isn&#39;t running的问题
  7. Pylint在项目中的使用
  8. [Spring Data JPA问题]Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException
  9. WP主题模板制作修改教程
  10. 【PAT】1008. 数组元素循环右移问题 (20)