-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecretos.cpp
65 lines (59 loc) · 1.52 KB
/
Secretos.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
struct pos{
int empieza,termina,nivel;
pos(int x1,int x2,int x3) : empieza(x1),termina(x2),nivel(x3) {}
};
void leer(vector<char>& a,vector<pos>& b,string linea){
int x=0,c=0;
for(int i = 0;i<linea.length();i++){
if(linea[i] == '('){
x+=1;
pos nuevo(c,0,0);
b.push_back(nuevo);
} else if(linea[i] == ')'){
if(b.size()>1){
b[b.size()-(b.size()-x)].termina = c;
b[b.size()-(b.size()-x)].nivel = x;
x-=1;
} else {
b[0].termina = c;
b[0].nivel = x;
x-=1;
}
} else {
c++;
a.push_back(linea[i]);
}
}
}
void girar(vector<char>&a,const vector<pos> b){
vector<pos>::const_iterator it = b.begin();
for(;it<b.end();it++){
reverse(a.begin()+(*it).empieza,a.begin()+(*it).termina);
}
}
void escribir(const vector<char> a){
vector<char>::const_iterator it = a.begin();
ofstream fout("out.txt");
for(;it<a.end();it++){
fout << (*it);
}
}
bool metodo(pos a,pos b){
return a.nivel > b.nivel;
}
int main(){
vector<char> mensaje;
vector<pos> reverselist;
string linea;
ifstream fin("in.txt");
getline(fin,linea);
leer(mensaje,reverselist,linea);
sort(reverselist.begin(),reverselist.end(),metodo);
girar(mensaje,reverselist);
escribir(mensaje);
}