【题目链接】: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)的更多相关文章

  1. Codeforces 514C Watto and Mechanism(字典树)

    题目链接  Watto and Mechanism 题意  给出$n$个串(相当于字典),然后给出$m$个询问. 每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符 (必须改变且只能改变 ...

  2. Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】

    <题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...

  3. codeforces gym 101164 K Cutting 字符串hash

    题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...

  4. Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串

    [题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...

  5. 【题解】 Codeforces Edu41 F. k-substrings (字符串Hash)

    题面戳我 Solution 我们正着每次都要枚举从长到短,时间复杂度承受不了,但是我们可以发现一个规律,假设某次的答案为\(x\),那么这个字符串为\(A+X+B\)组成,无论中间的\(X\)是重叠还 ...

  6. hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...

  7. CodeForces 1056E - Check Transcription - [字符串hash]

    题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Git新建本地分支与远程分支关联问题:git branch --set-upstream【转】

    本文转载自:http://blog.csdn.net/netwalk/article/details/21088405 Git新建本地分支与远程分支关联问题:git branch --set-upst ...

  2. iOS开发】之CocoaAsyncSocket使用

    本文介绍了CocoaAsyncSocket库中GCDAsyncSocket类的使用.粘包处理以及时间延迟测试. 一.CocoaAsyncSocket介绍 CocoaAsyncSocket中主要包含两个 ...

  3. ※交换排序(1)——快速排序(quick sort)

    快速排序使用分治策略(Divide and Conquer)来把一个序列分为两个子序列.步骤为: 从序列中挑出一个元素,作为"基准"(pivot). 把所有比基准值小的元素放在基准 ...

  4. js如何获取某id的子标签

    思路:根据id获取父对象,然后使用childNodes获取所有子对象数组,关键代码: document.getElementById(div_id).childNodes;   // 子对象数组 实例 ...

  5. bzoj 2069 [ POI 2004 ] ZAW —— 多起点最短路 + 二进制划分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2069 首先,对于和 1 相连的点,一定是从某个点出发,回到另一个点: 所以需要枚举起点和终点 ...

  6. la3211

    2-sat+二分... 每次二分答案然后连边2-sat...边要开到n*n 样例水得跟没有一样... #include<bits/stdc++.h> using namespace std ...

  7. Kaka's Matrix Travels

    http://poj.org/problem?id=3422 #include <stdio.h> #include <algorithm> #include <stri ...

  8. 【转】linux read 用法

    转自:http://www.cnblogs.com/iloveyoucc/archive/2012/04/16/2451328.html 1.基本读取 read命令接收标准输入(键盘)的输入,或其他文 ...

  9. 15年用canvas画的

    请恕我当年的工作太轻松,用canvas手打了一个图,技术含量并没有什么,现在看看,甚是怀念_(¦3」∠)_ <!DOCTYPE html> <html> <head&g ...

  10. (转)Vuex简单入门

    今天试了一下Vuex,感觉跟Redux的实现思想类似.再此,简单地总结一下. 什么是Vuex 在Vue中,多组件的开发给我们带来了很多的方便,但同时当项目规模变大的时候,多个组件间的数据通信和状态管理 ...