7-9 挑单词 (20 分)
输入若干行数据,每行只包含英文字符(不区分大小写)和数字字符,请找出所有的“单词”,这里所说的“单词”指的是长度大于等于3的连续英文字符构成的字符序列,并把找到的“单词”按字母序从大到小排列,若某个单词重复出现,在结果中只输出一次,若没有满足要求的单词,输出“no word"。
输入格式:
第一行给出总行数n,接下来n行给出由英文字符(不区分大小写)和数字字符组成的串,每行中字符数量不超过1000,每行以回车结束。
输出格式:
输出“单词”的降序序列,每个“单词”首字母大写,单词间用一个空格分隔,最后一个单词后没有空格。
输入样例1:
在这里给出一组输入。例如:
1
Hi1123hello2how98are2111119you88
输出样例1:
在这里给出相应的输出。例如:
You How Hello Are
输入样例2:
在这里给出一组输入。例如:
2
Fine445thank66you
and990you276w
输出样例2:
在这里给出相应的输出。例如:
You Thank Fine And
输入样例3:
在这里给出一组输入。例如:
2
Fe445ak66
an990u276w
输出样例3:
在这里给出相应的输出。例如:
no word
我确实不会写!!!
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main() {
int n;
cin >> n;
getchar();
vector <char> v;
set <string,greater<string>> s;
for(int i = 0; i < n; i ++) {
string str;
cin >> str;
v.clear();
for(int j = 0; j < str.length(); j ++) {
if(isalpha(str[j])) {
if(v.size() == 0)
str[j] = toupper(str[j]); //加入单词的时候就首字母大写
else
str[j] = tolower(str[j]);
v.push_back(str[j]);
} else {
string s1;
if(v.size() >= 3) {
for(auto it :v)
s1 += it;
s.insert(s1);
}
v.clear();
}
}
}
if(s.size()) {
int isfir = 1;
for(auto it : s) {
if(!isfir)
cout << " ";
isfir = 0;
cout << it;
}
} else
cout << "no word" ;
return 0;
}
错误的!!!
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
getchar();
string s;
map<string,int> m;
int flag=0;
for(int i=0; i<n; i++) {
getline(cin,s);
for(int j=0; j<s.length(); j++) {
int len=0;
while(s[j+len]>='a'&&s[j+len]<='z'||s[j+len]>='A'&&s[j+len]<='Z') {
len++;
}
j=j+len-1;
if(len>=3) {
m[s.substr(j,len)]++;
flag++;
}
}
}
if(flag==0) {
cout<<"no word";
} else {
for(auto it=m.end(); it!=m.begin(); it--,flag--) {
cout<<it->first;
if(flag>1) cout<<" ";
}
}
return 0;
}