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 ...
随机推荐
- 数据结构_bubble_sort
问题描述 给定一个 1~N 的排列 P,即 1 到 N 中的每个数在 P 都只出现一次. 现在要对排列 P 进行冒泡排序,代码如下:for (int i = 1; i <= N; ++i)for ...
- form的提交方式
1,最普通最常用的方法就是用submit type.. <form name=”form” method=”post” action=”#"> <input type=”s ...
- 自定义MVC的Helper扩展方法 转 Insus.NET
记得在开发ASP.NET时候,也经常性使用C#可以写自己义的扩展方法,如: http://www.cnblogs.com/insus/p/3154363.html 或http://www.cnblog ...
- Java50道经典习题-程序26 求星期
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母.分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母.周一至周日的英文单词 ...
- java7大设计原则
1.单一职责原则(srp) 每一个对象或者类只有一个职责.高内聚.低耦合. 2.开闭原则(ocp) 对扩展开放,对修改关闭.使用性,灵活性,扩展性,稳定性延续性,重复.维护. 3.里氏替换原则(lsp ...
- 正经学C#_位移与其位移运算符[c#入门经典]
在c#入门经典一书中,最为糟糕的一节就是位移了,完全没有讲明白,也没有说全,似乎只是轻轻点了一下何为位移,带了两次原码和补码,完全不理会是否明白不明白.这一点这本书很差.因为此书说了,在大多数应用开发 ...
- AJAX使用四步曲
前言 AJAX这个东西还是很模糊的,下面会对AJAX这个技术进行详细讲解一些,另外,在网上商城中应用到了,使用它有四个步骤,下面详细介绍一些. 内容 定义: AJAX=异步JavaScript和XML ...
- Burp Suite初探
Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程. 一.安装部署 需要配置java环境,首先安装java,然后配置 ...
- 暴力【bzoj2208】: [Jsoi2010]连通数
2208: [Jsoi2010]连通数 暴力过的. 没脸说... 正解好像是缩点+递推. 应该也不难写. code: #include <iostream> #include <cs ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...