【codeforces 514C】Watto and Mechanism(字符串hash)
【题目链接】:http://codeforces.com/contest/514/problem/C
【题意】
给你n个字符串;
然后给你m个询问;->m个字符串
对于每一个询问字符串
你需要在n个字符串里面找到和它的长度相同,且只有一个位置的字符不同的字符串;
或者告知这是不存在的;
【题解】
字符串hash
因为只有3个字符
所以权值就为3^x;
找个大质数取模就好;
不够大就再大一点。。
然后对于每一位,尝试换成其他两个字母,看看存不存在;
【Number Of WA】
3
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 6e5+100;
const LL MOD = 1e12+7;
int n,m;
LL p[N];
string s;
set<LL> myset;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
p[0] = 1;
rep1(i,1,N-2)
p[i] = (p[i-1]*3)%MOD;
rei(n),rei(m);
rep1(i,1,n)
{
cin >> s;
LL temp = 0;
int len = s.size();
rep1(j,0,len-1)
temp = (temp*3+s[j]-'0')%MOD;
myset.insert(temp);
}
rep1(i,1,m)
{
cin >> s;
bool ok = false;
int len = s.size();
LL temp = 0,ttemp;
rep1(j,0,len-1)
temp = (temp*3+s[j]-'0')%MOD;
rep1(j,0,len-1)
{
char t = s[j];
for (char d='a';d<='c';d++)
if (t!=d)
{
ttemp = (temp-(t-'0')*p[len-j-1])%MOD;
if (ttemp<0) ttemp=(ttemp+MOD)%MOD;
ttemp=(ttemp+(d-'0')*p[len-j-1])%MOD;
if (myset.count(ttemp))
{
ok = true;
break;
}
}
if (ok) break;
}
if (ok)
puts("YES");
else
puts("NO");
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 514C】Watto and Mechanism(字符串hash)的更多相关文章
- Codeforces 514C Watto and Mechanism(字典树)
题目链接 Watto and Mechanism 题意 给出$n$个串(相当于字典),然后给出$m$个询问. 每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符 (必须改变且只能改变 ...
- Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】
<题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...
- codeforces gym 101164 K Cutting 字符串hash
题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...
- Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串
[题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...
- 【题解】 Codeforces Edu41 F. k-substrings (字符串Hash)
题面戳我 Solution 我们正着每次都要枚举从长到短,时间复杂度承受不了,但是我们可以发现一个规律,假设某次的答案为\(x\),那么这个字符串为\(A+X+B\)组成,无论中间的\(X\)是重叠还 ...
- hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
- Watto and Mechanism Codeforces Round #291 (Div. 2)
C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]
传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...
随机推荐
- C#在WinForm中使用WebKit传递js对象实现与网页交互的方法
这篇文章主要介绍了C#在WinForm中使用WebKit传递js对象实现与网页交互的方法,涉及针对WebBroswer控件及WebKit控件的相关使用技巧,需要的朋友可以参考下 本文实例讲述了C#在W ...
- [luoguP4142]洞穴遇险
https://www.zybuluo.com/ysner/note/1240792 题面 戳我 解析 这种用来拼接的奇形怪状的东西,要不就是轮廓线\(DP\),要不就是网络流. 为了表示奇数点(即\ ...
- Makefile 实际用例分析(二) ------- 比较通用的一种架构
之前已经讲了这一篇文章:Makefile实际用例分析(一)-----比较通用的一种架构 现在这篇其实和那个差的不是很多,只是在布局上有些差别(这个makefile也是论坛上一起讨论过的,囧,忘了哪个论 ...
- bzoj2427:[HAOI2010]软件安装(Tarjan+tree_dp)
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1053 Solved: 424[Submit][Statu ...
- JavaScript--输出内容(document.write)
document.write() 可用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容. 第一种:输出内容用“”括起,直接输出""号内的内容. <scrip ...
- 51nod1446 Kirchhoff矩阵+Gauss消元+容斥+折半DFS
思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...
- azkaban-executor启动时出现conf/global.properties (No such file or directory)的问题解决(图文详解)
问题详情 // :: INFO [FlowRunnerManager] [Azkaban] Cleaning recently finished // :: INFO [FlowRunnerMana ...
- D3.js 力导向图(气泡+线条+箭头+文字)
<!DOCTYPE html> <meta charset="utf-8"> <style> .link { fill: none; strok ...
- Skeleton Screen — 骨架屏
用户体验一直是前端开发需要考虑的重要部分,在数据请求时常见到锁屏的loading动画,而现在越来越多的产品倾向于使用Skeleton Screen Loading(骨架屏)替代,以优化用户体验. Sk ...
- PHP MySQL 连接数据库,进行增、删、改、查、操作
<table width="100%" border="1" cellpadding="0" cellspacing="0& ...