import java.io.*;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main
{
private static Map<String, Integer> handle(String expr) {
// 系数
Map<String, Integer> map = new HashMap<>();
if (expr.equals("")) return map;
int Coef = 0, i = 0;
while (Character.isDigit(expr.charAt(i))) {
Coef = Coef * 10 + expr.charAt(i) - '0';
i++;
}
if (Coef == 0) Coef = 1;
StringBuilder element = new StringBuilder();
while (i < expr.length()) {
// 元素后面的系数
int coef = 0;
char c = expr.charAt(i++);
// i 为左括号
if (c == '(') {
int start = i;
int parentheses = 1;
while (i < expr.length() && parentheses != 0) {
c = expr.charAt(i++);
if (c == '(') {
parentheses++;
}
if (c == ')') {
parentheses--;
}
}
int end = i - 1;
// c 可能为系数ceof
Map<String, Integer> subMap = handle(expr.substring(start, end));
if (i < expr.length()) {
c = expr.charAt(i++);
while (Character.isDigit(c)) {
coef = coef * 10 + c - '0';
if (i < expr.length()) c = expr.charAt(i++);
else break;
}
if (c >= 'A' && c <= 'Z' || c == '(') {
i--;
}
}
for (String key : subMap.keySet()) {
map.put(key, map.getOrDefault(key, 0) + subMap.get(key) * Coef * (coef == 0 ? 1 : coef));
}
}
// c 为大写字母
else {
element.append(c);
if (i < expr.length()) {
c = expr.charAt(i++);
if (c >= 'a' && c <= 'z') {
element.append(c);
if (i < expr.length()) c = expr.charAt(i++);
}
// 计算元素后面的系数
while (Character.isDigit(c)) {
coef = coef * 10 + c - '0';
if (i < expr.length()) c = expr.charAt(i++);
else break;
}
if (c >= 'A' && c <= 'Z' || c == '(') {
i--;
}
}
map.put(element.toString(), map.getOrDefault(element.toString(), 0) + Coef * (coef == 0 ? 1 : coef));
element.setLength(0);
}
}
return map;
}
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
while (n-- > 0) {
String equation = sc.next();
String[] expr = equation.split("=");
String[] left = expr[0].split("\\+");
String[] right = expr[1].split("\\+");
Map<String, Integer> map = new HashMap<>();
for (String expression : left) {
Map<String, Integer> subMap = handle(expression);
for (String key : subMap.keySet()) {
map.put(key, map.getOrDefault(key, 0) + subMap.get(key));
}
}
for (String expression : right) {
Map<String, Integer> subMap = handle(expression);
for (String key : subMap.keySet()) {
map.put(key, map.getOrDefault(key, 0) - subMap.get(key));
}
}
String ans = "Y";
for (String key : map.keySet()) {
if (map.get(key) != 0) {
ans = "N";
break;
}
}
out.println(ans);
}
out.flush();
}
}
class Scanner{
private BufferedReader reader;
private StringTokenizer st;
public Scanner(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next(){
while(st == null || !st.hasMoreTokens()){
try {
String line = reader.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
} catch (Exception e) {
throw (new RuntimeException());
}
}
return st.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
}
[Java]化学反応式201912-3
おすすめ
転載: blog.csdn.net/weixin_41714373/article/details/109051931
おすすめ
ランキング