24h購物| | PChome| 登入
2012-10-20 23:47:42| 人氣285| 回應0 | 上一篇 | 下一篇

NCPC2012A String Editer

推薦 0 收藏 0 轉貼0 訂閱站台

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]
*/

台長: Morris
人氣(285) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 資訊競賽 |
此分類下一篇:NCPC2012D A Matrix Decipher
此分類上一篇:[PTC][201209] ABCE 爛解

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文