UOJ 13 [UER #1]跳蚤OS
2016-08-30 17:48
183 查看
trie树。
在trie上建每一条路径的字符串。节点加一个go指针,如果go不为NULL则说明他是快捷方式,并且go指向它指向的文件夹。然后找一找,连一连,就好了。
其实我觉得如果对于每个文件夹,把名字哈希之后视为一个节点,依然可做。但是太懒不想打哈希。
在trie上建每一条路径的字符串。节点加一个go指针,如果go不为NULL则说明他是快捷方式,并且go指向它指向的文件夹。然后找一找,连一连,就好了。
其实我觉得如果对于每个文件夹,把名字哈希之后视为一个节点,依然可做。但是太懒不想打哈希。
#include<cstdio> #include<cstring> using namespace std; char s[500005], t[500005]; struct node { node *next[30], *go, *fa; int v; node(){fa=go=NULL;memset(next,NULL,sizeof(next));} }; int pack(char c) { if(c=='/')return 26; else return c-'a'; } char unpack(int c) { if(c==26)return '/'; else return c+'a'; } struct TRIE { node *root; void init() { root= new node; } node* inser(char *s) { node *p=root; for(int i = 0; s[i]; i++) { int c=pack(s[i]); if(!p->next[c]) { p->next[c]=new node; p->next[c]->fa=p; p->next[c]->v=c; } p=p->next[c]; if(c==26 && p->go) p=p->go; } if(p->v!=26) { if(!p->next[26]) { p->next[26]=new node; p->next[26]->fa=p; p->next[26]->v=26; } p=p->next[26]; if(p->go)p=p->go; } return p; } }trie; int main() { int n, m; scanf("%d%d",&n,&m); trie.init(); for(int i = 1; i <= n; i++) { scanf("%s%s",s,t); node *p=trie.inser(s); node *q=trie.inser(t); p->go=q; } for(int i = 1; i <= m; i++) { scanf("%s",s); node *p=trie.inser(s); if(p->fa!=trie.root)p=p->fa; int j=0; while(p!=trie.root) { t[j++]=unpack(p->v); p=p->fa; } for(int k = j-1; k >= 0; k--) printf("%c",t[k]); puts(""); } }
相关文章推荐
- UOJ 13|【UER #1】跳蚤OS|字典树
- UOJ_14_【UER #1】DZY Loves Graph_并查集
- uoj #14.【UER #1】DZY Loves Graph
- UOJ 12 [UER #1]猜数
- UOJ187[UR#13]Ernd(斜率优化DP)
- UOJ #188. 【UR #13】Sanrd
- UOJ 12|【UER #1】猜数|数学
- uoj#187. 【UR #13】Ernd
- ProjectEuler 13
- Linux IPC实践(13) --System V IPC综合实践
- 二维数组练习13_图像模糊处理
- java学习笔记13--比较器(Comparable、Comparator)
- chapte13:学习shell scripts之(2)判断式的使用
- Android Api demo系列(13) (App>Fragment>FragmentAlertDialog)
- 通过链表实现,13个人围成一圈,从第一个开始顺序报号1、2、3.凡报到3者推出圈子,找出打印并最后人的序号
- 深入理解BootStrap 缩略图(thumbnail)与警示窗(alert)13
- 用Python和Pygame写游戏-从入门到精通(13)简单的AI游戏
- NYOJ 13 Fibonacci数
- Uoj#35. 后缀排序
- ApiDemos学习知识点之os-MorseCode(13)