Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #include<array>
- #include<cstring>
- using namespace std;
- struct node{
- using nptr=node*;
- nptr next[128];
- int cnt;
- node():cnt(0){
- memset(next,0,sizeof(nptr)*128);
- }
- };
- using nptr=node*;
- struct trie{
- nptr root;
- void insert(const string& str){
- nptr cur=root;
- for(const auto& i:str){
- if(!isalnum(i)) continue;
- if(!cur->next[int(i)])
- cur->next[int(i)]=new node;
- cur=cur->next[int(i)];
- }
- cur->cnt++;
- }
- void dfs(nptr cur){
- if(!cur) return;
- static string str;
- if(cur->cnt)
- cout<<str<<" : "<<cur->cnt<<endl;
- for(int i=0;i<128;i++){
- str.push_back(i);
- dfs(cur->next[i]);
- str.pop_back();
- }
- }
- trie():root(new node){}
- };
- #include<fstream>
- int main(){
- // 在執行檔同資料夾放進input.txt
- // 執行之後輸出會在output.txt
- fstream fin("input.txt",ios::in);
- fstream fout("output.txt",ios::out);
- cout.rdbuf(fout.rdbuf());
- trie tree;
- string str;
- while(fin>>str)
- tree.insert(str);
- tree.dfs(tree.root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment