题目链接

如果不考虑重复的元素, 那么我们可以很容易的发现, 长度为n的字符串它的子串数量是 $ 2^n $ 。

我们设每个到位置i, 答案的数量为f[i]。

然后我们考虑重复的, 我们发现, 每加入一个字符c, 记它出现的上一个位置为last[c], 那么last[c]之前的字符和last[c]产生的字符串与 last[c]之前的字符和c产生的字符串就会发生重复。 所以我们减掉f[last[c]-1]。

对于长度大于m, 我们要新加入的字符, 肯定是选一个last[c]最小的来加, 这样产生的重复就越小。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 2e6+5;
ll f[maxn];
int last[28];
char s[maxn/2];
int main()
{
int n, m, k;
cin>>m>>k;
scanf("%s", s+1);
n = strlen(s+1);
for(int i = 0; i<k; i++)
last[i] = 0;
f[0] = 1;
for(int i = 1; i<=n+m; i++) {
f[i] = f[i-1]*2%mod;
int c = -1, pos = i;
if(i<=n) {
c = s[i]-'a';
pos = last[c];
} else {
for(int j = 0; j<k; j++) {
if(last[j]<pos) {
pos = last[j];
c = j;
}
}
}
f[i] = (f[i]-f[pos-1]+mod)%mod;
last[c] = i;
}
cout<<f[m+n]<<endl;
return 0;
}

codeforces 645E . Intellectual Inquiry的更多相关文章

  1. Codeforces 645E. Intellectual Inquiry(DP,贪心)

    Codeforces 645E. Intellectual Inquiry 题意:给定一串字符,由前k个小写拉丁字母组成,要求在该字符串后面补上n个字符(也从前k个小写拉丁字母里面选),使得最后得到的 ...

  2. CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp

    E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...

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

    E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...

  4. codeforces 645 E. Intellectual Inquiry

    一个字符串,由前k个字母组成,长度为m + n,其中前m个字符已经确定,后面n个由你自由选择, 使得这个串的不同的子序列的个数最多,空串也算一个子序列. 1 <= m <= 10^6,0 ...

  5. CF CROC 2016 Intellectual Inquiry

    题目链接:http://codeforces.com/contest/655/problem/E 大意是Bessie只会英文字母表中的前k种字母,现在有一个长度为m+n的字母序列,Bessie已经知道 ...

  6. 「CF645E」 Intellectual Inquiry

    题目链接 CF645E 题意 有一个长为\(n\)的由小写字母组成的字符串,需要用小写字母再填\(m\)位,使最后的字符串中本质不同的子串数量尽量多,答案对\(10^9+7\)取模. 本题数据:\(n ...

  7. Codeforces Gym 100269A Arrangement of Contest 水题

    Problem A. Arrangement of Contest 题目连接: http://codeforces.com/gym/100269/attachments Description Lit ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. Git 和 SVN之间的五个基本区别

    GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等.如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征.所以,这篇文章的主要目的就是 ...

  2. NHibernate各种数据库连接参数文件配置方法说明

    //NHibernate各种数据库连接参数文件配置方法说明 //配置文件Config/Hibernate.cfg.xml内容如下所示: <?xml version="1.0" ...

  3. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  4. React 从0开始 消息传递

    React笔记 React 数据决定DOM 以往的做法是通过JS去操作DOM 将数据填充 JSX Jsx javascript xml HTML的结构组装到js中 jsx使用style的时候 不能直接 ...

  5. document.ready()的用法

    1.Jquery是优秀的Javascrīpt框架,$是jquery库的申明,它很不稳定(我就常遇上),换一种稳定的写法jQuery.noConflict(); jQuery(document).rea ...

  6. 全局键盘钩子(WH_KEYBOARD)

    为了显示效果,在钩子的DLL中我们会获取挂钩函数的窗体句柄,这里的主程序窗体名为"TestMain",通过FindWindow查找. KeyBoardHook.dll代码 libr ...

  7. docker 运行挂载磁盘

    docker:/data# mkdir /awp docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...

  8. 我使用过的Linux命令之date - 显示、修改系统日期时间

    原文地址:http://www.cnblogs.com/diyunpeng/archive/2011/11/20/2256538.html 用途说明 ate命令可以用来显示和修改系统日期时间,注意不是 ...

  9. #include <fstream>

    1 fstream 2 ifstream 3 ofstream 4 seekg 5 seekp 6 tellg 7 tellp 1 fstream 打开输入输出文件流 #include <ios ...

  10. 在magento中使用正则式

    $sqlCondition = "IFNULL(_table_name.value, _table_name_default.value) REGEXP '^[^a-zA-Z]'" ...