题目链接:

http://codeforces.com/problemset/problem/535/D

D. Tavas and Malekas

time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string s of length n comes out from Tavas' mouth instead.
>
>
> Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on s. Malekas has a favorite string p. He determined all positions x1 
> Then Malekas wrote down one of subsequences of x1, x2, ... xk (possibly, he didn't write anything) on a piece of paper. Here a sequence b is a subsequence of sequence a if and only if we can turn a into b by removing some of its elements (maybe no one of them or all).
>
> After Tavas woke up, Malekas told him everything. He couldn't remember string s, but he knew that both p and s only contains lowercase English letters and also he had the subsequence he had written on that piece of paper.
>
> Tavas wonders, what is the number of possible values of s? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him.
>
> Answer can be very large, so Tavas wants you to print the answer modulo 109 + 7.
#### 输入
> The first line contains two integers n and m, the length of s and the length of the subsequence Malekas wrote down (1 ≤ n ≤ 106 and 0 ≤ m ≤ n - |p| + 1).
>
> The second line contains string p (1 ≤ |p| ≤ n).
>
> The next line contains m space separated integers y1, y2, ..., ym, Malekas' subsequence (1 ≤ y1  In a single line print the answer modulo 1000 000 007.
####样例输入
> 6 2
> ioi
> 1 3

样例输出

26

题意

给你一个子串p,且在长度为n的原串中出现的m个插入位,问原串有几种可能。

题解

我们只要考虑插入位置之间的重叠部分是否能够完全匹配,这个其实就是p串的前缀在和后缀匹配,用kmp处理出failed指针就可以轻松解决了。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=1e6+10; const int mod=1e9+7; LL xp26[maxn]; char str[maxn];
int arr[maxn];
int n,m,len; int f[maxn],vis[maxn];
void getFail(char* P,int* f){
int m=strlen(P);
f[0]=0; f[1]=0;
for(int i=1;i<m;i++){
int j=f[i];
while(j&&P[i]!=P[j]) j=f[j];
f[i+1]=P[i]==P[j]?j+1:0;
} clr(vis,0);
int j=f[m];
while(j){
vis[j]=1;
// bug(j);
j=f[j];
}
} int main() { scf("%d%d",&n,&m);
scf("%s",str);
len=strlen(str); getFail(str,f); rep(i,0,m) scf("%d",&arr[i]); int ans=m?len:0; for(int i=1; i<m; i++) {
int x1=arr[i-1],x2=arr[i];
int y1=x1+len-1,y2=x2+len-1;
if(y1<x2) ans+=len;
else {
int l=y1-x2+1;
// bug(l);
if(vis[l]) {
ans+=y2-y1;
} else {
prf("0\n");
return 0;
}
}
} LL last=1;
for(int i=0;i<n-ans;i++) last*=26,last%=mod;
prf("%I64d\n",last); return 0;
} //end-----------------------------------------------------------------------

写了个哈希的,死且仅死在第67组数据,xrz,

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=1e6+10; const int P=321;
const int mod=1e9+7; unsigned long long H[maxn],xp[maxn];
LL xp26[maxn]; char str[maxn];
int arr[maxn];
int n,m,len; void pre() {
xp[0]=1;
rep(i,1,maxn) xp[i]=xp[i-1]*P; xp26[0]=1;
rep(i,1,maxn) xp26[i]=xp26[i-1]*26%mod;
} void get_H() {
H[0]=0;
for(int i=1; i<=len; i++) {
H[i]=H[i-1]*P+str[i]-'a'+1;
}
} unsigned long long query(int l,int r) {
return H[r]-H[l-1]*xp[r-l+1];
} int main() {
pre();
scf("%d%d",&n,&m);
scf("%s",str+1);
len=strlen(str+1); get_H(); rep(i,0,m) scf("%d",&arr[i]); sort(arr,arr+m); int ans=m?len:0; for(int i=1; i<m; i++) {
int x1=arr[i-1],x2=arr[i];
int y1=x1+len-1,y2=x2+len-1;
if(y1<x2) ans+=len;
else {
int l=y1-x2+1;
if(query(1,l)==query(len-l+1,len)) {
ans+=y2-y1;
} else {
// puts("fuck");
// bug(l);
prf("0\n");
return 0;
}
}
}
// bug(ans);
prf("%I64d\n",xp26[n-ans]); return 0;
} //end-----------------------------------------------------------------------

Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp的更多相关文章

  1. Codeforces Round #299 (Div. 2)D. Tavas and Malekas

    KMP,先预处理按每个节点标记,扫一遍更新每个匹配位置,最后kmp判断是否有重合而且不相同的地方 注意处理细节,很容易runtime error #include<map> #includ ...

  2. 二分搜索 Codeforces Round #299 (Div. 2) C. Tavas and Karafs

    题目传送门 /* 题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1 二分搜索:搜索r,求出sum <= t * m的最大的r 详细解释:http:/ ...

  3. 水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas

    题目传送门 /* 很简单的水题,晚上累了,刷刷水题开心一下:) */ #include <bits/stdc++.h> using namespace std; ][] = {" ...

  4. DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas

    题目传送门 /* DFS:按照长度来DFS,最后排序 */ #include <cstdio> #include <algorithm> #include <cstrin ...

  5. Codeforces Round #299 (Div. 1) A. Tavas and Karafs 水题

    Tavas and Karafs Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/536/prob ...

  6. Codeforces Round #299 (Div. 2) B. Tavas and SaDDas 水题

    B. Tavas and SaDDas Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/535/p ...

  7. Codeforces Round #299 (Div. 2) A. Tavas and Nafas 水题

    A. Tavas and Nafas Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/535/pr ...

  8. Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)

    C. Tavas and Pashmaks   Tavas is a cheerleader in the new sports competition named "Pashmaks&qu ...

  9. Codeforces Round #299 (Div. 2) B. Tavas and SaDDas【DFS/*进制思维/位运算/一个数为幸运数,当且仅当它的每一位要么是4,要么是7 ,求小于等于n的幸运数个数】

    B. Tavas and SaDDas time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. 关于Linux的交叉编译环境配置中的问题

    Linux的交叉编译arm-linux-gcc搭建时,安装结束却无法查看版本.输入以下命令查看Ubuntu的版本: uname -a 可以看到此Ubuntu为64位16.04.1版本,所以需要下载32 ...

  2. 对sql作业的总结(PostgreSQL的递归查询)

    已知条件如下: CREATE TABLE appointment ( emp_id integer NOT NULL, jobtitle ) NOT NULL, salary ,) NOT NULL, ...

  3. 基于R语言的结构方程:lavaan简明教程 [中文翻译版]

    lavaan简明教程 [中文翻译版] 译者注:此文档原作者为比利时Ghent大学的Yves Rosseel博士,lavaan亦为其开发,完全开源.免费.我在学习的时候顺手翻译了一下,向Yves的开源精 ...

  4. WP10的一点小问题

    兼容WP8.0/WP7.5不太完整!也许是测试版的问题.毕竟还没发布正式版! 具体如:WP8.0或WP7.5的启动器!就是选择图片的启动器!调用后对话框返回的结果都是Cancel本来应该是OK的.也就 ...

  5. 20155238 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    实验内容 使用JDK编译.运行简单的Java程序. 使用Eclipse 编辑.编译.运行.调试Java程序. 实现学生成绩管理功能,并进行测试. 实验步骤及结果 (一)命令行下Java程序开发 编译运 ...

  6. matplotlib绑定到PyQt5(无菜单)

    很简单的实现matplotlib绑定到PyQt5 [知识点] import matplotlib matplotlib.use("Qt5Agg") from matplotlib. ...

  7. 洛谷 P1198 [JSOI2008]最大数

    洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...

  8. Velocity学习4

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...

  9. BootStap学习笔记(2)

    学习该内容之前可能会用到的内容: css属性Font-Weight:如果数字为700就是加粗的.或者更粗的为bolder,更细的是lighter. html Cite标签定义文档的引用,默认字体以斜体 ...

  10. 【SIKIA计划】_04_C#中级教程 (2015版)笔记

    IKIC#中级教程 (2015版)正常模式指的是不会影响程序的正常运行.1,在VS中我们使用Console.Write(或者WriteLine)方法向控制台输出变量的值,通过这个我们可以查看变量的值是 ...