Summer Training Team Selection (1) Problem F Line Them Up 判断升序降序

Problem F
Line Them Up
Time limit: 1 second
An eccentric coach asks players on the team to line up alphabetically at the start of practice. The coach does
not tell the players whether they need to line up in increasing or decreasing order, so they guess. If they
guess wrong, the coach makes them run laps before practice. Given a list of names, you are to determine if
the list is in increasing alphabetical order, decreasing alphabetical order or neither.
Input
The input consists of a single test case. The first line will contain the number N of people on the team
(2  N  20). Following that are N lines, each containing the name of one person. A name will be at least
two characters and at most 12 characters in length and will consist only of capital letters, and with no white
spaces (sorry BILLY BOB and MARY JOE). Duplicates names will not be allowed on a team.
Output
Output a single word: INCREASING if the list is in increasing alphabetical order, DECREASING if it is in

decreasing alphabetical order, and otherwise NEITHER.


Source

2016 UESTC ACM Summer Training Team Selection (1) 
ACM-ICPC 2015 Mid-Central Regional Problem F: Line Them Up

My Solution

给n个字符串判断升序还是降序还是乱序

就定义一个, m  = n;nn = n;

然后如果降了一次m--;

如果升了一次m++

最好用m和n比较

这个方法好像处理简单的字符串的时候挺常用的

if(2*nn == m+1) printf("INCREASING");
else if(m == 1) printf("DECREASING");

else printf("NEITHER");

复杂度 O(26*n)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char line1[16];
char line2[16];

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    #endif // LOCAL
    int n, len1, len2,nn, m, t;
    scanf("%d", &n);
    m = n;nn = n;
    scanf("%s", line1);n--;len1 = strlen(line1);
    while(n--){
        scanf("%s", line2);
        len2 = strlen(line2);
        t = m;
        int sz = len1 <= len2 ? len1 : len2;
        for(int i = 0; i < sz; i++){
            if(line1[i] > line2[i]){
                m--;
                break;
            }
            else if(line1[i] < line2[i]){
                m++;
                break;
            }
        }
        if(t==m){
            m += len1 <= len2 ? 1 : -1;
        }
        strcpy(line1, line2);
        len1 = len2;

    }
    //cout<<nn<<" "<<m<<endl;
    if(2*nn == m+1) printf("INCREASING");
    else if(m == 1) printf("DECREASING");
    else printf("NEITHER");
    return 0;
}

Thank you!



                                                                                                                                               ------from ProLights


你可能感兴趣的:(Summer Training Team Selection (1) Problem F Line Them Up 判断升序降序)