Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. string PATH = "D:\\pobrane\\studenci.txt";
  8. string wzorzec = "krolewsk";
  9. fstream instream;
  10. vector<vector<string>> tab;
  11. vector<int> pi;
  12. unordered_map<string, pair<int, int>> mp;
  13. int globId = 1;
  14.  
  15. void MakePi(){
  16.  
  17. pi.resize(wzorzec.size());
  18. int b = -1;
  19. pi[0] = -1;
  20. for(int i = 1; i < wzorzec.size(); i++){
  21.  
  22. while(b!=-1&&wzorzec[b]!=wzorzec[i]) b=pi[b];
  23.  
  24. b+=1;
  25. pi[i] = b;
  26. //cout<<b<<" ";
  27. }
  28. }
  29.  
  30. void parse(){
  31. instream.open(PATH);
  32. tab.resize(10000);
  33. string line;
  34. //parser
  35. getline(instream, line);
  36. while(getline(instream, line)){
  37. string tmp;
  38. for(int i =0 ; i < line.size(); i++){
  39. if(line[i]==';'){
  40. tab[globId].push_back(tmp);
  41. tmp.resize(0);
  42. }else tmp.push_back(line[i]);
  43. }
  44. tab[globId].push_back(tmp);
  45. globId++;
  46. //cout<<tab[globId-1][3]<<endl;
  47. }
  48. instream.close();
  49. }
  50.  
  51. bool kmp(string &s){
  52. int p = -1;
  53. int b = -1;
  54.  
  55. for(int i = 0; i < s.size(); i++){
  56. char c = s[i];
  57. if(c<90) c+=32;
  58. while(b>-1&&c!=wzorzec[b]) b = pi[b];
  59.  
  60. b++;
  61. if(b>=wzorzec.size()) return true;
  62. }
  63. return false;
  64. }
  65.  
  66. void process(){
  67. for(int i = 1; i < globId; i++){
  68. if(kmp(tab[i][3])){
  69. string nazwisko = tab[i][2];
  70. nazwisko.pop_back();
  71. //cout<<tab[i][6]<<endl;
  72. mp[nazwisko] = make_pair(mp[nazwisko].first+stoi(tab[i][6]), mp[nazwisko].second+1);
  73. }
  74. }
  75. }
  76.  
  77. int getResult(){
  78. int wyn = 0;
  79. for(unordered_map<string, pair<int, int>>::iterator it = mp.begin(); it!=mp.end(); it++){
  80. if((it->second).second>1) {
  81. wyn+=(it->second).first;
  82. cout<<it->first<<" "<<it->second.second<<" "<<wyn<<endl;
  83. }
  84. }
  85. return wyn;
  86. }
  87. int main()
  88. {
  89. MakePi();
  90. parse();
  91. process();
  92. cout<<getResult();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement