标签:io ar sp java for 数据 on art bs
package com.tang.kuohao;
import java.util.InputMismatchException;
import java.util.Scanner;
public class KuoHaoDui {
public static String STRINGA = "{";
public static String STRINGB = "[";
public static String STRINGC = "(";
public static String STRINGAA = "}";
public static String STRINGBB = "]";
public static String STRINGCC = ")";
/*
* 描述 现在,有一行括号序列,请你检查这行括号是否配对。
*
* 输入
* 第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,
* 且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符 输出
* 每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
*/
public static void main(String[] args) {
try {
System.out.println("请输入一个整数:");
Scanner s = new Scanner(System.in);
int str = s.nextInt();
if (str > 100) {
System.out.println("不能大于100组测试数据");
return;
}
for (int i = 0; i < str; i++) {
System.out.println("请输入第" + (i + 1) + "测试数据组:");
Scanner si = new Scanner(System.in);
String sp = si.next();
int cc = new KuoHaoDui().isHefa(sp);
if (cc == 0) {
String sc = new KuoHaoDui().dataReturn(sp);
if (sc.equals("true")) {
System.out.println("Yes");
} else if (sc.equals("false")) {
System.out.println("No");
}
} else {
System.out.println("数据不合法,请检查!");
}
}
} catch (InputMismatchException e) {
// TODO: handle exception
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String dataReturn(String sp) {
String returnFlag = "";
int start = 0;
int end = sp.length();
int qumo = end % 2;
if (qumo != 0) {
return "false";
}
for (int i = 0; i < end; i++) {
String cc = sp.substring(start, start + 1);
if (cc.equals(STRINGA)) {
returnFlag = isEquersStr(cc, STRINGAA, sp);
} else if (cc.equals(STRINGAA)) {
returnFlag = isEquersStr(cc, STRINGA, sp);
} else if (cc.equals(STRINGB)) {
returnFlag = isEquersStr(cc, STRINGBB, sp);
} else if (cc.equals(STRINGBB)) {
returnFlag = isEquersStr(cc, STRINGB, sp);
} else if (cc.equals(STRINGC)) {
returnFlag = isEquersStr(cc, STRINGCC, sp);
} else if (cc.equals(STRINGCC)) {
returnFlag = isEquersStr(cc, STRINGC, sp);
} else {
returnFlag = "nohf";
}
start++;
}
return returnFlag;
}
private int getChuXianCiShu(String str, String sp) {
int countSize = 0;
int spit = 0;
for (int i = 0; i < sp.length(); i++) {
String strChar = sp.substring(spit, spit + 1);
if (str.equals(strChar)) {
countSize++;
}
spit++;
}
return countSize;
}
private String isEquersStr(String str1, String str2, String sp) {
String sjhf = "true";
int a = getChuXianCiShu(str1, sp);
int b = getChuXianCiShu(str2, sp);
if (a != b) {
sjhf = "false";
}
return sjhf;
}
private int isHefa(String sp) {
int cc=0;
int spit = 0;
for (int i = 0; i < sp.length(); i++) {
String strChar = sp.substring(spit, spit + 1);
if (!(strChar.equals(STRINGA) || strChar.equals(STRINGAA)
|| strChar.equals(STRINGB) || strChar.equals(STRINGBB)
|| strChar.equals(STRINGC) || strChar.equals(STRINGCC))) {
cc++;
}
spit++;
}
return cc;
}
}
标签:io ar sp java for 数据 on art bs
原文地址:http://www.cnblogs.com/supertang/p/4116436.html