codeforce 858D Polycarp's phone book(字典树)
来源:互联网 发布:mac建立无线局域网 编辑:程序博客网 时间:2024/05/23 19:19
直接用数组静态建树,把每一个字符串的每一个后缀加入到字典树中 这样就能遍历到每一个子串109ms
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 500000+10;typedef long long LL;char str[N][11], a[11];int ok[N*6], num[N*6], ch[N*6][11], sum[N*6];int main(){ int n, m=0; scanf("%d", &n); for(int i=0;i<n;i++) { scanf("%s",a); int l=0; for(int j=0;j<9;j++) { int k=0; for(int p=j;p<9;p++) { int x=a[p]-'0'; if(!ch[k][x]) ch[k][x]=++m; k=ch[k][x]; if(!ok[k]) num[l++]=k,ok[k]=1, sum[k]++; } } for(int j=0;j<l;j++) ok[num[j]]=0; for(int j=0;j<9;j++) str[i][j]=a[j]; } for(int i=0;i<n;i++) { int mint=11, s=0, t=9; for(int j=0;j<9;j++) { int k=0; for(int p=j;p<9;p++) { k=ch[k][str[i][p]-'0']; if(sum[k]==1&&p-j+1<mint) { mint=p-j+1,s=j,t=p; break; } } } for(int j=s;j<=t;j++) putchar(str[i][j]); puts(""); } return 0;}
自己写的大暴力 时限4s 跑3.961s刚过去了。。。
#include <iostream>#include <bits/stdc++.h>using namespace std;const int N = 1000000+10;typedef long long LL;char str[70000+10][20];string ss, sx;unordered_map<string,int>q,qx;int main(){ int t; scanf("%d", &t); q.clear(); for(int i=0;i<t;i++) { scanf("%s",str[i]); ss=str[i]; qx.clear(); for(int k=1;k<=9;k++) { for(int j=0;j+k-1<9;j++) { sx=ss.substr(j,k); //cout<<sx<<endl; if(!qx.count(sx)) qx[sx]=1,q[sx]+=1; } } } for(int i=0;i<t;i++) { ss=str[i]; int flag=0; for(int k=1;k<=9;k++) { for(int j=0;j+k-1<9;j++) { sx=ss.substr(j,k); //cout<<sx<<endl; //cout<<q[sx]<<endl; if(q[sx]==1) { for(int h=j;h<=j+k-1;h++) printf("%c",str[i][h]); printf("\n"); flag=1; break; } } if(flag) break; } } return 0;}
阅读全文
0 0
- codeforce 858D Polycarp's phone book(字典树)
- 858D Polycarp's phone book 字典树
- codeforces 858D Polycarp's phone book(字典树)
- Codeforces Round #434 D. Polycarp's phone book (字典树)
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- Codeforces Round #434 (Div. 2) D Polycarp's phone book(字符串,字典树)
- Codeforces 861 D Polycarp's phone book(字典树模板)
- Codeforces Round #434 Polycarp's phone book (字典树)
- CodeForces 858D Polycarp's phone book(Trie)
- Polycarp's phone book
- Codeforces 861 D. Polycarp's phone book (trie)
- Codeforces858D Polycarp's phone book
- Codeforces Round #434 (Div. 2, )-字典树&好题&板子-Polycarp's phone book
- CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技
- cf434 B. Polycarp's phone book
- ACM 字典树 Phone List & Hat’s Words
- codeforce 118D Caesar's Legions
- Codeforce 118D: Caesar's Legions
- Xcode 9
- 分页时不再第一页重新搜索显示无记录
- ****dirname(__FILE__) 绝对路径问题二
- 关于java反射机制的理解(转自知乎)
- 苹果osx系统切换中文
- codeforce 858D Polycarp's phone book(字典树)
- HTTP基本原理解析
- 查看python是32位还是64位
- ChannelHandler和ChannelPipeline
- Numpy 属性
- selenium多表单切换
- HGDB查看当前会话的pid
- plsql连接oracle
- Kali linux渗透测试(四)