欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ1819


题意概括

  字符串a与字符串b的编辑距离是指:允许对a或b串进行下列“编辑”操作,将a变为b或b变为a,最少“编辑”次数即为距离。

  删除串中某个位置的字母;

  添加一个字母到串中某个位置;

  替换串中某一位置的一个字母为另一个字母;

  对于一个待查询字符串,如果它是单词,则返回-1;如果它不是单词,则返回字典中有多少个单词与它的编辑距离为1。


题解

  根据输入的单词构建trie,然后大力搜索即可。

  dfs,时间复杂度为m*20*20*20可以过去


代码

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=10005,L=20+5;
struct Trie{
int e,Next[26];
void init(){
e=0;
memset(Next,0,sizeof Next);
}
}tree[N*L];
int n,m,ans,trie_cnt,len,addv[N],mark;
char ch[L];
void build(char ch[],int bh){
int rt=1,t,len=strlen(ch);
for (int i=0;i<len;i++){
t=ch[i]-'a';
if (!tree[rt].Next[t])
tree[tree[rt].Next[t]=++trie_cnt].init();
rt=tree[rt].Next[t];
}
tree[rt].e=bh;
}
void init_trie(){
tree[1].init();
trie_cnt=1;
}
bool dfs(int pos,int rt,bool used){
if (pos>=len){
if (!used)
for (int i=0;i<26;i++)
if (tree[rt].Next[i])
if (dfs(pos,tree[rt].Next[i],1))
return 1;
if (tree[rt].e==0)
return 0;
if (!used)
return 1;
if (addv[tree[rt].e]!=mark)
ans++;
addv[tree[rt].e]=mark;
return 0;
}
int t=ch[pos]-'a';
if (used){
if (!tree[rt].Next[t])
return 0;
return dfs(pos+1,tree[rt].Next[t],1);
}
//not used
if (tree[rt].Next[t])
if (dfs(pos+1,tree[rt].Next[t],0))
return 1;
//add & change
for (int i=0;i<26;i++)
if (tree[rt].Next[i]){
int nxt=tree[rt].Next[i];
if (dfs(pos,nxt,1))
return 1;
if (t!=i)
if (dfs(pos+1,nxt,1))
return 1;
}
// delete
if (dfs(pos+1,rt,1))
return 1;
return 0;
}
int main(){
scanf("%d%d",&n,&m);
init_trie();
for (int i=1;i<=n;i++){
scanf("%s",ch);
build(ch,i);
}
memset(addv,0,sizeof addv);
for (int i=1;i<=m;i++){
mark=i;
scanf("%s",ch);
len=strlen(ch);
ans=0;
if (dfs(0,1,0)){
puts("-1");
continue;
}
printf("%d\n",ans);
}
return 0;
}

  

BZOJ1819 [JSOI]Word Query电子字典 Trie的更多相关文章

  1. [bzoj1819] [JSOI]Word Query电子字典

    正解是trie树...在树上跳来跳去什么的 然而在企鹅qq那题的影响下我写了hash... 添加一个字母到一个串,就相当于另一个串删对应位置上的字母. 改变某个位置上的字母,就相当于两个字符串删掉同一 ...

  2. 2786: [JSOI]Word Query电子字典

    2786: [JSOI]Word Query电子字典 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3  Solved: 3[Submit][Statu ...

  3. 1819: [JSOI]Word Query电子字典

    1819: [JSOI]Word Query电子字典 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 729  Solved: 238[Submit][S ...

  4. bzoj 1819: 电子字典 Trie

    题目: Description 人们在英文字典中查找某个单词的时候可能不知道该单词的完整拼法,而只知道该单词的一个错误的近似拼法,这时人们可能陷入困境,为了查找一个单词而浪费大量的时间.带有模糊查询功 ...

  5. 洛谷P4407 [JSOI2009]电子字典

    题目描述 人们在英文字典中查找某个单词的时候可能不知道该单词的完整拼法,而只知道该单词的一个错误的近似拼法,这时人们可能陷入困境,为了查找一个单词而浪费大量的时间.带有模糊查询功能的电子字典能够从一定 ...

  6. 改变word的语言字典

    改变word的语言字典 上周末看论坛有人提出否有方法用代码改变word的语言字典,因为默认的语言会影响现用语言输入的拼写器和其他校对工具.我们的Spire.doc正好支持,正好闲来无事所以我用西班牙语 ...

  7. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  8. Remember the Word,LA3942(Trie树+DP)

    Trie树基础题,记录下代码. #include <cstdio> #include <cstring> #define MaxNode 4005*100 #define No ...

  9. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

随机推荐

  1. [C++]Linux之Ubuntu下编译C程序出现错误:“ stray ‘\302'或者'\240' in program”的解决方案

    参考文献:[error: stray ‘\240’ in program或 error: stray ‘\302’ in program](http://blog.csdn.net/u01299585 ...

  2. 【摘】SVN提交与版本冲突

    一般性解决办法 1.要提交的内容备份到项目之外[为还原版本做准备] 2.还原[回到之前版本] 3.更新[更新版本号和版本] 4.填充内容[即 将自己之前备份的内容填充项目对应处] 5.提交 6.OK ...

  3. [C++]指针与引用(应用辨析)

    1.指针变量允许将一个整数经强制转换后赋值给指针变量    Eg:      float *fp;      fp = (float *)5000;//意义:将5000作为一个地址赋给指针变量fp 2 ...

  4. static extern

    /*主程序在a.c*/ //a.c #include <stdio.h> #include "b.h" main(){ printf ("%d\n" ...

  5. java Comparable 和 Comparator接口区别

    Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”.  即然实现Comparable接口的类支持排序,假设现在存在“实现C ...

  6. 【libreoffice】libreoffice实现office转pdf、html、jpg等格式数据

    其实libreoffice有好多功能,完全可以替代office 1.windows下将word转为pdf 1  安装libreoffice 到官网下载后安装即可.https://donate.libr ...

  7. JS执行一次任务与定期任务与清除执行

    1.一次性任务的执行与清除执行 1.定期执行 <script> timer = 0; timer = setTimeout(function() { console.log("s ...

  8. 基于URL的权限管理(三)

    思路:先创建一个专门的类ActiveUser用于存储用户登录的信息,主要用于存储用户id,账户,名称,菜单,权限. 认证拦截器主要是查看用户是否已登陆,如果没有转发到登陆界面,用户用账户跟密码登录时候 ...

  9. Debian ifconfig 命令找不到

    如何配置让 Debian 非特权用户也可以使用 ifconfig . ifconfig 在 /sbin 目录下,新建一个用户时, Debian 默认从 /etc/skel/ 复制配置文件, /sbin ...

  10. Protues常用元器件查找对应表

    原理图常用库文件:Miscellaneous Devices.ddbDallas Microprocessor.ddbIntel Databooks.ddbProtel DOS Schematic L ...