UVA 1462 or UVALive 4769 Fuzzy Google Suggest Trie树上的dfs 2009年哈尔滨G题
来源:互联网 发布:mac os 最新版本号 编辑:程序博客网 时间:2024/05/29 10:11
题目大意:
就是现在给出至多30W个长度不超过10的串, 然后对于m <= 300次询问, 每次进行模糊匹配问满足条件的串有多少个
模糊匹配S和T的距离<=DIS(给定0 <= DIS <= 2)的算匹配成功
S和T的距离是有S变成T需要进行的最少操作数, 一次操作可以是将S删去一个字符或者插入一个字符或者将其中某个字符替换成其他字母
大致思路:
就是对于30W个串建立Trie树之后进行dfs搜索就可以了
刚开始在UVALive上交一直Runtime Error... 实在找不到错误了之后去UVA上找一样的题 交了就过了, 我把VJ上以前AC UVALive上这题的代码交上去也都RE了...可能是UVALive最近抽了吧....但是在UVA 1462上交还是能正常通过了
代码如下:
Result : Accepted Memory : ? KB Time : 992 ms
/* * Author: Gatevin * Created Time: 2015/5/7 10:19:37 * File Name: Rin.cpp *///#pragma comment(linker, "/STACK:16777216")#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)#define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endlstruct Trie{ int next[3000010][26], end[3000010]; int vis[3000010], mark[3000010]; int L, root; int newnode() { memset(next[L], -1, sizeof(next[L])); vis[L] = 0; mark[L] = 0; end[L++] = 0; return L - 1; } void init() { L = 0; root = newnode(); } void insert(char *s) { int now = root; int len = strlen(s); for(int i = 0; i < len; i++) { if(next[now][s[i] - 'a'] == -1) next[now][s[i] - 'a'] = newnode(); now = next[now][s[i] - 'a']; end[now]++; } } void dfs(int now, char *s, int dis, int cas)//dis表示当前还剩下几次可以变更, cas表示是第几次搜索 { if(*s == '\0') { vis[now] = cas;//标记为某个模糊匹配能到达的位置 return; } mark[now] = cas;//标记能走的路径 if(dis == 0) { if(next[now][*s - 'a'] != -1) dfs(next[now][*s - 'a'], s + 1, dis, cas); return; } if(next[now][*s - 'a'] != -1) dfs(next[now][*s - 'a'], s + 1, dis, cas);//不变化 for(int i = 0; i < 26; i++) if(i != *s - 'a')//替换 if(next[now][i] != -1) dfs(next[now][i], s + 1, dis - 1, cas); dfs(now, s + 1, dis - 1, cas);//删除 for(int i = 0; i < 26; i++) if(next[now][i] != -1) dfs(next[now][i], s, dis - 1, cas);//添加 return; } int ans; void die(int now, int cas) { if(vis[now] == cas)//模糊匹配能匹配到这个分支下的全部, 不用继续遍历 { ans += end[now]; return; } if(mark[now] != cas) return;//模糊匹配没有到达过这里 for(int i = 0; i < 26; i++) if(next[now][i] != -1) die(next[now][i], cas); return; } void solve(char *s, int dis, int cas) { dfs(root, s, dis, cas); ans = 0; die(root, cas); printf("%d\n", ans); }};Trie trie;char in[20];int main(){ int n; while(scanf("%d", &n) != EOF) { trie.init(); for(int i = 1; i <= n; i++) scanf("%s", in), trie.insert(in); int m, dis; scanf("%d", &m); for(int cas = 1; cas <= m; cas++) { scanf("%s %d", in, &dis); trie.solve(in, dis, cas); } } return 0;}
0 0
- UVA 1462 or UVALive 4769 Fuzzy Google Suggest Trie树上的dfs 2009年哈尔滨G题
- UVALive 4769 Fuzzy Google Suggest Trie树
- UVA 1462 Fuzzy Google Suggest
- UVA 1462 - Fuzzy Google Suggest(字典树+dfs)
- uva 1462 - Fuzzy Google Suggest(字典树+dfs)
- Fuzzy Google Suggest [未完成]
- uva1462(Fuzzy Google Suggest)
- Fuzzy Google Suggest acm@hit2888
- POJ 1816 Wild Words(trie 树上的DFS)
- google suggest的实现
- UVA 1267 && LA 3902 Network (思路--树上的DFS)
- uva12186 树上的dfs
- UVALive (LA) - 3942 UVa 1401 Trie
- SCU2016-04 G题 01trie树第k大,树上异或路径
- google suggest
- Trie树上dfs跑 与 或 异或
- 关于Google的Suggest功能的实现
- 关于Google的Suggest功能的实现
- javascript事件绑定
- C语言typedef 关键字
- C语言结构体必知必会
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- 【Android应用开发技术:用户界面】其他杂项组件
- UVA 1462 or UVALive 4769 Fuzzy Google Suggest Trie树上的dfs 2009年哈尔滨G题
- Java学习笔记之 File构造方法
- 黑马程序员——Java基础——对象序列化、管道流、RandomAccessFile类、操作基本数据类型的流对象、字符编码
- 新版JQuery Mobile返回按钮
- P122.17
- input IDC
- [leetcode]Balanced Binary Tree
- Getting iWork for Free on Older Macs
- hdu 1423 Greatest Common Increasing Subsequence(最长公共递增子序列lcis)