题目链接
题目描述
考拉有n个字符串字符串,任意两个字符串长度都是不同的。考拉最近学习到有两种字符串的排序方法: 1.根据字符串的字典序排序。例如:
"car" < "carriage" < "cats" < "doggies < "koala"
2.根据字符串的长度排序。例如:
"car" < "cats" < "koala" < "doggies" < "carriage"
考拉想知道自己的这些字符串排列顺序是否满足这两种排序方法,考拉要忙着吃树叶,所以需要你来帮忙验证。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100+5;
struct node{
    char s[maxn];
    int len;
}word[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i = 0;i < n;i++)
        {
               scanf("%s",word[i].s);
            word[i].len = strlen(word[i].s);        
        }
        int flag1 = 1,flag2 = 1;
        for(int i = 0;i < n-1;i++)
        {
            if(flag1 && word[i].len > word[i+1].len )
            {
                flag1 = 0;
            }
            if(flag2 && strcmp(word[i].s,word[i+1].s)>0 )
            {
                flag2 = 0;
            }
        }
        if(flag1 && flag2)
        {
            cout<<"both"<<endl;
        }else if(flag1)
        {
            cout<<"lengths"<<endl;
        }else if(flag2)
        {
            cout<<"lexicographically"<<endl;
        }else 
        {
            cout<<"none"<<endl;
        }
    }
    return 0;
} 
Last modification:September 20th, 2019 at 11:43 pm
如果觉得我的文章对你有用,请随意赞赏