-
Notifications
You must be signed in to change notification settings - Fork 0
/
Taula.cc
132 lines (120 loc) · 3.81 KB
/
Taula.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "Taula.hh"
#include <algorithm>
using namespace std;
typedef unordered_map<string,int> MSI;
typedef pair<string,int> PSD;
typedef vector<PSD> VPSD;
bool ordenacio(PSD a, PSD b){
if(a.second==b.second){
if(a.first.size()==b.first.size()){
return a.first<b.first;
}
return a.first.size()<b.first.size();
}
return a.second>b.second;
}
Taula::Taula(){
}
void Taula::afegir_un(string s) {
if(aparicions_string.find(s)==aparicions_string.end()){
aparicions_string[s]=1;
return;
}
aparicions_string[s]+=1;
return;
}
void Taula::replace_strings(string actual, string seguent) {
if(aparicions_string.find(actual)==aparicions_string.end()) return;
int auxiliar=aparicions_string[actual];
aparicions_string.erase(actual);
if(aparicions_string.find(seguent)==aparicions_string.end()){
aparicions_string[seguent]=auxiliar;
bool found=false;
int i=0;
while(not found){
if(freq_string[i].first==actual){
freq_string[i].first=seguent;
found=true;
if(i!=0 and freq_string[i-1].second<=freq_string[i].second){
int j=i;
while(j!=0 and ((freq_string[j-1].second<freq_string[i].second) or (freq_string[j-1].second==freq_string[j].second and (freq_string[j-1].first.size()>freq_string[j].first.size() or (freq_string[j-1].first.size()==freq_string[j].first.size() and ((freq_string[j-1].first)>(freq_string[j].first))))))){
PSD auxiliar;
auxiliar=freq_string[j-1];
freq_string[j-1]=freq_string[j];
freq_string[j]=auxiliar;
--j;
}
}
if(i!=freq_string.size()-1 and freq_string[i+1].second==freq_string[i].second){
int j=i;
while(j!=freq_string.size()-1 and freq_string[j+1].second==freq_string[j].second and (freq_string[j+1].first.size()<freq_string[j].first.size() or (freq_string[j+1].first.size()==freq_string[j].first.size() and ((freq_string[j+1].first) < (freq_string[j].first))))){
PSD auxiliar;
auxiliar=freq_string[j+1];
freq_string[j+1]=freq_string[j];
freq_string[j]=auxiliar;
++j;
}
}
}
++i;
}
return;
}
aparicions_string[seguent]+=auxiliar;
int freq;
bool found=false;
int i=0;
while(not found){
if(freq_string[i].first==actual){
found=true;
freq=freq_string[i].second;
freq_string.erase(freq_string.begin()+i);
}
++i;
}
found=false;
i=0;
while(not found){
if(freq_string[i].first==seguent){
freq_string[i].second+=freq;
found=true;
if(i!=0 and freq_string[i-1].second<=freq_string[i].second){
int j=i;
while(j!=0 and ((freq_string[j-1].second<freq_string[j].second) or (freq_string[j-1].second==freq_string[j].second and (freq_string[j-1].first.size()>freq_string[j].first.size() or (freq_string[j-1].first.size()==freq_string[j].first.size() and ((freq_string[j-1].first)>(freq_string[j].first))))))){
PSD auxiliar;
auxiliar=freq_string[j-1];
freq_string[j-1]=freq_string[j];
freq_string[j]=auxiliar;
--j;
}
}
if(i!=freq_string.size()-1 and freq_string[i+1].second==freq_string[i].second){
int j=i;
while(j!=freq_string.size()-1 and freq_string[j+1].second==freq_string[j].second and (freq_string[j+1].first.size()<freq_string[j].first.size() or (freq_string[j+1].first.size()==freq_string[j].first.size() and ((freq_string[j+1].first)<(freq_string[j].first))))){
PSD auxiliar;
auxiliar=freq_string[j+1];
freq_string[j+1]=freq_string[j];
freq_string[j]=auxiliar;
++j;
}
}
}
++i;
}
return;
}
void Taula::crear_vector() {
freq_string=VPSD(aparicions_string.size());
int i=0;
for(auto it:aparicions_string){
freq_string[i]=PSD(it.first, it.second);
++i;
}
sort(freq_string.begin(), freq_string.end(), ordenacio);
return;
}
void Taula::escriure_taula_freq() {
for(int i=0; i<freq_string.size(); ++i){
cout << freq_string[i].first << " " << freq_string[i].second << endl;
}
}