真题来源:化学方程式
Java满分题解:
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Main {
static final int N=2510;
static int n,m;
static int id;
static int len,mid;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
String str = bf.readLine();
n=Integer.parseInt(str);
for(int z=0;z<n;z++) {
str=bf.readLine();
char[] s=str.toCharArray();
len=s.length;
for(int i=0;i<len;i++) {
if(s[i]=='=') {
mid=i;
break;
}
}
id=0;
Map<String,Integer> mp = new HashMap<>();
List<Pair<String,Integer> > arr = calculate(s,mid);
for(Pair<String,Integer> u:arr) {
String key = u.first;
Integer val = u.second;
// pw.print(u.first+" "+u.second+" ");
if(mp.containsKey(key)){
mp.replace(key, mp.get(key)+val);
}
else mp.put(key,val);
}
// pw.println();
id=mid+1;
List<Pair<String,Integer> > arr2 =calculate(s,len);
for(Pair<String,Integer> u:arr2) {
String key = u.first;
Integer val = u.second;
// pw.print(u.first+" "+u.second+" ");
if(mp.containsKey(key)){
mp.replace(key, mp.get(key)-val);
}
else mp.put(key,val);
}
// pw.println();
boolean flag=true;
for(Entry<String,Integer> u:mp.entrySet()) {
Integer val=u.getValue();
if(val!=0) {
pw.println("N");
flag=false;
break;
}
}
if(flag)pw.println("Y");
}
pw.flush();
}
public static List<Pair<String,Integer> > calculate(char[] str,int len) {
List<Pair<String,Integer> > arr = new ArrayList<>();
while(id<len) {
if(str[id]=='+')id++;
int coef=getDigit(str,len);
List<Pair<String,Integer> > arr2 = calculateFormula(str,len);
add2All(arr2,arr,coef);
}
return arr;
}
public static List<Pair<String,Integer> > calculateFormula(char[] str,int len){
List<Pair<String,Integer> > arr = new ArrayList<>();
while(id<len) {
if(str[id]=='+')break;
if(str[id]=='(') {
int left=1;
int t=id+1;
while(true) {
id++;
if(str[id]=='(')left++;
else if(str[id]==')')left--;
if(left==0) {
int len2=id;
id=t;
List<Pair<String,Integer> > arr2 =calculateFormula(str,len2);
id=len2+1;
int coef2 = getDigit(str,len);
add2All(arr2,arr,coef2);
break;
}
}
}
else {
String key = getElement(str,len);
int coef2 = getDigit(str,len);
arr.add(new Pair(key,coef2));
}
}
return arr;
}
public static void add2All(List<Pair<String,Integer> > a,List<Pair<String,Integer> > b ,int x) {
for(Pair<String,Integer> u:a) {
b.add(new Pair(u.first,u.second*x));
}
}
public static int getDigit(char[] str,int len) {
int coef=1;
if(id<len&&isDigit(str[id])) {
coef=0;
while(id<len&&isDigit(str[id])) {
coef=coef*10+str[id]-'0';
id++;
}
}
return coef;
}
public static String getElement(char[] str,int len) {
StringBuffer sb= new StringBuffer();
sb.append(str[id]);
id++;
while(id<len&&isLowerCase(str[id])) {
sb.append(str[id]);
id++;
}
return sb.toString();
}
public static boolean isDigit(char c) {
if(c>='0'&&c<='9')return true;
return false;
}
public static boolean isUpperCase(char c) {
if(c>='A'&&c<='Z')return true;
return false;
}
public static boolean isLowerCase(char c) {
if(c>='a'&&c<='z')return true;
return false;
}
public static class Pair<F,S>{
F first;
S second;
public Pair(F first,S second) {
this.first = first;
this.second = second;
}
}
}
运行结果: