D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash
http://codeforces.com/contest/535/problem/D
如果真的要把m个串覆盖上一个串上面,是可以得,不会超时。
要注意到一点,全部覆盖后再判断时候合法,和边放边判断,结果是一样的,后者还更难做到。
那么就是先按顺序把串覆盖上去,已经存在的就不去覆盖了,然后kmp一次记录匹配位置,判断即可。
用DFS覆盖,DFS回溯的时候记录一个数组tonext[i]表示第i个点的第一个空位是tonext[i],这样覆盖上去就是O(n)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
int tonext[maxn];
int ret;
char str[maxn];
char sub[maxn];
void dfs(int cur, int k, int from) {
if (k < ) return;
if (k == ) {
ret = cur;
return;
}
if (tonext[cur] != cur) {
ret = tonext[cur];
dfs(tonext[cur], k - (tonext[cur] - cur), from + tonext[cur] - cur);
tonext[cur] = ret;
} else {
ret = cur + ;
str[cur] = sub[from];
dfs(cur + , k - , from + );
tonext[cur] = ret;
}
}
int a[maxn];
bool HASH[maxn];
int kmpnext[maxn];
void get_next(char sub[], int lensub) {
int i = , j = ;
kmpnext[] = ;
while (i <= lensub) {
if (j == || sub[i] == sub[j]) {
kmpnext[++i] = ++j;
} else j = kmpnext[j];
}
return;
}
void kmp(int lenstr, int lensub) {
int i = , j = ;
while (i <= lenstr) {
if (j == || str[i] == sub[j]) {
++i;
++j;
} else j = kmpnext[j];
if (j == lensub + ) {
HASH[i - lensub] = true;
j = kmpnext[j];
}
}
return;
}
void work() {
int lenstr, m;
scanf("%d%d", &lenstr, &m);
for (int i = ; i <= lenstr; ++i) {
tonext[i] = i;
str[i] = 'A';
// printf("f");
}
// printf("%c\n", str[1]);
scanf("%s", sub + );
int lensub = strlen(sub + );
for (int i = ; i <= m; ++i) {
scanf("%d", &a[i]);
dfs(a[i], lensub, );
}
str[lenstr + ] = '\0';
// printf("%s\n", str + 1);
get_next(sub, lensub);
kmp(lenstr, lensub);
for (int i = ; i <= m; ++i) {
if (!HASH[a[i]]) {
cout << << endl;
return;
}
}
LL ans = ;
for (int i = ; i <= lenstr; ++i) {
if (str[i] == 'A') {
ans *= ;
if (ans >= MOD) ans %= MOD;
}
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
开始的时候想不到直接kmp
用的其实就是kmp的next数组,不断next[next[]]
就是记录所有的前缀和后缀相等。
对于abc****abc,也就是前后缀相等的话,长度是3
记上一个覆盖到的位置是pos[i - 1] + lensub - 1
然后如果这个和他没交集,就算,如果有,要判断。怎么判断呢?
算出交集大小,如果交集刚好是3,那么是可以得。否则,是NO
交集是3说明后缀的那3个和前缀匹配了。
注意m可能是0
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
char sub[maxn];
int tonext[maxn];
int pos[maxn];
bool book[maxn];
void get_next(char sub[], int lensub) {
int i = , j = ;
tonext[] = ;
while (i <= lensub) {
if (j == || sub[i] == sub[j]) {
tonext[++i] = ++j;
} else j = tonext[j];
}
return;
}
void work() {
int lenstr, m;
// cin >> lenstr >> m;
// cin >> sub + 1;
scanf("%d%d", &lenstr, &m);
scanf("%s", sub + );
int lensub = strlen(sub + );
get_next(sub, lensub);
for (int i = ; i <= m; ++i) {
// cin >> pos[i];
scanf("%d", &pos[i]);
}
int t = tonext[lensub + ];
while (t != ) {
book[t - ] = true;
t = tonext[t];
}
int total = lenstr - lensub;
if (m == ) {
total += lensub;
}
for (int i = ; i <= m; ++i) {
int to = pos[i - ] + lensub - ;
int haha = to - pos[i] + ;
if (haha <= ) {
total -= lensub;
continue;
}
if (!book[haha]) {
printf("0\n");
return;
}
total -= lensub - haha;
}
LL ans = ;
for (int i = ; i <= total; ++i) {
ans *= ;
if (ans >= MOD) ans %= MOD;
}
printf("%I64d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash的更多相关文章
- Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp
题目链接: http://codeforces.com/problemset/problem/535/D D. Tavas and Malekas time limit per test2 secon ...
- D. Tavas and Malekas 解析(字串匹配)
Codeforce 535 D. Tavas and Malekas 解析(字串匹配) 今天我們來看看CF535D 題目連結 題目 給你一個字串$p$和一些$index$代表字串$p$在哪些位置會和長 ...
- Codeforces 535D - Tavas and Malekas
535D - Tavas and Malekas 题目大意:给你一个模板串,给你一个 s 串的长度,告诉你 s 串中有 m 个模板串并告诉你,他们的其实位置, 问你这样的 s 串总数的多少,答案对1e ...
- Vijos P1114 FBI树【DFS模拟,二叉树入门】
描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...
- 矩阵hash + KMP - UVA 12886 The Big Painting
The Big Painting Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=88791 M ...
- HDU 5438 Ponds dfs模拟
2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...
- 字符串Hash/树Hash学习笔记
哈希 Tags:字符串 作业部落 评论地址 一.概述 百度百科: 散列表(Hash table/哈希表),是根据关键码值(Key value)而直接进行访问的数据结构. 哈希表常用于比较两个字符串是否 ...
- Hash Map (Hash Table)
Reference: Wiki PrincetonAlgorithm What is Hash Table Hash table (hash map) is a data structure use ...
- kmp&扩展kmp
kmp: KMP的主要目的是求B是不是A的子串,以及若是,B在A中所有出现的位置 写的很详细的大佬的博客:http://www.matrix67.com/blog/archives/115 模板: / ...
随机推荐
- Statelessness Provide credentials with the request. Each request MUST stand alone and should not be affected from previous conversation happened from same client in past.
The server never relies on information from previous requests. Statelessness As per the REST (REpres ...
- Unity3D集成腾讯语音GVoice SDK
友情提示:最近发现腾讯GVoice有另一个官网,叫做腾讯游戏服务,经过对比发现这个网站才是最新的,下面我介绍的那个估计是已经废弃的,但不知道为啥老的网站没有直接链接到新网址而是仍然保留了.不过新官网的 ...
- bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)
题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...
- Github--开源代码仓库式系统(转)
要了解Github,我们首先要知道Git,Git是管理代码的工具,写代码不是件轻松的事儿,一个人写的时候已经不轻松了,一群人写就更不轻松了,但这世界上很多事都是怎么不轻松怎么来的,大部分人都会和别人一 ...
- node.js适合游戏后台开发吗?
网站服务器和游戏服务器是怎么样联系到一起的? 百牛信息技术bainiu.ltd整理发布于博客园 1. 游戏分很多种,咱们先来看看MMORPG. 再怎么简单的RPG服务器都免不了处理多人交互的情形,上百 ...
- robotframework:appium切换webview后,在第一个页面操作成功,跳转到第二个页面后,执行命令失败
问题: 在用robot写手机淘宝app的自动化时,打开手机淘宝后,点击天猫国际,跳转到天猫国际页面,天猫国际页面是H5, 需要切换到对应的webview,切换到webview后,点击美妆菜单,跳转到美 ...
- Bootstrap-CL:字体图标(Glyphicons)
ylbtech-Bootstrap-CL:字体图标(Glyphicons) 1.返回顶部 1. Bootstrap 字体图标(Glyphicons) 本章将讲解字体图标(Glyphicons),并通过 ...
- 伪元素 first-letter
::after, ::before, ::backdrop, ::first-letter,::first-line, ::selection(css3)是伪元素, :active, :focus, ...
- WaveNet: 原始音频生成模型
官方博客 WaveNet: A Generative Model for Raw Audio paper地址:paper Abstract WaveNet是probabilistic and auto ...
- 与adb相关的问题,比如掉线问题、Android Studio 提示Session 'app':Error Installing APK、找不到设备
这一篇帖子 会写的比较简单 不过相信大家也可能遇到这些问题 为了怕自己忘记 记录下来 顺便也和大家一起分享 描述:在一些机型上安装软件 提示卸载原先的软件 但是又安装不上新软件 DELETE ...