AcWing 3284. JAVA 化学方程式 模拟栈
原题链接
中等
作者:
特困生
,
2021-04-10 11:43:14
,
所有人可见
,
阅读 448
样例
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
static int N;
static int cnt1[] = new int[131];
static int cnt2[] = new int[131];
static char skt[] = new char[1000000];
static int tt;
static char skt2[] = new char[1000000];
static int tt2;
static List<String> oo1 ; //记录每个元素。
static List<String> oo2 ; //记录每个元素
static String pp;
public static void main(String[] args) throws IOException {
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(cin.readLine());
for(int k = 0;k<N;k++){
oo1 = new ArrayList<>(); //每一轮都要初始化
oo2 = new ArrayList<>();
Arrays.fill(cnt1,0);
Arrays.fill(cnt2,0);
String y = cin.readLine();
String s[] = y.split("\\=");
if(k==0) {
pp = y;
}
// if(N==100 && k==7 && pp.equals("TwPcQCjWcXIGDgAxJkFOvEmJuQ+YWQDVhYlLZsRlHpDbUVZjPHmWiUVLWxMeMTNVbPdIMViDpCPF+MHYDIHVZBVKUMMCqWtCViBNHgWnCpOmJNMbJFNCJlChGTkGrAsAvPpAIqVzFnUIdHrIGyXHySITkMOKBCtPhKmGiJVzRvLfVFHbNGETHxKZFPMESQz+JtSoCiKyDZRkQCKROo+DYsQKPdBYHVTDCQHwZeUjDyLRFkRoDXKpGWGbCYE+ITXDjYjVoHPNmETFkLvEyEaAIZv+YdBsPNdQaXPrHNhRErViWPpFqAlFwJoAbODvYALKyLIIlMrNnWBqIWXxFvCiBjNFKSa+JUn+XuDYkXs=LvNXsIl+YdVDHgKCiHHrUjIHySaDjKy+KyFDbKm+Cp+HQNBErZeN+AvYOmPcAUIKTFKUYjHm+K+FkBOvQaAlDpJPFIPdXxDyVhFTkDHZvJoEU+JlPdXRvAFSGHbQJt+OoLCWtEAbEyMWcMCYTwViDgDNPBqViDZsML+QzVIGiCqM+BsFqQXuRlZj+ChRINLHSoCQHxEaYkVoFkJ+ViVVbCjYsVAYXTTkLfRkPh+JuHXAxTDDTDvCtSWxYHwJPrVzNmJMeIdZKOPYlIUGb+BXEIYPWVz+FvJkBLNn+OZMrFwMbNdMVFnC+PpMQNBjNhE+R+RoR+KpW+PZ+IWnDCIPpGr+G+FEmIqGLG+HpGyMVWK+QWCTAsCi+WiWXUn")) System.out.println(y);
work(s[0],cnt1,oo1);
work(s[1],cnt2,oo2);
int flag = 0;
for(int i = 0;i<131;i++){
if(cnt1[i]!=cnt2[i]){
flag = 1;
break;
}
}
// System.out.println(flag);
for(String ss:oo1) {
if(flag ==1) break;
if(!oo2.contains(ss)) flag = 1;
}
if(flag==1) System.out.println("N");
else System.out.println("Y");
}
}
static void work(String s,int[] cnt,List list){
String[] x = s.split("\\+");
for(int i = 0;i<x.length;i++) {
char c[] = x[i].toCharArray();
int k = 1;
for(int j=0;j<c.length;j++) {
if(j==0 && c[0]>='1' && c[0]<='9') {//记录最开始的倍数
k = c[0] - '0';
while(c[j+1]>='0' && c[j+1]<='9') {
k = k*10 + c[j+1]-'0';
j++;
}
continue;
}
if(c[j]>='1' && c[j]<='9') { //如果遇到数字,出栈组合进站。
int d = c[j]-'0';
while(j+1<c.length && c[j+1]>='0' && c[j+1]<='9') {
d= d*10 + c[j+1]-'0';
j++;
}
calcNum(d);
}else if(c[j]==')') {
int d = 1;
if(j+1<c.length && c[j+1]>='1' && c[j+1]<='9') {
d = c[j+1]-'0';
j++;
while(j+1<c.length && c[j+1]>='0' && c[j+1]<='9') {
d= d*10 + c[j+1]-'0';
j++;
}
calcA(d);
} else calcA(1);
}else {
if(c[j]>='a' && c[j]<='z') { //遇到小写字母,需要记录单词
list.add(""+c[j]+skt[tt]);
}
skt[++tt] = c[j];
}
}
// System.out.println(k+"-"+tt);
while(tt!=0) {
char p = skt[tt--];
cnt[p]+=k;
}
}
}
static void calcNum(int x) {
if(skt[tt]>='a' && skt[tt]<='z') {
//处理一个小写字母
char c = skt[tt--];
for(int i = 0;i<x;i++) {
skt2[++tt2] = c;
}
}
//处理前面一个大写字母
char c = skt[tt--];
for(int i = 0;i<x;i++) {
skt2[++tt2] = c;
}
while(tt2!=0) { //赋值
skt[++tt] = skt2[tt2--];
}
}
static void calcA(int x) {
while(skt[tt]!='(') {
char c = skt[tt--];
for(int i = 0;i<x;i++) {
skt2[++tt2] = c;
}
}
tt--; //去掉左括号。
while(tt2!=0) {
skt[++tt] = skt2[tt2--];
}
}
}
这里数据太弱了,csp里面最多有10e9个字母,所以模拟战不能用
使用stack即可