【题目链接】: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. cookie domain and cookie path

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie Domain=<domain-value> Opt ...

  2. Black Rock Shooter

    在人气动漫 Black Rock shooter 中,当加贺里对麻陶 说出了"滚回去"以后,与此同时,在另一个心灵世界里, BRS 也遭到了敌人的攻击.此时,一共有 n 个攻击排成 ...

  3. windows下 redis/tomcat 服务自启动

    //设置redis服务自启动 //根据个人配置执行语句.   redis-server --service-install redis.windows.conf --loglevel verbose ...

  4. vue学习记录(一)—— vue开发调试神器vue-devtools安装

    网上有些贴子少了至关重要的一步导致我一直没装上, 切记!!install后还需build,且install和build都在vue-devtools文件夹内执行 github下载地址 点击跳转 具体步骤 ...

  5. “浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 C-Cities

    题目描述:There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirec ...

  6. InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析

    InnoDB锁机制之Gap Lock.Next-Key Lock.Record Lock解析 有意思,解释的很好

  7. [Android]异常2-Unexpected error while executing

    异常原因: 可能一>Android Studio的自动编译没有成功 解决方法有: 解决一>菜单栏里的“Build”,“Clean Project” 注:

  8. Android基础TOP4:Tost的使用

    Activity: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  9. JS高级——Object.prototype成员

    基本概念 成员 描述 Object.prototype.__proto__ 指向当对象被实例化的时候,用作原型的对象. Object.prototype.hasOwnProperty() 返回一个布尔 ...

  10. Python标准库sys

    1.命令行参数sys.argv 我们从Python语言之模块第一部分的例子开始,看看sys.argv中到底存了些什么内容. #Filename: using_sys.py import sys i=0 ...