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 ...
随机推荐
- Pull项目失败
1.网速原因 2.提示邮箱失效. 邮箱失效:解决方案 File->Setting: 然后,要记得重启,IDEA. 然后,在终端输入:git branch -l 查看项目分支 这样,设置好了用户名 ...
- WordCount 编码与测试
word count github 项目地址:https://github.com/liuqiang666/wordCount PSP表格 PSP2.1 PSP阶段 预估耗时(小时) 实际耗时( ...
- LeetCode第110题:平衡二叉树
问题描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,nu ...
- HDU 2819 Swap (二分匹配+破输出)
题意:给定上一个01矩阵,让你变成一个对角全是 1 的矩阵. 析:二分匹配,把行和列看成两个集合,用匈牙利算法就可以解决,主要是在输出解,在比赛时一紧张不知道怎么输出了. 输出应该是要把 match[ ...
- android报错:org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapObject
今天在写一个webservice时一直报错,报Caused by: java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast ...
- SourceTree——MAC OSX下的Git GUI客户端
在MAC下面为Git找一款用得顺手的GUI客户端还真不容易.学习工具使用还是先器而后道的路径比较适合我,当年上手CVS SVN都是如此,先通过tortoise客户端熟练了基本操作,之后在搭建构建平台的 ...
- 解决VMware Workstation 不可恢复错误: (vcpu-0)
转载:http://tieba.baidu.com/p/3487673152 如图的错误 如果你按照破解了mac支持的VMware Workstation 11的新建虚拟机向导一步一步创建了一个mac ...
- json互转笔记
JSON.parse()与JSON.stringify()的区别 JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"name& ...
- git 命令总结(转)
结构图: <1> Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新建代码库 # 在当前目录新建 ...
- [SinGuLaRiTy] NOIP膜你赛-Day 1
[SinGuLRiTy-1022] Copyright (c) SinGuLaRITy 2017. All Rights Reserved. 对于所有题目:Time Limit:1s || Memo ...