Codeforces Round #434 (Div. 2) D Polycarp's phone book(字符串,字典树)
2017-09-20 16:48
483 查看
其实是个暴力题..
枚举所有的九位子串,检查它是否会只在某个子串中出现.
具体实现方法是,把所有原串和他们的所有后缀存入字典树,进行一些标记,记录一个边被不同原串经历的次数.
然后就可以枚举遍历.
D题还是不看题解做不出,看了题解也不能瞬间搞懂..
代码强度也很高.
容易re,字典树放大一点.
(不懂啊,为什么70还会RE啊??)
枚举所有的九位子串,检查它是否会只在某个子串中出现.
具体实现方法是,把所有原串和他们的所有后缀存入字典树,进行一些标记,记录一个边被不同原串经历的次数.
然后就可以枚举遍历.
D题还是不看题解做不出,看了题解也不能瞬间搞懂..
代码强度也很高.
容易re,字典树放大一点.
(不懂啊,为什么70还会RE啊??)
/* xzppp */ #include <iostream> #include <vector> #include <cstdio> #include <string.h> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <list> #include <string> #include <cmath> #include <bitset> #include <iomanip> using namespace std; #define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout); #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define MP make_pair #define PB push_back #define _ %MOD #define MST(x) memset((x),0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int > pii; typedef pair<LL,LL> pll; typedef pair<double,double > pdd; typedef pair<double,int > pdi; const int MAXN = 10000+17; const int MAXM = 1e6+17; const int MAXV = 2*1e3+17; const int BIT = 15+3; const int INF = 0x7f7f7fff; const LL INFF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9+7; int pid = 1; struct dicn { int num,son[10],last,tms; }dic[MAXN*80]; string ans[7*MAXN]; void insert(string str,int id) { int now = 0; for (int i = 0; i < str.length(); ++i) { if(dic[now].son[str[i]-'0']==0) dic[now].son[str[i]-'0'] = pid++; int nxt = dic[now].son[str[i]-'0']; if(dic[nxt].last!=id) { dic[nxt].tms++; dic[nxt].last = id; } now = nxt; } } void dfs(int rt,string now) { for (int i = 0; i <= 9; ++i) { int nxt = dic[rt].son[i]; if(nxt!=0) { if(dic[nxt].tms==1) if(ans[dic[nxt].last].length()>now.length()+1||ans[dic[nxt].last].length()==0) ans[dic[nxt].last] = now+(char)('0'+i); dfs(nxt,now+(char)(i+'0')); } } } int main() { #ifndef ONLINE_JUDGE FFF #endif int n; while(cin>>n) { for (int i = 0; i < n; ++i) { string temp; cin>>temp; for (int j = 0; j < temp.size(); ++j) insert(temp.substr(j),i+1); } dfs(0,""); for (int i = 1; i <= n; ++i) cout<<ans[i]<<endl; } return 0; }
相关文章推荐
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- Codeforces 861 D Polycarp's phone book(字典树模板)
- Codeforces Round #434 (Div. 2, )-字典树&好题&板子-Polycarp's phone book
- codeforces 858D Polycarp's phone book(字典树)
- Codeforces 861D - Polycarp's phone book 字典树/hash
- Codeforces Round #434 D. Polycarp's phone book (字典树)
- Codeforces Round #434 Polycarp's phone book (字典树)
- 【Codeforces Round #434 (Div. 1) B】Polycarp's phone book
- codeforce 858D Polycarp's phone book(字典树)
- CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技
- Codeforces 861 D. Polycarp's phone book (trie)
- Codeforces858D Polycarp's phone book
- Codeforces Round #434 (Div.2) - D - Polycarp's phone book
- Polycarp's phone book
- CodeForces 858D Polycarp's phone book(Trie)
- NEU1007: English Game 字典树排序 字符串处理
- Codeforces#449 (Div. 2)C 字符串递归处理
- 2016 UESTC Training for Search Algorithm & String H - 中二少女与字符串 Trie 字典树
- python字符串的操作——python cookbook
- Python Cookbook 第二版 汉化版 [Recipe 1.9] 简化字符串 translate 方法的用法