当前位置:网站首页>牛客刷题——两种排序方法
牛客刷题——两种排序方法
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;
}

边栏推荐
- Class question: how to ensure that line table storage can be inserted at any time?
- Reading summary of nacos2.x source code
- ctfhub-sql布尔盲注
- Introduction to social engineering practice
- 软件测试技术复习
- 关于keil中,while循环条件不成立却无法跳出的问题
- [untitled]
- 力扣32题最长有效括号
- 判断是否为平衡二叉树
- [c language] output the students within the specified score range with the structure
猜你喜欢

排序的循环链表

【无标题】
![Codeworks round 479 (Div. 3) [done]](/img/a0/f3c6989d8f755c03076b237514ee64.jpg)
Codeworks round 479 (Div. 3) [done]

RadioGroup动态添加RadioButton

关于keil中,while循环条件不成立却无法跳出的问题
![[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)

软件测试技术复习
![Spring 2021 daily question [end of week4]](/img/b3/2f5a66b0d4374db3d4db0b71d72f7e.jpg)
Spring 2021 daily question [end of week4]
MySQL/Redis 常见面试题汇总

Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
随机推荐
VIM common commands
Spring 2021 daily question [week3 not finished]
PIL-Pillow图像处理【1】-安装与新建
SISO decoder for a general (n, n-1) SPC code (supplementary Chapter 3)
ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
高性能架构设计
使用mysql判断日期是星期几
Radio button text background changes at the same time
ctfhub-sql布尔盲注
关于keil中,while循环条件不成立却无法跳出的问题
Function and principle of key in V-for
SISO decoder for a general (n,n-1) SPC code(补充章节3)
SISO decoder for a general (n,n-1) SPC code(補充章節3)
[piecemeal knowledge] [network composition] the mobile phone can be connected to the campus network, but the computer can't
[C语言]用结构体按分数高低降序输出学生的姓名和分数
NFT platform development NFT mall source code NFT mall development chain game development
如何学习和自学
排序的循环链表
Sorted circular linked list