【题目链接】: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. 【CodeForces】166'E

    166’E Tetrahedron You are given a tetrahedron. Let’s mark its vertices with letters A, B, C and D co ...

  2. Jsp内置对象和EL隐藏(内置)对象

      JSP中的内置对象一共有九个, 由于有的不太常用, 所以总是记不住, 从Sun公司的网站上找到的PDF文档, 把这一部分放在这里, 以备随时查用:     JSP九个内置对象: Implicit ...

  3. ajax接收处理json格式数据

    ajax在前后端的交互中应用非常广泛,通过请求后台接口接收处理json格式数据展现在前端页面. 下面我们来简单用 ajax在本地做一个接收并处理json的小例子 首先我们要新建一个叫做data的jso ...

  4. 56. EditorGridPanel和渲染器renderer的使用

    转自:https://blog.csdn.net/chenkangwan/article/details/4748716?utm_source=blogxgwz6 1. 在EditorGriddPan ...

  5. ognl表达式注意事项

    1.在jsp页面中: <s:a action="departmentAction_delete.action?did="></s:a> 说明:   1.st ...

  6. 异常强大的Markdown编辑插件-Markdown Preview Enhanced

    最近使用Markdown写作,了解到以下这些Markdown写作工具 MaHua 在线markdown编辑器 百度搜索Markdown时,它排在非常靠前的位置 马克飞象- 专为印象笔记打造的Markd ...

  7. Tornado异步模式

    先介绍下背景:由于工作需要,前段时间又写了一段爬虫去获取和更新一些数据.之前爬虫主要用Scrapy框架批量爬取一些页面数据,或者用Gevent调用目标站点的接口.偶然看到了Tornado,听说这个框架 ...

  8. POJ 1873 计算几何

    思路: 二进制枚举一下要删哪些点 求个凸包,算一下贡献 //By SiriusRen #include <cmath> #include <cstdio> #include & ...

  9. springMVC是什么等七个问题

  10. EF 新增数据时提示it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element

    it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionM ...