Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <fstream>
- using namespace std;
- string PATH = "D:\\pobrane\\studenci.txt";
- string wzorzec = "krolewsk";
- fstream instream;
- vector<vector<string>> tab;
- vector<int> pi;
- unordered_map<string, pair<int, int>> mp;
- int globId = 1;
- void MakePi(){
- pi.resize(wzorzec.size());
- int b = -1;
- pi[0] = -1;
- for(int i = 1; i < wzorzec.size(); i++){
- while(b!=-1&&wzorzec[b]!=wzorzec[i]) b=pi[b];
- b+=1;
- pi[i] = b;
- //cout<<b<<" ";
- }
- }
- void parse(){
- instream.open(PATH);
- tab.resize(10000);
- string line;
- //parser
- getline(instream, line);
- while(getline(instream, line)){
- string tmp;
- for(int i =0 ; i < line.size(); i++){
- if(line[i]==';'){
- tab[globId].push_back(tmp);
- tmp.resize(0);
- }else tmp.push_back(line[i]);
- }
- tab[globId].push_back(tmp);
- globId++;
- //cout<<tab[globId-1][3]<<endl;
- }
- instream.close();
- }
- bool kmp(string &s){
- int p = -1;
- int b = -1;
- for(int i = 0; i < s.size(); i++){
- char c = s[i];
- if(c<90) c+=32;
- while(b>-1&&c!=wzorzec[b]) b = pi[b];
- b++;
- if(b>=wzorzec.size()) return true;
- }
- return false;
- }
- void process(){
- for(int i = 1; i < globId; i++){
- if(kmp(tab[i][3])){
- string nazwisko = tab[i][2];
- nazwisko.pop_back();
- //cout<<tab[i][6]<<endl;
- mp[nazwisko] = make_pair(mp[nazwisko].first+stoi(tab[i][6]), mp[nazwisko].second+1);
- }
- }
- }
- int getResult(){
- int wyn = 0;
- for(unordered_map<string, pair<int, int>>::iterator it = mp.begin(); it!=mp.end(); it++){
- if((it->second).second>1) {
- wyn+=(it->second).first;
- cout<<it->first<<" "<<it->second.second<<" "<<wyn<<endl;
- }
- }
- return wyn;
- }
- int main()
- {
- MakePi();
- parse();
- process();
- cout<<getResult();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement