-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSortingMethods
More file actions
56 lines (56 loc) · 1.43 KB
/
TwoSortingMethods
File metadata and controls
56 lines (56 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<string>
#include<vector>
int main()
{
int n;
std::cin>>n;
std::vector<std::string> strs(n);
bool lengths = true;//是否按照长度排序
bool lexic = true;//是否按照字典序排序
for(int i = 1;i <= n;i++)
std::cin>>strs[i-1];
//判断是否按照长度排序
int stri;//第i个字符串的长度12
int stri1 = 0;//第i-1个字符串的长度
for(int i = 0;i < n;i++)
{
stri = strs[i].size();
if(stri < stri1)
{
lengths = false;
break;
}
stri1 = stri;
}
//判断是否按照字典序排序
for(int i = 1;i < n;i++)
{
std::string::iterator stri1 = strs[i-1].begin();
std::string::iterator stri = strs[i].begin();
while(stri1 < strs[i-1].end() && stri < strs[i].end() && *stri1 == *stri)
++stri,++stri1;
if(stri == strs[i].end() && stri1 != strs[i-1].end())
{
lexic = false;
break;
}
else if(stri1 != strs[i-1].end() && stri != strs[i].end())
{
if(*stri < *stri1)
{
lexic = false;
break;
}
}
}
if(lengths && lexic)
std::cout<<"both";
else if(lengths)
std::cout<<"lengths";
else if(lexic)
std::cout<<"lexicographically";
else
std::cout<<"none";
return 0;
}