代码可以通过测试案例,最后得分20.代码奉上,请大佬提携指教
查看原帖
代码可以通过测试案例,最后得分20.代码奉上,请大佬提携指教
1332067
yyao433100楼主2024/4/6 01:11
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string str;
    getline(cin, str);
    int n = str.length();
    vector<int> myvector;
    // cout << n << " ";
    myvector.push_back(n);
    int count = 0;
    char w = str[0]; // 标识0或1;

    for (int j = 0; j < n; j++)
    {
        if (str[j] == w)
        {
            count++;
        }
        else
        {
            myvector.push_back(count);
            // cout << count << endl;
            count = 1;
            w = str[j];
        }
    }

    for (int i = 0; i < n - 1; i++)
    {
        // cin >> str;
        getline(cin, str);
        // cout << str << endl;
        for (int j = 0; j < n; j++)
        {
            if (str[j] == w)
            {
                count++;
            }
            else
            {
                myvector.push_back(count);
                // cout << count << endl;
                count = 1;
                w = str[j];
            }
        }
    }

    // if (str[n - 2] == str[n - 1])
    myvector.push_back(count);

    for (vector<int>::iterator it = myvector.begin(); it != myvector.end(); it++)
    {
        cout << *it << " ";
    }
}
2024/4/6 01:11
加载中...