decreasing alphabetical order, and otherwise NEITHER.
给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; }
------from ProLights