题解:B4267 [朝阳区小学组 2019] word

· · 题解

Solution

可以使用 getline 输入一行。

遍历。若为空格,记录之前的一字符串,统计此字符串。

最后,排序并输出。

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 205;
int tot, l;
string s, ss, a[N];
int main(){
    while(getline(cin, s)){
        tot = 0;
        l = s.length();
        for(int i = 0; i < l; i++){
            if(s[i] == ' '){
                a[++tot] = ss;
                ss = "";
            }else ss += s[i];
        }
        if(ss != ""){
            a[++tot] = ss;
            ss = "";
        }
        sort(a + 1, a + tot + 1);
        cout << tot << ' ';
        for(int i = 1; i <= tot; i++) cout << a[i] << ' ';
        cout << '\n';
    }
    return 0;
}