BZOJ 1212 HNOI 2004 L语言 Trie树
2015-08-07 16:11
363 查看
标题效果:给一些词。和几个句子,当且仅当句子可以切子可以翻译词典,这意味着该子将被翻译。
找到最长前缀长度可以被翻译。
思维:使用Trie树阵刷。你可以刷到最长的地方是最长的字符串可以翻译到的地方。
PS:在BZOJ上Trie竟然比AC自己主动机快。我的渣代码都刷到第一篇了。。
。
CODE:
找到最长前缀长度可以被翻译。
思维:使用Trie树阵刷。你可以刷到最长的地方是最长的字符串可以翻译到的地方。
PS:在BZOJ上Trie竟然比AC自己主动机快。我的渣代码都刷到第一篇了。。
。
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; struct Trie{ Trie *son[27]; bool end; Trie() { memset(son,NULL,sizeof(son)); end = false; } }*root = new Trie(); int words,cnt; char s[1 << 20|100],temp[20]; bool f[1 << 20|100]; inline void Insert(char *s) { Trie *now = root; while(*s != '\0') { if(now->son[*s - 'a'] == NULL) now->son[*s - 'a'] = new Trie(); now = now->son[*s - 'a']; ++s; } now->end = true; } inline void Ask(char *s,int i) { Trie *now = root; int t = 0; while(*s != '\0') { if(now->son[*s - 'a'] == NULL) return ; now = now->son[*s - 'a']; ++s; ++t; if(now->end) f[i + t] = true; } if(now->end) f[i + t] = true; } inline int Work() { memset(f,false,sizeof(f)); f[0] = true; int re = 0,length = strlen(s + 1); for(int i = 0; i <= length; ++i) { if(!f[i]) continue; re = i; Ask(s + i + 1,i); } return re; } int main() { cin >> words >> cnt; for(int i = 1; i <= words; ++i) { scanf("%s",temp); Insert(temp); } for(int i = 1; i <= cnt; ++i) { scanf("%s",s + 1); printf("%d\n",Work()); } return 0; }
相关文章推荐
- 【MVC 1】MVC+EF实体框架—原理解析
- 解压字符串
- 万年历农历程序(抄表法)
- 使用spring @Scheduled注解执行定时任务
- 迷宫问题(广搜 bfs)
- 【MVC 1】MVC+EF实体框架—原理解析
- datables插件ajax用法
- 实现placeholder属性的UITextView
- js 半角
- 分析及防护:Win10 执行流保护绕过问题
- poj-3080-Blue Jeans-串-暴力
- SLF4J: Class path contains multiple SLF4J bindings.
- 使用Gson工具的心得
- HDU 1242 BFS+优先队列
- iOS UIActivityIndicatorView活动指示器
- SQL RAISERROR 用法
- json与xml的比较
- hdu 4046 2011北京赛区网络赛G 线段树 ***
- selenium webdriver对iframe的处理
- Android测试框架或者工具对比