Editorial for Mã Hóa Xâu


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

\(\color{red}{\text{Spoiler Alert}_{{}_{{}^{{}^{v2.0}}}}}\)

\(\color{red}{\text{Khuyến khích bạn đọc trước khi đọc phần lời giải xin hãy thử code ra thuật của mình dù nó có sai hay đúng}}\)

\(\color{red}{\text{Sau đó từ phần bài giải và thuật toán trước đó mà đối chiếu, rút nhận xét với thuật của mình và thu được bài học (không lãng phí thời gian đâu).}}\)



\(\color{orange}{\text{Hướng dẫn}}\)

  • Nhận từng xâu và xuất ra độ dài cho tới khi hết xâu (hoặc xâu nhận được là rỗng - độ dài 0)

\(\color{goldenrod}{\text{Tiếp cận}}\)

  • Cách 1: Chạy vô hạn việc nhận xâu, nếu xâu la rỗng thì dừng, ngược lại xuất độ dài xâu

  • Cách 2: Trong khi vẫn còn nhận được xâu từ dữ liệu vào, xuất độ dài xâu

  • Cách 3: Tương tự cách 2 nhưng viết ngắn gọn bằng vòng lặp for (điều này giúp speedcode nhưng giảm readability)

  • Cách 4: Nhận toàn bộ xâu rồi xử lí từng phần riêng biệt (trong C++ là dùng \(std::getline()\))

  • Cách 5: Nhận từng kí tự rồi xử lí từng phần riêng biệt (trong C++ là dùng \(std::getchar()\))

  • Phân tích:

Cách 2 đơn giản nhất.

Cách 3 khó đọc nhất.

Cách 5 phức tạp nhất.

Cách 4 lưu nhiều dữ liệu nhất

Cách 5 lưu ít dữ liệu nhất.


\(\color{green}{\text{Code tham khảo }}\): Cách 1

\(^{^{\color{purple}{\text{Độ phức tạp : }} O(|s|)\ \color{purple}{\text{thời gian}}\ ||\ O(|s|)\ \color{purple}{\text{bộ nhớ}}}}\)

C++
#include <iostream>

using namespace std;

int main()
{
    while (true)
    {
        string s;
        cin >> s;
        if (s.empty()) break;

        cout << s.size() << ' ';
    }

    return 0;
}

\(\color{green}{\text{Code tham khảo }}\): Cách 2

\(^{^{\color{purple}{\text{Độ phức tạp : }} O(|s|)\ \color{purple}{\text{thời gian}}\ ||\ O(|s|)\ \color{purple}{\text{bộ nhớ}}}}\)

C++
#include <iostream>

using namespace std;

int main()
{
    string s;
    while (cin >> s)
    {
        cout << s.size() << ' ';
    }

    return 0;
}

\(\color{green}{\text{Code tham khảo }}\): Cách 3

\(^{^{\color{purple}{\text{Độ phức tạp : }} O(|s|)\ \color{purple}{\text{thời gian}}\ ||\ O(|s|)\ \color{purple}{\text{bộ nhớ}}}}\)

C++
#include <iostream>

using namespace std;

int main()
{
    for (string s; cin >> s; cout << s.size() << ' ');
    return 0;
}

\(\color{green}{\text{Code tham khảo }}\): Cách 4

\(^{^{\color{purple}{\text{Độ phức tạp : }} O(|s|)\ \color{purple}{\text{thời gian}}\ ||\ O(|s|)\ \color{purple}{\text{bộ nhớ}}}}\)

C++
#include <iostream>

using namespace std;

int main()
{
    string s;
    getline(cin, s);
    s += ' ';

    int cnt = 0;
    for (char c : s)
    {
        bool is_uplatin = (c >= 'A' && c <= 'Z');
        bool is_lowlatin = (c >= 'a' && c <= 'z');
        bool is_number = (c >= '0' && c <= '9');
        if (is_uplatin || is_lowlatin || is_number)
        {
            ++cnt;
        }
        else
        {
            if (cnt != 0) /// have a string with length (cnt)
            {
                cout << cnt << ' ';
                cnt = 0;
            }
        }
    }
    return 0;
}

\(\color{green}{\text{Code tham khảo }}\): Cách 5

\(^{^{\color{purple}{\text{Độ phức tạp : }} O(|s|)\ \color{purple}{\text{thời gian}}\ ||\ O(1)\ \color{purple}{\text{bộ nhớ}}}}\)

C++
#include <iostream>

using namespace std;

int main()
{
    int cnt = 0;
    for (char c; c != EOF; c = getchar())
    {
        bool is_uplatin = (c >= 'A' && c <= 'Z');
        bool is_lowlatin = (c >= 'a' && c <= 'z');
        bool is_number = (c >= '0' && c <= '9');
        if (is_uplatin || is_lowlatin || is_number)
        {
            ++cnt;
        }
        else
        {
            if (cnt != 0) /// have a string with length (cnt)
            {
                cout << cnt << ' ';
                cnt = 0;
            }
        }
    }

    if (cnt != 0)
    {
        cout << cnt << ' ';
        cnt = 0;
    }

    return 0;
}


Comments

There are no comments at the moment.