当前位置:网站首页>牛客刷题——两种排序方法
牛客刷题——两种排序方法
2022-06-11 18:14:00 【HHYX.】
题目链接:两种排序方法
题目描述
考拉有n个字符串字符串,任意两个字符串长度都是不同的。考拉最近学习到有两种字符串的排序方法:
1.根据字符串的字典序排序。例如:
“car” < “carriage” < “cats” < “doggies < “koala”
2.根据字符串的长度排序。例如:
“car” < “cats” < “koala” < “doggies” < “carriage”
考拉想知道自己的这些字符串排列顺序是否满足这两种排序方法,考拉要忙着吃树叶,所以需要你来帮忙验证。
输入描述:
输入第一行为字符串个数n(n ≤ 100) 接下来的n行,每行一个字符串,字符串长度均小于100,均由小写字母组成
输出描述:
如果这些字符串是根据字典序排列而不是根据长度排列输出"lexicographically”,
如果根据长度排列而不是字典序排列输出"lengths",
如果两种方式都符合输出"both",否则输出"none"

题目分析
根据题意得这里给定了一堆字符串,需要进行判断这堆字符串是依据长度进行排序的还是依据字典序进行排序的。字典序也就是字符串进行比较大小的顺序。而长度则是比较每个字符串的长度。因此这里可以分别进行判断,使用一个bool类型的函数,如果始终满足字典序(长度顺序)则可以返回true,如果出现不满足的情况则返回false。根据是否满足字典序或者长度顺序还是两种都满足可以进行最终的输出.代码实现如下:
代码实现
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool IsLengths(const vector<string>& v)
{
for(size_t i=0;i<v.size() - 1;i++)
{
if(v[i].size()>v[i+1].size())//判断前一个是否小于后一个
{
return false;
}
}
return true;
}
bool Islexicographically(const vector<string>& v)
{
for(size_t i=0;i<v.size() - 1;i++)
{
if(v[i] > v[i+1])//判断前一个是否小于后一个
{
return false;
}
}
return true;
}
int main()
{
vector<string> v;
int n=0;
cin >> n ;
v.resize(n);//设置v中元素个数
for(size_t i=0;i<v.size();i++)
{
cin>>v[i];//输入各个字符串
}
bool lsort = IsLengths(v);//判断是否为长度序
bool csort = Islexicographically(v);//判断是否为字典序
if(lsort && csort)
{
cout<<"both"<<endl;
}
else if(lsort)
{
cout<<"lengths"<<endl;
}
else if(csort)
{
cout<<"lexicographically"<<endl;
}
else
{
cout<<"none"<<endl;
}
return 0;
}

边栏推荐
- Talking about telework | community essay solicitation
- 金融银行_催收系统简介
- SISO decoder for a general (n,n-1) SPC code(补充章节3)
- LDAP 目录服务器的现代化应用
- [not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration
- Spring 2021 daily question [week6 not finished]
- 密码学概述
- SISO Decoder for min-sum(补充章节2)
- 软件需求工程复习
- v-for中的key的作用和原理
猜你喜欢
![[c language] compress strings and add markup characters](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[c language] compress strings and add markup characters
MySQL/Redis 常见面试题汇总
![[c language] limit the number of searches and output the maximum value found in the number of internal searches](/img/e6/cbb8dd54b49ade453251a70c8455e8.png)
[c language] limit the number of searches and output the maximum value found in the number of internal searches
![Codeworks round 479 (Div. 3) [done]](/img/a0/f3c6989d8f755c03076b237514ee64.jpg)
Codeworks round 479 (Div. 3) [done]
![[C语言]用结构体按分数高低降序输出学生的姓名和分数](/img/41/b9dba88941560b296f4d7153b7c684.png)
[C语言]用结构体按分数高低降序输出学生的姓名和分数

【无标题】

密码学概述
![Codeworks round 481 (Div. 3) [done]](/img/60/01ed6180ccc4c99fe361d493525018.jpg)
Codeworks round 481 (Div. 3) [done]
![[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration](/img/ae/9a0c300f2dcb03b05d737f14b0955f.jpg)
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration

【C】 Compilation preprocessing and environment
随机推荐
Sword finger offer (2nd Edition)
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
使用Transformers将TF模型转化成PyTorch模型
DC-DC自举电容(BOOT)几个问题
LDAP 目录服务器的现代化应用
Class question: how to ensure that line table storage can be inserted at any time?
使用mysql判断日期是星期几
System learning typescript (V) - joint type
Codeworks round 479 (Div. 3) [done]
合并多棵二叉搜索树
金融银行_催收系统简介
Hwang
【新手上路常见问答】关于项目管理
[C语言]对一个数组的元素排序后平移元素
Talking about telework | community essay solicitation
Winter vacation daily question (improvement group) [end of week 4]
密码学概述
在同花顺上面开股票账户好不好,安不安全?
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
高性能架构设计