codeforces 645E . Intellectual Inquiry
题目链接
如果不考虑重复的元素, 那么我们可以很容易的发现, 长度为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的更多相关文章
- Codeforces 645E. Intellectual Inquiry(DP,贪心)
Codeforces 645E. Intellectual Inquiry 题意:给定一串字符,由前k个小写拉丁字母组成,要求在该字符串后面补上n个字符(也从前k个小写拉丁字母里面选),使得最后得到的 ...
- 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 ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp
E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...
- codeforces 645 E. Intellectual Inquiry
一个字符串,由前k个字母组成,长度为m + n,其中前m个字符已经确定,后面n个由你自由选择, 使得这个串的不同的子序列的个数最多,空串也算一个子序列. 1 <= m <= 10^6,0 ...
- CF CROC 2016 Intellectual Inquiry
题目链接:http://codeforces.com/contest/655/problem/E 大意是Bessie只会英文字母表中的前k种字母,现在有一个长度为m+n的字母序列,Bessie已经知道 ...
- 「CF645E」 Intellectual Inquiry
题目链接 CF645E 题意 有一个长为\(n\)的由小写字母组成的字符串,需要用小写字母再填\(m\)位,使最后的字符串中本质不同的子串数量尽量多,答案对\(10^9+7\)取模. 本题数据:\(n ...
- Codeforces Gym 100269A Arrangement of Contest 水题
Problem A. Arrangement of Contest 题目连接: http://codeforces.com/gym/100269/attachments Description Lit ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- q.js实现nodejs顺序调用
nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise 什么是promise promise一个标准,它描述了异步调用的返回结果,包括正确 ...
- .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending
.NET 3.5(5) - LINQ查询操作符之Select.Where.OrderBy.OrderByDescending 介绍 ·Select - Select选择:延迟 ·Where ...
- EF6 Codefirst+MySql 数据库迁移
简介 项目使用MSSql作为数据库,但是因为SQL服务器贵那么一点,并发连接差那么一点,要把数据迁移到MySQL,顺带迁移过程以及问题. 环境 Visual Studio 2013 MySQL 5.7 ...
- Android ListView滑动底部自动加载更多
直接上代码: // lv = (ListView) findViewById(R.id.lv); // // for(int i = 0;i < 50;i++){ // ls.add(" ...
- @Html.ActionLink 添加样式 html标签
@Html.ActionLink(item.MessageTitle, "Detail", "News",new { MessageId = item.Mess ...
- laravel5.1框架简介及安装
最近自己出来实习了,进入了一个新的环境,不仅是生活中,在代码和架构中也完全是一个新的架构.由于公司使用laravel5.1框架,所以最近学习了laravel5.1框架,好了接下来就简单介绍一下lara ...
- Qt 中程序自动重启
参照至 dbzhang老师的博文,记录于此....... 要想理解重启,先得看看Qt程序怎么退出! 1.退出 int main(int argc, char** argv) { QApplicatio ...
- docker 在esx上的网络配置
- shell programs
find * -not -path "docs/*" -regex ".*\.\(rb\)" -type f -print0 | xargs -0 gr ...
- poj1007 qsort快排
这道题比较简单,但通过这个题我学会了使用c++内置的qsort函数用法,收获还是很大的! 首先简要介绍一下qsort函数. 1.它是快速排序,所以就是不稳定的.(不稳定意思就是张三.李四成绩都是90, ...