發表於 C++CPE考古題UVA一顆星

【CPE】UVA 11541 Decoding 20190924 考古題 by C++

11541 Decoding

簡易翻譯和條件:

這題光看題目輸出輸入就知道要幹嘛了

輸入是一串(英文+數字)*N組合的字串

輸出是英文加上對應數字的次數

ex:
輸入: A1B3C4
輸出: ABBBCCCC

這題目我直覺是想到使用 regular expression 來做啦~

不過我能夠熟悉到直接使用不查資料的是 python 的 re XD

但是 CPE 不接受使用 Python阿!!!
所以我就查了一下 C++ 版本的 regular expression 寫啦~

就像 Python 一樣,直接 search 符合 pattern 條件的字串就好啦~
然後根據拆出來的數字用 for 迴圈輸出。

基本上就是一題考你是否熟系 C++ 套件的題目啦!

如果有任何問題或是你有更好的寫法歡迎再下面留言討論喔!
我們下一題見啦!

程式碼:

#include 
#include 
#include 

using namespace std;

int main()
{// A1B2C3D10E1
    int N = 0, ORIGINN;
    cin >> N;
    cin.ignore(); // if dont add this line, the next getline will receive a endline first

    ORIGINN = N;
    string pattern("([A-Z])([0-9]*)");
    regex reg(pattern);

    while(N--){
        string line;
        getline(cin, line);
        cout << "Case " << ORIGINN-N << ": ";
        smatch m;
        while(regex_search(line, m, reg)){

            int times = stoi(m[2]);
            for(int i=0; i<times; i++){
                cout << m[1];
            }

            line = m.suffix().str(); // 重新整理
        }
        cout << endl;
    }

    return 0;
}

作者:

一位 熱愛資工領域、喜歡好笑事物、偶爾打打網球 的學生 ! For A Better Me!

發表迴響

Please log in using one of these methods to post your comment:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.