Informatics Orsay Book 1185: Word Sorting (Father)

1185: Word ordering


Time limit: 1000 ms Memory limit: 65536 KB
Commits: 7688 Passes: 3919

【Title description】

Enter a line of word sequence with one or more spaces between adjacent words. Please output these words in lexicographic order. Repeated words are required to be output only once. (Case sensitive)

【Enter】

A line of word sequence, with a minimum of 1 word and a maximum of 100 words, with each word no longer than 50 words, with at least 1 space between words. The data does not contain any characters except letters and spaces.

【Output】

These words are output in lexicographic order, and repeated words are output only once.

【Input example】

She  wants  to go to Peking University to study  Chinese

[Sample output]

Chinese
Peking
She
University
go
study
to
wants

 

Explanation: Pay attention to the input

while(cin>>a[i]) 

#include<bits/stdc++.h>
using namespace std;
string a[105];
bool cmp(string x,string y){
	return x<y;
}
int main(){
  	freopen("test.in","r",stdin);
  	freopen("test.out","w",stdout);
  	int k = 1;
	while(cin>>a[k]){
		k++;
	}
	k--;
	for(int i=1;i<=k;i++){
		for(int j=1;j<=k-i;j++){
			if(!cmp(a[j],a[j+1])){
				swap(a[j],a[j+1]);
			}
		}
	}
	for(int i=1;i<=k;i++){
		if(a[i]!=a[i-1]){
			cout<<a[i]<<endl;
		}
	}
	return 0;
}

 

 

Posted 33 original articles · liked 0 · visits 167

Guess you like

Origin blog.csdn.net/weixin_42790071/article/details/105467672