挑单词
输入若干行数据,每行只包含英文字符(不区分大小写)和数字字符,请找出所有的“单词”,这里所说的“单词”
指的是长度大于等于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
思路:题目给的字符串只有数字和字母,只要找出连续长度大于3的字串,记录开始下标和结束下标,然后时用substr函数,得到字串,在处理输出。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, index = 0, a = 0, b, c = 0;
cin >> n;
getchar();
string str1, str2[10000], str3, str4 = "";
while (n--)
{
getline(cin, str1);
a = 0;
b = 0;
for (int i = 0; i < str1.length(); i++)
{
if ((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z'))
{
if(str1[i] >= 'A' && str1[i] <= 'Z'){
str1[i] = 'a' + str1[i] - 'A';
}//将字符串字符转化为小写,方便后续按照题目要求输出。
if (!a && !b)//判断开始下标。a是记录子串长度。
{
b = i;
a++;
}
else
{
a++;
}
}
if (a >= 3 && ((str1[i] >= '0' && str1[i] <= '9') || (i == str1.length() - 1)))
{
str3 = str1.substr(b, a);//通过a,b获取字串
if(str4.find(str3) == string::npos){
str2[index] = str3;
index++;
str4 += str3;
c = 1;
}
a = 0;
b = 0;
}
if (a < 3 && ((str1[i] >= '0' && str1[i] <= '9') || (i == str1.length() - 1)))
{
a = 0;
b = 0;
}
}
}
for(int i = 1; i < index; i++){
for(int j = 0; j < index - i; j++){
if(str2[j] < str2[j + 1]){
str3 = str2[j];
str2[j] = str2[j + 1];
str2[j + 1] = str3;
}
}
}
if(c){
for(int i = 0; i < index; i++){
for(int j = 0; j < str2[i].length(); j++){
if(!j){
if ((str2[i][j] >= 'a' && str2[i][j] <= 'z')){
printf("%c" , 'A' + str2[i][j] - 'a');
}else{
cout << str2[i][j];
}
}else{
cout << str2[i][j];
}
}
if(i != index - 1){
cout << " ";
}
}
}else{
cout << "no word";
}
return 0;
}