hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
2015-08-05 16:31
666 查看
题目传送门
/* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难。另外,我想用64位的自然溢出wa了,不清楚 */ /************************************************ * Author :Running_Time * Created Time :2015-8-5 13:05:49 * File Name :D.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 6e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int KEY = 257; char s[MAXN]; set<ll> S; ll ha[MAXN]; int n, m; void init(void) { ha[0] = 1; for (int i=1; i<MAXN; ++i) ha[i] = ha[i-1] * KEY % MOD; } ll get_hash(char *s) { int len = strlen (s); ll res = 0; for (int i=0; i<len; ++i) { res = (res * KEY + s[i]) % MOD; } return res; } bool judge(char *s) { int len = strlen (s); ll h = get_hash (s); for (int i=0; i<len; ++i) { for (ll ch='a'; ch<='c'; ++ch) { if (ch == s[i]) continue; if (S.find ((((ch-s[i]) * ha[len-i-1] + h) % MOD + MOD) % MOD) != S.end ()) return true; } } return false; } int main(void) { //Codeforces Round #291 (Div. 2) C. Watto and Mechanism init (); while (scanf ("%d%d", &n, &m) == 2) { S.clear (); for (int i=1; i<=n; ++i) { scanf ("%s", s); S.insert (get_hash (s)); } for (int i=1; i<=m; ++i) { scanf ("%s", s); if (judge (s)) puts ("YES"); else puts ("NO"); } } return 0; }
相关文章推荐
- GreenMail邮件测试服务器
- 【C++】struct和class
- 控制打印页面某一部分
- Linux中的ipcs命令与ipcrm命令
- 最新的maven国内镜像使用手册
- poj 2456 最大化最小值
- Idea新建SpringMVC报错原因
- 关于JQuery加载本地JSON文件无法读取(权限不足的)解决
- mysql时间——查询上周、月、季度、年和本周、月、季度、年
- error C2381 “exit” 重定义;__declspec(noreturn) 不同
- ios错误ignoring file xxx missing required architecture x86_64 in file
- oracle数据库导入导出
- 使用PowerPoint输出高质量图片
- hadoop2.6.0执行自带wordcount出现异常
- js页面传值的实现
- Windows BAT
- node.js async 几个函数
- oracle initialization or shutdown in progress解决方法
- PowerMock简单使用
- 位运算 - 异或求和