题解

JerryLi  •  1年前


#include <bits/stdc++.h>
using namespace std;

int main(){
	//freopen("in.txt","r",stdin);
	string str;
	string word[1010];
	getline(cin,str);
	int  p = 0;
	for(int i=0;i<str.length();i++){
		if(str.at(i)!=' '&&str.at(i)!=','&&str.at(i)!='.'&&str.at(i)!='!'){
			word[p] += str.at(i);
		}
		else{
			p++;
		}
	}
	for(int i=0;i<p;i++){
		int flag = 0;
		for(int j=0;j<p-1;j++){
			int it = 0;
			while(word[j].length()!=word[j+1].length()){
				if(word[j].length()<word[j+1].length()){
					word[j] += "1";
				}
				else{
					word[j+1] += "1";
				}
			}
			while(word[j].at(it)==word[j+1].at(it)) {
				if(word[j].at(it)=='1'||word[j+1].at(it)=='1') break;
				it++;
			}
			if(word[j].at(it)>word[j+1].at(it)){
				swap(word[j],word[j+1]);
				flag = 1;
			}
		}
		if(flag==0) break;
	}
	cout << p << endl;
	for(int i=0;i<p;i++) {
		for(int j=0;j<word[i].length();j++){
			if(word[i].at(j)=='1'){
				word[i].erase(j);
			}
		}
		cout << word[i] << endl;
	}
	return 0;
}

评论: