fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class A{
  8. map<char, A*> m;
  9. public:
  10. A(string s){add(s);};
  11. A(){};
  12. ~A(){
  13. for(auto& i:m)
  14. delete i.second;
  15. };
  16. void add(string s){
  17. if(s.empty()){
  18. m[0] = new A();
  19. }else{
  20. char a = s[0];
  21. string b = s.substr(1, s.length() - 1);
  22. if(exist(a))
  23. m[a]->add(b);
  24. else
  25. m[a] = new A(b);
  26. };
  27. };
  28.  
  29. bool exist(char a){
  30. return m.find(a) != m.end();
  31. };
  32.  
  33. bool check(string s){
  34. if(s.empty())
  35. return exist(0);
  36. if(exist(s[0]))
  37. return m[s[0]]->check(s.substr(1, s.length() - 1));
  38. else
  39. return false;
  40. };
  41.  
  42. void out(string s = ""){
  43. for(auto i:m){
  44. if(i.first)
  45. i.second->out(s + i.first);
  46. else
  47. cout<<s<<endl;
  48. };
  49. };
  50. };
  51.  
  52. int main()
  53. {
  54. A a;
  55. int n;
  56. string s;
  57. cin>>n;
  58. for(int i=0;i<n;i++){
  59. cin>>s;
  60. a.add(s);
  61. };
  62. a.out();
  63. cout<<a.check("temp")<<a.check("tem")<<a.check("temp1");
  64. return 0;
  65. };
Success #stdin #stdout 0s 3484KB
stdin
3
temp
test
123
stdout
123
temp
test
100