【题意】给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个字符不同。(字符串总长度为6·105),所有字符只有a,b,c。

【题解】因为只有三种字符,用Trie最合适。先把n个字符串插入到Trie中。然后每读入一个字符串,首先枚举差异字符的位置,依次替换为另外两种字符,并检查是否合法。如果合法就直接输出YES。

显然每替换一个字符就从头检查一遍太浪费时间,所以保存字典树中差异字符位置的指针,每次从该位置开始检查即可。

我一开始是直接处理n个字符串,枚举差异字符的位置,分别替换后加入到字典树中。但是这样做会耗费相当多的空间,空间复杂度是指数级别(3^len)。MLE或者RE。但是一直返回的是WA7,QAQ找了半天错。。

还有一种方法时使用字符串哈希,但是如果运气不好的话,会出现冲突,然后WA掉,这次比赛很多人跪在了这里

#include<bits/stdc++.h>
#define eps 1e-9
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 1000005
#define MAXM 40005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
int i,j,k,n,m,x,y,T,ans,big,cas,num,len;
bool flag;
char s[]; struct Trie
{
int ch[][];
int val[];
int size;
Trie()
{
size=;
memset(ch[],,sizeof(ch[]));
val[]=;
val[]=;
} int idx(char c)
{
return c-'a';
} void insert(char *s,int v)
{
int u=,len=strlen(s);
for (int i=;i<len;i++)
{
int c=idx(s[i]);
if (!ch[u][c])
{
memset(ch[size],,sizeof(ch[size]));
val[size]=;
ch[u][c]=size++;
}
u=ch[u][c];
}
val[u]=v;
} bool exist(char *s)
{
int u=,len=strlen(s);
for (int i=;i<len;i++)
{
int c=idx(s[i]); for (int j=;j<;j++)
{
if (c==j || ch[u][j]==) continue;
int u2=ch[u][j];
flag=true;
for (int k=i+;k<len;k++)
{
int c=idx(s[k]);
if (!ch[u2][c])
{
flag=false;
break;
}
u2=ch[u2][c];
}
if (flag && val[u2])
{
return true;
}
} if (!ch[u][c])
{
return false;
}
u=ch[u][c];
}
return false;
}
} tree; int main()
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
{
scanf("%s",s);
tree.insert(s,);
}
for (i=;i<=m;i++)
{
scanf("%s",s);
if (tree.exist(s)) puts("YES");
else puts("NO");
} return ;
}

Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串的更多相关文章

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

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

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

  3. 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...

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

  5. 数学 Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

    题目传送门 /* 水题,就是用三点共线的式子来判断射击次数 */ #include <cstdio> #include <cmath> #include <string& ...

  6. 贪心/字符串处理 Codeforces Round #291 (Div. 2) A. Chewbaсca and Number

    题目传送门 /* WA了好几次,除了第一次不知道string不用'\0'外 都是欠考虑造成的 */ #include <cstdio> #include <cmath> #in ...

  7. Codeforces Round #291 (Div. 2)

    A 题意:给出变换规则,单个数字t可以变成9-t,然后给出一个数,问最小能够变成多少. 自己做的时候理解成了不能输出前导0,但是题目的本意是不能有前导0(即最高位不能是0,其余位数按照规则就好) 55 ...

  8. Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]

    传送门 D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

    因为是x,y均为整数因此对于同一直线的点,其最简分数x/y是相同的(y可以为0,这里不做除法)于是将这些点不断求最简分数用pair在set中去重即可. #include <cmath> # ...

随机推荐

  1. sphinx (coreseek)——3、区段查询 与 增量索引实例

    首先本文测试数据100多万的域名的wwwtitle 信息  检索数据: 首先建立临时表格: CREATE TABLE `sph_counter` ( `index_id` ) NOT NULL, `m ...

  2. 解决Silverlight5_tools无法安装问题(试验已成功)

    当前位置: 银光首页 > Silverlight > Silverlight学习教程 > 命令:regedit 打开节点:HKEY_LOCAL_MACHINE\SOFTWARE\Mi ...

  3. Linux服务器集群技术的概述

    目前,越来越多的网站采用Linux操作系统,提供邮件.Web.文件存储.数据库等服务.也有非常多的公司在企业内部网中利用Linux服务器提供这些服务.随着人们对Linux服务器依赖的加深,对其可靠性. ...

  4. ssh远程登录Ubuntu报错:Permission denied, please try again.

    ssh到server上的时候密码是对的但是报如下信息:# ssh 172.16.81.221root@172.16.81.221's password:Permission denied, pleas ...

  5. easyui_tree 复选框 动态加载树

    controller动态获取单位用户树 #region 下拉树菜单 /// <summary> /// 获取工作人员树菜单 /// </summary> /// <par ...

  6. Unity3D Quaternion各属性和函数测试

    Quaternion属性与方法 一,属性: x.y.z就不说了,只看一个eulerAngles,代码如下: public Quaternion rotation = Quaternion.identi ...

  7. SKYLINE

    uvalive4108:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  8. Delphi XE6 for Android 让手机震动(调用Java的函数)

    震动,是调用了 安卓api  JNI 里面的 函数  ,这些都是 调用java的 ,如下面的引用, uses  FMX.Helpers.Android,  Androidapi.JNI.App,  A ...

  9. ASP.NET服务器推送及前后台实时交互

    一:项目需求 页面需要做一个上传Excel功能,并处理Excel中的数据到数据库中 这是一个较为漫长的过程 这时我需要在页面上显示处理进度的提示,给用户一个良好的体验 比如给用户提示“正在上传文档”, ...

  10. mysql 安装1

    Linux 安装mysql.tar.gz包(2012-09-28 19:25:06) 标签: it 分类: linux学习编 我用的mysql的版本的是:mysql--linux-i686-icc-g ...