当前位置:网站首页>Niu Ke's questions -- two sorting methods
Niu Ke's questions -- two sorting methods
2022-06-11 18:30:00 【HHYX.】
Two sort methods
Topic link : Two sort methods
Title Description
Koala has n A string string , Any two strings are different in length . Koala recently learned that there are two ways to sort strings :
1. Sort strings in lexicographic order . for example :
“car” < “carriage” < “cats” < “doggies < “koala”
2. Sort according to the length of the string . for example :
“car” < “cats” < “koala” < “doggies” < “carriage”
Koalas want to know whether their string order meets these two sorting methods , Koalas are busy eating leaves , So I need you to help verify .
Input description :
Enter the number of first line strings n(n ≤ 100) Next n That's ok , One string per line , String length is less than 100, It's all made up of small letters
Output description :
If these strings are arranged according to dictionary order rather than length "lexicographically”,
If the output is arranged according to length rather than dictionary order "lengths",
If both methods match the output "both", Otherwise output "none"

Topic analysis
According to the meaning of the question, a bunch of strings are given here , You need to determine whether the strings are sorted by length or by dictionary order . Dictionary order is the order in which strings are compared in size . Length compares the length of each string . So here we can judge separately , Use one bool Function of type , If the dictionary order is always satisfied ( Length order ) You can return true, In case of dissatisfaction, it returns false. The final output can be made according to whether dictionary order or length order is satisfied or both . The code implementation is as follows :
Code implementation
#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())// Judge whether the previous one is smaller than the latter one
{
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])// Judge whether the previous one is smaller than the latter one
{
return false;
}
}
return true;
}
int main()
{
vector<string> v;
int n=0;
cin >> n ;
v.resize(n);// Set up v The number of elements in
for(size_t i=0;i<v.size();i++)
{
cin>>v[i];// Enter individual strings
}
bool lsort = IsLengths(v);// Judge whether it is length order
bool csort = Islexicographically(v);// Judge whether it is a dictionary order
if(lsort && csort)
{
cout<<"both"<<endl;
}
else if(lsort)
{
cout<<"lengths"<<endl;
}
else if(csort)
{
cout<<"lexicographically"<<endl;
}
else
{
cout<<"none"<<endl;
}
return 0;
}

边栏推荐
- Two methods for matlab to save imshow drawing pictures to a specified folder
- [c language] output students' names and scores in descending order of scores with structures
- . Net core redis hyperloglog type
- 5 minutes to understand the red, blue and purple in the attack and defense drill
- Force deduction 23 questions, merging K ascending linked lists
- SISO decoder for min sum (supplementary Chapter 2)
- Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots
- Niu Ke's questions -- binary search tree and bidirectional linked list
- 力扣刷题——二叉树的层序遍历Ⅱ
- SISO decoder for a general (n, n-1) SPC code (supplementary Chapter 3)
猜你喜欢

Reading summary of nacos2.x source code

关于keil中,while循环条件不成立却无法跳出的问题
![[c language] compress strings and add markup characters](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[c language] compress strings and add markup characters
![[c language] shift elements after sorting elements of an array](/img/5b/3e74fc40787d94f6d0ab93332140ba.png)
[c language] shift elements after sorting elements of an array

合并多棵二叉搜索树

Getting started with CTF

使用Visdom對損失函數進行監控
![[golang] leetcode - 349 Intersection of two arrays (hash table)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[golang] leetcode - 349 Intersection of two arrays (hash table)

信号的处理与捕捉

力扣刷题——二叉树的层序遍历Ⅱ
随机推荐
初识企业级平台
ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
On the sequence traversal of binary tree Ⅱ
Some thoughts on how to do a good job of operation and maintenance management
牛客刷题——把字符串转换成整数
Modern application of LDAP directory server
Force deduction 32 questions longest valid bracket
牛客刷题——两种排序方法
Feign 共享登录信息进行请求
使用Visdom對損失函數進行監控
基于TI AM5728 + Artix-7 FPGA开发板(DSP+ARM) 5G通信测试手册
SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
264 Concepts
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
LDAP 目录服务器的现代化应用
[C语言]限制查找次数,输出次数内查找到的最大值
TR-069 protocol introduction
力扣31 下一个排列
[golang] leetcode - 292 Nim games (Mathematics)
vim常用命令