A. Yet Another Problem with Strings 分块 + hash
http://codeforces.com/gym/101138/problem/A
感觉有一种套路就是总长度 <= 某一个数的这类题,大多可以分块
首先把集合串按长度分块,对于每一个询问串,
在 > magic的big集合里,因为最多sqrtn个,可以暴力枚举每一个,然后暴力枚举询问串的每一个长度是其的子串,判断是否相等
在 <= magic的small集合里,枚举每一个长度是magic, magic-1, magic-2, magic-3........1的字符串,然后看看是否在small集合里存在
small集合用unordermap保存即可。
ps
判断map是否存在一个元素,用mp.find
不然用mp[]每次都会生成一个节点,然后MLE, 清空用mp.erase
不然Mp.find是true的
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef unsigned long long int ULL; const int seed = ;
const int maxn = + ;
struct Node {
ULL hs;
int lenstr;
Node(ULL _hs, int _lenstr) {
hs = _hs, lenstr = _lenstr;
}
bool operator < (const Node & rhs) const {
if (hs != rhs.hs) return hs > rhs.hs;
else return lenstr > rhs.lenstr;
}
};
vector<Node> all;
set<Node> ss;
unordered_map<ULL, bool> mp;
char str[maxn];
int magic;
set<Node> :: iterator it;
void addchar(int id, char ch) {
// set<Node> :: iterator it;
if (all[id].lenstr > magic) {
ss.erase(ss.find(all[id]));
all[id].lenstr++;
all[id].hs = all[id].hs * seed + ch;
ss.insert(all[id]);
} else {
if (all[id].lenstr == magic) {
mp.erase(all[id].hs);
// mp[all[id].hs] = false;
all[id].lenstr++;
all[id].hs = all[id].hs * seed + ch;
ss.insert(all[id]);
} else {
mp.erase(all[id].hs);
all[id].lenstr++;
all[id].hs = all[id].hs * seed + ch;
mp[all[id].hs] = true;
}
}
}
ULL po[maxn];
ULL sum[maxn];
int last_yes;
bool ok() {
int lenstr = strlen(str + );
for (int i = ; i <= lenstr; ++i) {
str[i] = (str[i] - 'a' + last_yes) % + 'a';
sum[i] = sum[i - ] * seed + str[i];
}
for (it = ss.begin(); it != ss.end(); ++it) {
int len = it->lenstr;
for (int j = len; j <= lenstr; ++j) {
if (it->hs == sum[j] - po[len] * sum[j - len]) return true;
}
}
for (int i = ; i <= lenstr; ++i) {
for (int c = ; c <= magic && c <= i; ++c) {
if (mp.find(sum[i] - po[c] * sum[i - c]) != mp.end()) return true;
}
}
return false;
} void work() {
int n, q;
scanf("%d%d", &n, &q);
magic = (int)sqrt(n * 1.0);
for (int i = ; i <= n; ++i) {
scanf("%s", str + );
ULL hashVal = ;
int lenstr = strlen(str + );
for (int j = ; j <= lenstr; ++j) {
hashVal = hashVal * seed + str[j];
}
if (lenstr > magic) ss.insert(Node(hashVal, lenstr));
else {
mp[hashVal] = true;
}
all.push_back(Node(hashVal, lenstr));
}
last_yes = ;
for (int i = ; i < q; ++i) {
int op;
scanf("%d", &op);
if (op == ) {
int index, ch;
scanf("%d%d", &index, &ch);
addchar((index + last_yes) % n, (ch + last_yes) % + 'a');
} else {
scanf("%s", str + );
if (ok()) {
printf("YES\n");
last_yes = i;
} else printf("NO\n");
}
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
po[] = ;
for (int i = ; i <= maxn - ; ++i) po[i] = po[i - ] * seed;
work();
return ;
}
A. Yet Another Problem with Strings 分块 + hash的更多相关文章
- Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- hdu_5085_Counting problem(莫队分块思想)
题目连接:hdu_5085_Counting problem 题意:给你一个计算公式,然后给你一个区间,问这个区间内满足条件的数有多少个 题解:由于这个公式比较特殊,具有可加性,我们考虑讲一个数分为两 ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】
一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...
- POJ3468 a simple problem with integers 分块
题解:分块 解题报告: 是个板子题呢qwq 没什么可说的,加深了对分块的理解趴还是 毕竟这么简单的板子题我居然死去活来WA了半天才调出来,,,哭了QAQ 还是说下我错在了哪几个地方(...是的,有好几 ...
- CF985F Isomorphic Strings (字符串Hash,巧解)
题目链接 题意翻译 给你一个长度为 \(n\) 的字符串,\(m\) 次询问. 问两个相同长度的子串是否匹配. 我们称两个子串是匹配的,当且仅当其满足: 其中一个子串的字母可替代另一个子串的字母 例如 ...
- POJ 3468 A Simple Problem with Integers (分块)
Description You have \(N\) integers, \(A_1, A_2, ... , A_N\). You need to deal with two kinds of ope ...
- CodeForces 165C Another Problem on Strings(组合)
A string is binary, if it consists only of characters "0" and "1". String v is a ...
- Day8 - C - Another Problem on Strings CodeForces - 165C
A string is binary, if it consists only of characters "0" and "1". String v is a ...
随机推荐
- 数据结构_sfdg(小F打怪)
问题描述 小 F 很爱打怪, 今天因为系统 bug, 他提前得知了 n 只怪的出现顺序以及击倒每只怪得到的成就值 ai. 设第一只怪出现的时间为第 1 秒,这个游戏每过 1 秒钟出现一只新怪且没被击倒 ...
- Linux sogou input method
afda@afda-Y720-15IKB:~$ wget "http://pinyin.sogou.com/linux/download.php?f=linux&bit=64&quo ...
- 20169219《Linux内核原理与分析》课程总结
第一周作业 这周主要学习l了linux基础入门这个课,还是很有用的.一周学完,前两个可能做起来比较慢,但是后面就快了,不要放弃希望,你可以的! 我就是没写到博客里面,哼哼,你就不要看了.自己总结吧,摊 ...
- 文本PDG文件名构成
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03 文本PDG的构成规则为:<前缀><起始页号>_<页数>.pdg 前缀 ...
- bat 操作数据库(附加,分离,删除,还原)
BAT代码: @echo off Title DataBase Color 0A :caozuo echo. echo ═══════════════════════════════════════ ...
- scala lambda 表达式 & spark RDD函数操作
形式:(参数)=> 表达式 [ 一种匿名函数 ] 例1:map(x => x._2) 解:x=输入参数,“=>” 右边是表达式(处理参数): x._2 : x变为(**,x,**. ...
- help手册使用
属性的方法名的一般规律: 设置的属性名: set+属性名 获取属性值: 1.如果是bool类型,可能是 is+属性名 或者 属性名 2.不是bool类型,就是属性名
- mysql 表压缩
mysql中经常出现历史表,这些表不会进行修改数据的操作,只有读操作.那么我们可以对其进行压缩处理,缩减磁盘空间.Innodb表和MyISAM表的压缩指令不一样.下面分别来讨论: 一.InnoDB表 ...
- Python之‘数据结构’
简介 数据结构基本上就是--它们是可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的.在Python里面有三种内建的数据结构--列表.元组和字典. 一.列表 list是处理一组有序项目的数据 ...
- 数据结构28:广义表及M元多项式
广义表,又称为列表.记作: LS = (a1,a2,…,an) ;( LS 为广义表的名称, an 表示广义表中的数据). 广义表可以看作是线性表的推广.两者区别是:线性表中的数据元素只能表示单个数据 ...