题目链接:

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

D. Tavas and Malekas

time limit per test2 secondsmemory 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 < x2 < ... < xk where p matches s. More formally, for each xi (1 ≤ i ≤ k) he condition sxisxi + 1... sxi + |p| - 1 = p is fullfilled.

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 < y2 < ... < ym ≤ n - |p| + 1).

输出

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 printftypedef __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 printftypedef __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-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5902276.html

Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp相关推荐

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

    题目传送门 1 /* 2 DFS:按照长度来DFS,最后排序 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #includ ...

  2. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes kmp + dp

    传送门 文章目录 题意: 思路: 题意: 思路: 通过完美子串的定义,我们不难发现满足条件的子串就是kmpkmpkmp中ne[n]ne[n]ne[n]不断向前跳得到的串,现在问题就是如何求这些前缀串在 ...

  3. 【Codeforces Round #299 (Div. 2) B】Tavas and SaDDas

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每次取出最小的数字,在后面加上一个4或一个7就好; 会发现最后的数字很少的. [代码] #include <bits/stdc ...

  4. codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)

    题意:一个等差数列,首项为a,公差为b,无限长.操作cz是区间里选择最多m个不同的非0元素减1,最多操作t次,现给出区间左端ll,在t次操作能使区间全为0的情况下,问右端最大为多少. 这么一个简单题吞 ...

  5. Codeforces Round #820 (Div. 3) G. Cut Substrings(kmp状态机dp)

    TP 题意: 给定两个字符串 s.t,问最少删除多少个 s 中出现的 t 串,使得 s 中不再存在任何 t 串.删除后的位置不拼接,而是变成 - 点 同时统计最少删除的方案数. 思路: 看到维护 最少 ...

  6. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  7. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  8. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  9. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

最新文章

  1. 【一周速递】计算机视觉/图像处理论文集
  2. CentOS文件浏览器设置
  3. Vue.js 组件基础
  4. ES 6 +ES 5 的相关学习笔记
  5. mysql 表字段信息从一张表迁移到另一张表_MySQL(数据库)笔记
  6. Flash和HTML5那点事:后者拿什么取代Flash?
  7. python自动补全_Windows 下python的tab自动补全
  8. java 阻塞队列 BQ_阻塞队列 BlockingQueue的使用(二)
  9. OpenV2X 开源社区成立,填补 5G 路侧开放基础架构(RSOI)空白
  10. 交换机组播风暴_交换机广播风暴控制知识
  11. UniApp使用navigateTo无法跳转到tabBar中的页面
  12. 基于PHP的学生在线考试管理系统
  13. 大数据在高校中的应用
  14. 超可爱的宝藏网站,看看你在漫画中长什么样
  15. 记一次WebService调用踩的坑
  16. linux 读卡器信息,Linux如何使用笔记本自带的SD/MMC读卡器
  17. 2006胡润百富榜发布 张茵荣登中国第一位女首富
  18. nginx 初级总汇知识点
  19. 行式和列式存储说明以及OLAP特点介绍
  20. 管理口令(P):[INS-30011] 输入的 ADMIN 口令不符合 Oracle 建议的标准

热门文章

  1. jquery ajax 文本丢失加号和连接号的问题
  2. 案例:隐秘而低调的内存泄露(OOM)
  3. Linux系统下的权限试题测试
  4. 前端又要失失失失失失失失失业了!
  5. ElasticSearch安装过程中遇到的一些问题
  6. You have new mail in /var/spool/mail/root消除提示的方法
  7. java中可重入锁的学习总结
  8. windows7下安装php的imagick和imagemagick扩展教程
  9. 一个简单的基于socket的通讯处理程序
  10. Starling框架帮助手册中文版(PDF下载)