输入第一行为字符串个数n(n ≤ 100) 接下来的n行,每行一个字符串,字符串长度均小于100,均由小写字母组成
如果这些字符串是根据字典序排列而不是根据长度排列输出"lexicographically",
如果根据长度排列而不是字典序排列输出"lengths",
如果两种方式都符合输出"both",否则输出"none"
3 a aa bbb
both
import java.util.Scanner;
/**
* 输入描述: 输入第一行为字符串个数n(n ≤ 100) 接下来的n行,每行一个字符串,字符串长度均小于100,均由小写字母组成 输出描述:
* 如果这些字符串是根据字典序排列而不是根据长度排列输出"lexicographically", 如果根据长度排列而不是字典序排列输出"lengths",
* 如果两种方式都符合输出"both",否则输出"none"
*
* @author Administrator
*
*/
public class TestKoalaString {
public static void JudgeKoalaString(String[] str) {
// 假设字符串排序两种方式都符合
boolean directSort = true;
boolean lenSort = true;
int j = 0;
// 先判断是否为长度排序问题
for (int i = 0; i < str.length - 1; i++) {
if (str[i].length() <= str[j + 1].length()) {
j++;
} else {
lenSort = false;// 说明此刻肯定不是按长度排序
}
}
// 传统思路:
//比较字典序问题
for(int i=0;i<str.length-1;i++){
if(str[i].compareTo(str[i+1])>=0){
directSort = false;
break;
}
}
// 第一个思路:
// for(int i = 0;i < str.length - 1;i++) {
// char[] b = str[i].toCharArray();
// char[] c = str[i+1].toCharArray();
// if(b[0] > c[0]) //当前面字符串的首字符大于后面字符串的首字符时,表示该排序不是字典排序
// directSort = false;
// else if(b[0] == c[0]) { //当首字符相等时,需判断前一个字符串与后一个字符串的字符组成是否相同
// for(int k = 0;b[k] == c[k];k++) {
// if(k+1 == c.length&&k+1<b.length){ //当遍历到后一个字符串末尾且此时还未到前一个字符串的末尾时,表示该排序不是字典排序
// directSort = false;
// break;
// }
// if(k+1 == b.length){
// break;
// }
// if(b[k] > c[k]) {
// directSort = false;
// break;
// }
// }
// }
// }
// 第二种思路:
// j = 0;
// int k = 0;
// for (int i = 0; i < str.length - 1; i++) {
// k = i;
// char[] ch1 = str[i].toCharArray();
// char[] ch2 = str[i + 1].toCharArray();
//
// while (j < ch1.length && j < ch2.length) {
// if (ch1[j] > ch2[j]) {
// directSort = false; // 说明此时不是按字典序排序,可能只按长度排序
// break;
// } else if (ch1[j] == ch2[j]) { // 要分两种情况,一种是所有字符组成相同(false),一种是前几个字符组成相同(j++)
// // 判断ch1和ch2是否由相同字符组成
// for (int m = 0; ch1[m] == ch2[m]; m++) {
// if (m + 1 == ch2.length && m + 1 < ch1.length) {
// directSort = false;
// break;
// }
// if (m + 1 == ch1.length) { // 比较结束
// break;
// }
// if (ch1[m] > ch2[m]) {
// directSort = false;
// break;
// }
// }
// }
// j++;// 比较下一个字符
// }
// ch2 = str[++k].toCharArray();
// j = 0; // 从第一位开始比较
// if (k == str.length - 1) {
// break;
// }
// if (!directSort) {
// break;
// }
// }
if (directSort && !lenSort){
System.out.println("lexicographically");
} else if (!directSort && lenSort) {
System.out.println("lengths");
} else if (directSort && lenSort) {
System.out.println("both");
} else {
System.out.println("none");
}
}
@SuppressWarnings("resource")
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n > 100) {
return;
}
sc.nextLine();
String[] str = new String[n];
for (int i = 0; i < n; i++) {
str[i] = sc.nextLine();
}
JudgeKoalaString(str);
sc.close();
}
}
/*
字典序排序,字符串大小排序
*/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string>vec;
//按照字典序排序
bool lexicographically(){
int i=0;
string s;
while((i+1) < vec.size()){
if(vec[i].compare(vec[i+1]) >0)
return 0;
i++;
}
return 1;
}
//按照字符串大小排序
bool Lenths(){
int i=0;
while((i+1) <vec.size() ){
if(vec[i+1].size() < vec[i].size() )
return 0;
i++;
}
return 1;
}
int main(){
string s;
int n,i=0;
bool b1,b2;
cin>>n;
while(i < n){
cin>>s;
vec.push_back(s);
i++;
}
b1=lexicographically();
b2=Lenths();
if(b1 && b2)
cout<<"both";
else if(b1 && !b2)
cout<<"lexicographically";
else if(!b1 && b2)
cout<<"lengths";
else
cout<<"none";
return 0;
}
import java.util.Scanner;
/**
* Created by Genge on 2016-08-20.
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
String[] words = new String[n];
for (int i = 0; i < n; i ++) {
words[i] = scanner.next();
}
System.out.println(validate(words));
}
scanner.close();
}
private static String validate(String[] words) {
boolean isABC = isAbc(words);
boolean isLEN = isLen(words);
if (isABC && isLEN) {
return "both";
}
if (isABC) {
return "lexicographically";
}
if (isLEN) {
return "lengths";
}
return "none";
}
private static boolean isLen(String[] words) {
boolean result = true;
for (int i = 1; i < words.length; i++) {
if (words[i].length() <= words[i - 1].length()) {
result = false;
break;
}
}
return result;
}
private static boolean isAbc(String[] words) {
boolean result = true;
for (int i = 1; i < words.length; i++) {
if (words[i].compareTo(words[i - 1]) <= 0) {
result = false;
break;
}
}
return result;
}
}
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(string a,string b){
return a.length()<b.length();
}
bool equal(string a[],string b[],int N){
for(int i=0;i<N;i++)
if(a[i]!=b[i]) return false;
return true;
}
string a[105],b[105],c[105];
int main(){
int i,n;
for(cin>>n,i=0;i<n;i++) cin>>a[i],b[i]=c[i]=a[i];
sort(b,b+n),sort(c,c+n,cmp);
if(equal(a,b,n)&&equal(a,c,n)) printf("both");
if(equal(a,b,n)&&(!equal(a,c,n))) printf("lexicographically");
if((!equal(a,b,n))&&equal(a,c,n)) printf("lengths");
if((!equal(a,b,n))&&(!equal(a,c,n))) printf("none");
}
import java.util.Scanner;
public class StringUtil{
//两种排序方法
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = Integer.valueOf(in.nextLine());
String str[] = new String[n+1];
for(int i=1; i<=n; i++){
str[i] = in.nextLine();
}
boolean zd = iszd(str);
boolean cd = iscd(str);
if(zd && cd)
System.out.println("both");
else if(zd)
System.out.println("lexicographically");
else if(cd)
System.out.println("lengths");
else
System.out.println("none");
}
}
//字典判断
public static boolean iszd(String[] str){
for(int i=1; i<str.length-1; i++){
if(str[i].compareTo(str[i+1]) >= 0)
return false;
}
return true;
}
//字符串长度判断
public static boolean iscd(String[] str){
for(int i=1; i<str.length-1; i++){
if(str[i].length() >= str[i+1].length())
return false;
}
return true;
}
} #include<iostream>
#include<string.h>
using namespace std;
int main()
{
int n,i,j;
int x=0,y=0;
char a[100][100];
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n-1;i++){
if(strlen(a[i])>strlen(a[i+1])){
x=1;
break;
}
}
for(i=0;i<n-1;i++){
if(strcmp(a[i],a[i+1])>0){
y=1;
break;
}
}
if(x==1 && y==0)
cout<<"lexicographically"<<endl;
else if(x==0 && y==1)
cout<<"lengths"<<endl;
else if(x==0 && y==0)
cout<<"both"<<endl;
else
cout<<"none"<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
bool flag1=0,flag2 = 0;
vector<string> str(n);
for(auto &i:str)
cin>>i;
for(int i=1;i<str.size();i++)
{
if(flag1==0&&str[i-1]>str[i])
flag1 =1;
if(flag2==0&&str[i-1].size()>str[i].size())
flag2 = 1;
if(flag1&&flag2)
break;
}
if(flag1==0&flag2==0)
cout<<"both"<<endl;
else if(flag1==0)
cout<<"lexicographically"<<endl;
else if(flag2==0)
cout<<"lengths"<<endl;
else
cout<<"none"<<endl;
}
return 0;
} import java.util.*;
public class Main{
public static boolean isLen(String[] strings){
int length=strings[0].length();
for(int i=1;i<strings.length;i++){
if(length<=strings[i].length()){
length=strings[i].length();
}
else{
return false;//不是根据长度排的
}
}
return true;
}
public static boolean abc(String[] strings){
ArrayList<String> list=new ArrayList<>();
for (int i = 0; i < strings.length; i++) {
list.add(strings[i]);
}
Collections.sort(list);
String[] strings2=new String[strings.length];
for (int i = 0; i < strings2.length; i++) {
strings2[i]=list.get(i);
}
for (int i = 0; i < strings2.length; i++) {
if (strings[i]!=strings2[i]) {
return false;//不是根据字母顺序
}
}
return true;//根据字母顺序
}
public static void main(String args[]){
Scanner s=new Scanner(System.in);
while(s.hasNext()){
int n=s.nextInt();
String[] strings=new String[n];
for(int i=0;i<n;i++){
strings[i]=s.next();
}
if(isLen(strings)&abc(strings)){
System.out.println("both");
}
else if(isLen(strings)==false&abc(strings)==true){
System.out.println("lexicographically");
}
else if(isLen(strings)==true&abc(strings)==false){
System.out.println("lengths");
}
else{
System.out.println("none");
}
}
}
}
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] str = new String[n];
for (int i = 0; i < n; i++) {
str[i] = in.next();
}
if (lexicographically(str)){
if (lengths(str)){
System.out.println("both");
}else {
System.out.println("lexicographically");
}
}else {
if (lengths(str)){
System.out.println("lengths");
}else {
System.out.println("none");
}
}
}
public static boolean lexicographically(String[] str){
if (str.length <= 1){
return true;
}
for (int i = 1; i < str.length; i++) {
if (str[i-1].compareTo(str[i]) > 0){
return false;
}
}
return true;
}
public static boolean lengths(String[] str){
if (str.length <= 1){
return true;
}
for (int i = 1; i < str.length; i++) {
if (str[i-1].length() > str[i].length()){
return false;
}
}
return true;
}
} #include<iostream>
using namespace std;
#include<string>
#include<vector>
int main()
{
vector<string> vs;
string s;
int n;cin>>n;
vs.resize(n);
for(auto& e:vs)
{
cin>>e;
}
bool lsort = true;
bool dsort = true;
for(int i=0;i<n-1;++i)
{
if(vs[i].length()>vs[i+1].length())
{
lsort = false;
break;
}
}
for(int i=0;i<n-1;++i)
{
if(vs[i]>vs[i+1])
{
dsort = false;
break;
}
}
if(lsort&&dsort)
cout<<"both"<<endl;
else if(lsort||dsort){
if(lsort)
cout<<"lengths"<<endl;
if(dsort)
cout<<"lexicographically"<<endl;
}
else
cout<<"none"<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
int n;
cin >> n;
string str[n];
for (int i = 0; i < n; ++i) {
cin >> str[i];
}
bool p1 = true;
bool p2 = true;
for (int j = 1; j < n; ++j) {
if (str[j - 1] > str[j]) {
p1 = false;
}
if (str[j - 1].size() > str[j].size()) {
p2 = false;
}
}
string res;
if (p1 && p2) {
res = "both";
} else if (p1 && !p2) {
res = "lexicographically";
} else if (!p1 && p2) {
res = "lengths";
} else {
res = "none";
}
cout << res << endl;
return 0;
} #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int nCount;
while (cin >> nCount)
{
vector<string> vecInput;
vecInput.resize(nCount);
for (int i = 0; i < nCount; ++i)
cin >> vecInput[i];
bool isLex = true, isLength = true;
for (int i = 1; i < nCount; ++i)
{
int nSize1 = vecInput[i - 1].size(), nSize2 = vecInput[i].size(), nIndex = 0;
while (nIndex < nSize1 && nIndex < nSize2)
{
if (vecInput[i - 1][nIndex] > vecInput[i][nIndex])
{
isLex = false;
break;
}
else if (vecInput[i - 1][nIndex] < vecInput[i][nIndex])
break;
++nIndex;
}
if (nIndex < nSize1 && nIndex == nSize2)
isLex = false;
if (isLex == false)
break;
}
for (int i = 1; i < nCount; ++i)
{
if (vecInput[i].size() < vecInput[i - 1].size())
{
isLength = false;
break;
}
}
if (isLex && isLength)
cout << "both" << endl;
else if (isLex)
cout << "lexicographically" << endl;
else if (isLength)
cout << "lengths" << endl;
else
cout << "none" << endl;
}
}
| |
//每次和前一个比就行了 不需要数组
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Integer n = Integer.parseInt(scanner.nextLine());
boolean lexicographically = true;
boolean lengths = true;
String pre = "";
for(int i=0;i<n;i++){
String s = scanner.nextLine();
lengths = lengths&&s.length()>=pre.length();
lexicographically = lexicographically&&s.compareTo(pre)>=0;
pre = s;
}
if(lexicographically&&lengths) System.out.println("both");
else if(lexicographically) System.out.println("lexicographically");
else if(lengths) System.out.println("lengths");
else System.out.println("none");
} import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); List<String> l=new ArrayList<String>();
int aa=s.nextInt(); while(s.hasNext()) { String m=s.nextLine(); l.add(m); } if((isABC(l))&&(isLength(l))) { System.out.println("both"); }else if((isABC(l)==false)&&(isLength(l)==true)) { System.out.println("lengths"); } else if((isABC(l)==true)&&(isLength(l)==false)) { System.out.println("lexicographically"); } else if((isABC(l)==false)&&(isLength(l)==false)) { System.out.println("none"); } } public static boolean isABC(List al) { boolean f=true; for(int i=0;i<al.size();i++) { if(i+1<al.size()) { String s1=(String)al.get(i); String s2=(String)al.get(i+1); if(s1.compareTo(s2)<0) { continue; } else { f=false; } } } return f; } public static boolean isLength(List al) { boolean f=true; for(int i=0;i<al.size();i++) { if(i+1<al.size()) { String s1=(String)al.get(i); String s2=(String)al.get(i+1); if(s2.length()-s1.length()>0) { continue; } else { f=false; } } } return f; }
}
import java.util.Scanner;
public class TwoSort2 {
public static boolean bol = true;
public static boolean abc = true;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = Integer.parseInt(sc.nextLine());
String[] str = new String[n];
for (int i = 0; i < n; i++) {
str[i] = sc.nextLine();
}
getResult(str);
if (abc && bol) {
System.out.println("both");
} else if (abc && !bol) {
System.out.println("lexicographically");
} else if (!abc && bol) {
System.out.println("lengths");
} else
System.out.println("none");
}
}
public static void getResult(String[] arr) {
int x = 0;
char a, b;
for (int i = 0; i < arr.length; i++) {
int y = 0;
boolean j = true;
if (x < arr[i].length()) {
x = arr[i].length();
} else {
bol = false;
}
if (i < arr.length - 1) {
while (j) {
if (y < arr[i].length() && y < arr[i + 1].length()) {
a = arr[i].charAt(y);
b = arr[i + 1].charAt(y);
if (a == b) {
j = true;
y++;
} else if (a > b) {
abc = false;
j = false;
} else if (a < b) {
j = false;
}
}
else if(y >= arr[i].length() && y<arr[i+1].length()) {
j=false;
}
else if(y <= arr[i].length() && y >= arr[i + 1].length()) {
abc = false;
j = false;
}
}
}
}
}
}
#include <iostream>
using namespace std;
string s[110];
int main()
{ int n; cin>>n>>s[0]; string t = s[0]; int l = t.length(); bool flag1 = true, flag2 = true; for(int i=1;i<n;i++) { cin>>s[i]; if(s[i] < t) flag1 = false; t = s[i]; if(s[i].length() < l) flag2 = false; l = t.length(); } if(flag1 && flag2) cout<<"both"<<endl; else if(flag1 && !flag2) cout<<"lexicographically"<<endl; else if(!flag1 && flag2) cout<<"lengths"<<endl; else cout<<"none"<<endl; return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int n,notLen=0,notLex=0;
cin>>n;
string s;
vector<string> vs;
for(int i=0;i<n;i++){
cin>>s;
vs.push_back(s);
}
for(int i=0;i<vs.size()-1;i++){
if(vs[i].compare(vs[i+1])>0){
notLex=1;
}
if(vs[i].size()>vs[i+1].size()){
notLen=1;
}
if(notLen==1&¬Lex==1) break;
}
if(notLex==1&¬Len==1){
cout<<"none"<<endl;
}else if(notLex==1&¬Len==0){
cout<<"lengths"<<endl;
}else if(notLex==0&¬Len==1){
cout<<"lexicographically"<<endl;
}else if(notLex==0&¬Len==0){
cout<<"both"<<endl;
}
return 0;
}