fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. using namespace std;
  5. #define ALPHABETSIZE 26
  6. class TrieNode{
  7. public:
  8. string t;
  9. int val;
  10. TrieNode *edges[ALPHABETSIZE];
  11. TrieNode(){
  12. val=-1;
  13. for(int i=0;i<ALPHABETSIZE;i++) edges[i]=NULL;
  14. }
  15. };
  16.  
  17. class Trie{
  18. private:
  19. TrieNode *root;
  20. public:
  21. void insert(string s,string t);
  22. TrieNode *update(string s);
  23. Trie(){
  24. root=new TrieNode();
  25. }
  26. };
  27.  
  28. void Trie::insert(string s,string t){
  29. TrieNode *tmp=root;
  30. for(int i=0;s[i];i++){
  31. if(s[i]<'a' || s[i]>'z') continue;
  32. if(!tmp->edges[s[i]-'a']){
  33. TrieNode* newNode=new TrieNode();
  34. tmp->edges[s[i]-'a']=newNode;
  35. }
  36. tmp=tmp->edges[s[i]-'a'];
  37. }
  38. tmp->t=t;
  39. tmp->val=0;
  40. //cout<<tmp->t<<endl;
  41. }
  42.  
  43. TrieNode* Trie::update(string s){
  44. TrieNode *tmp=root;
  45. for(int i=0;s[i];i++){
  46. //cout<<s[i]<<endl;
  47. if(s[i]<'a' || s[i]>'z') continue;
  48. if(!tmp->edges[s[i]-'a']) return NULL;
  49. tmp=tmp->edges[s[i]-'a'];
  50. //cout<<tmp->val<<endl;
  51. }
  52. if(tmp->val==-1) return NULL;
  53. tmp->val+=1;
  54. return tmp;
  55. }
  56.  
  57. int main() {
  58. int n;
  59. cin>>n;
  60. Trie *tr=new Trie();
  61. string s,t;
  62. char ch;
  63. ch=getchar();
  64. for(int i=0;i<n;i++){
  65. getline(cin,s);
  66. getline(cin,t);
  67. //cout<<s<<'\t'<<t<<endl;
  68. transform(s.begin(), s.end(), s.begin(), ::tolower);
  69. tr->insert(s,t);
  70. }
  71. int m;
  72. cin>>m;
  73. ch=getchar();
  74. //cout<<m;
  75. int max=0;
  76. string maxVotes;
  77. bool flag=false;
  78. for(int i=0;i<m;i++){
  79. getline(cin,s);
  80. transform(s.begin(), s.end(), s.begin(), ::tolower);
  81. //cout<<s<<endl;
  82. TrieNode *tmp=tr->update(s);
  83. if(tmp==NULL) continue;
  84. int count=tmp->val;
  85. //cout<<count<<endl;
  86. if(max<count){maxVotes=tmp->t; max=count; flag=false;}
  87. else if(max==count) flag=true;
  88. }
  89. if(flag==true) cout<<"tie"<<endl;
  90. else cout<<maxVotes<<endl;
  91. return 0;
  92. }
Success #stdin #stdout 0s 3436KB
stdin
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
Marilyn Manson
loo
Marilyn Manson
Marilyn Manson
Marilyn Manson
Marilyn Manson
stdout
Rhinoceros