2012-10-20 23:47:42Morris
NCPC2012A String Editer
Sample Input:
AK47 is powerful
>[2]^[B]>[1]#[48]
Sam sings badly
>[4]<<>[1]#[u]>[3]^[ i]>[6]![2]
Apple is better than Samsung
>[3]<<<<<<^[hTC]>[1]![2]
Sample Output:
AKB48 is powerful
Samung is bad
hTC is better than Samsung
這一題是在比賽時最多爭議的一題, 因為游標的移動沒有講得很清楚, 而且連範測都有一些問題, 在很多提問後, 瞬間以 0WA, 1AC通過, 以下的程式碼是我回來的時候自己打的, 當然可能也會有問題, 因為我沒有當下印出程式碼, 而且測資也不好生, 所以不會由我來放在 ZJ。
#include <cstdio>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string txt, s;
while(getline(cin, txt)) {
getline(cin, s);
int idx = 0, i, j, len = s.length();
int x;
for(i = 0; i < len; i++) {
if(s[i] == '<') {
if(s[i+1] == '<') {
i++;
if(idx) {
txt.erase(idx-1, 1);
idx--;
}
} else {
i += 2;
string num = "";
while(s[i] != ']')
num += s[i], i++;
stringstream sin;
sin << num;
sin >> x;
idx -= x;
}
} else if(s[i] == '>') {
i += 2;
string num = "";
while(s[i] != ']')
num += s[i], i++;
stringstream sin;
sin << num;
sin >> x;
idx += x;
} else if(s[i] == '^') {
i += 2;
string ins = "";
while(s[i] != ']')
ins += s[i], i++;
txt.insert(idx, ins);
idx += ins.length()-1;
} else if(s[i] == '#') {
i += 2;
string rep = "";
while(s[i] != ']')
rep += s[i], i++;
for(j = 0; j < rep.length(); j++)
txt[idx] = rep[j], idx++;
idx--;
} else {
i += 2;
string num = "";
while(s[i] != ']')
num += s[i], i++;
stringstream sin;
sin << num;
sin >> x;
txt.erase(idx, x);
}
}
cout << txt << endl;
}
return 0;
}/*
AK47 is powerful
>[2]^[B]>[1]#[48]
Sam sings badly
>[4]<<>[1]#[u]>[3]^[ i]>[6]![2]
Apple is better than Samsung
>[3]<<<<<<^[hTC]>[1]![2]
*/
AK47 is powerful
>[2]^[B]>[1]#[48]
Sam sings badly
>[4]<<>[1]#[u]>[3]^[ i]>[6]![2]
Apple is better than Samsung
>[3]<<<<<<^[hTC]>[1]![2]
Sample Output:
AKB48 is powerful
Samung is bad
hTC is better than Samsung
這一題是在比賽時最多爭議的一題, 因為游標的移動沒有講得很清楚, 而且連範測都有一些問題, 在很多提問後, 瞬間以 0WA, 1AC通過, 以下的程式碼是我回來的時候自己打的, 當然可能也會有問題, 因為我沒有當下印出程式碼, 而且測資也不好生, 所以不會由我來放在 ZJ。
#include <cstdio>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string txt, s;
while(getline(cin, txt)) {
getline(cin, s);
int idx = 0, i, j, len = s.length();
int x;
for(i = 0; i < len; i++) {
if(s[i] == '<') {
if(s[i+1] == '<') {
i++;
if(idx) {
txt.erase(idx-1, 1);
idx--;
}
} else {
i += 2;
string num = "";
while(s[i] != ']')
num += s[i], i++;
stringstream sin;
sin << num;
sin >> x;
idx -= x;
}
} else if(s[i] == '>') {
i += 2;
string num = "";
while(s[i] != ']')
num += s[i], i++;
stringstream sin;
sin << num;
sin >> x;
idx += x;
} else if(s[i] == '^') {
i += 2;
string ins = "";
while(s[i] != ']')
ins += s[i], i++;
txt.insert(idx, ins);
idx += ins.length()-1;
} else if(s[i] == '#') {
i += 2;
string rep = "";
while(s[i] != ']')
rep += s[i], i++;
for(j = 0; j < rep.length(); j++)
txt[idx] = rep[j], idx++;
idx--;
} else {
i += 2;
string num = "";
while(s[i] != ']')
num += s[i], i++;
stringstream sin;
sin << num;
sin >> x;
txt.erase(idx, x);
}
}
cout << txt << endl;
}
return 0;
}/*
AK47 is powerful
>[2]^[B]>[1]#[48]
Sam sings badly
>[4]<<>[1]#[u]>[3]^[ i]>[6]![2]
Apple is better than Samsung
>[3]<<<<<<^[hTC]>[1]![2]
*/