CSP201912_3
目录题目
思路
大模拟,考虑先整体以等号为界,将方程式分为左串与右串。
分别针对两个子串,以加号为界分离成独立的化合物,在每个化合物中统计各原子数量。
使用 map 维护。
难点应该是处理括号的嵌套。在忙别的事情,括号嵌套先不处理,连带着不嵌套的括号也先暂时不写了。
先拿60pts..........
似乎写炸了,为什么只有40pts???样例没括号的都过了呀???
要是有dalao一眼看出错误还请直接指正orz
总之抽时间再改,最讨厌大模拟了
Code
#include<bits/stdc++.h>
using namespace std;
int n;
int pos;
map <string, int> ml;
map <string, int> mr;
char sl[1010];
char sr[1010];
int cnt;
string atom[1010];
bool is_num(char c)
{
if(c >= '0' && c <= '9') return true;
return false;
}
bool is_A(char c)
{
if(c >= 'A' && c <= 'Z') return true;
return false;
}
bool is_a(char c)
{
if(c >= 'a' && c <= 'z') return true;
return false;
}
void Cal(char* s, map <string, int> &m)
{
int len = strlen(s);
int l = 0, r = l;
while(l < len)
{
if(s[r] != '+' && r < len)
{
r++;
continue;
}
else
{
//cout << "l = " << l << " " << "r = " << r << endl;
int sum = 0;
for(int i = l; i < r; i++)
{
if(i == l)
{
while(is_num(s[i]))
{
sum = (sum<<1) + (sum<<3) + (s[i]^48);
i++;
}
}
if(! sum) sum = 1;
if(is_A(s[i])) //判断直接的单原子或双原子
{
if(is_a(s[i + 1]))//双原子
{
char tmp[3] = {s[i], s[i + 1], '\0'};
atom[++cnt] = tmp;
if(is_num(s[i + 2]))
{
m[tmp] += ((s[i + 2]^48) * sum);
i += 2;
continue;
}
else
{
m[tmp] += sum;
i += 1;
continue;
}
}
else//单原子
{
char tmp[2] = {s[i], '\0'};
atom[++cnt] = tmp;
if(is_num(s[i + 1]))
{
m[tmp] += ((s[i + 1]^48) * sum);
i += 1;
continue;
}
else
{
m[tmp] += sum;
continue;
}
}
}
if(s[i] == '(')
{
continue;
}
if(s[i] == ')')
{
continue;
}
}
}
l = (++r);
}
/*for(int i = 1; i <= cnt; i++)
{
cout << atom[i] << " " << m[atom[i]] << endl;
}*/
return;
}
int main()
{
cin >> n;
cin.ignore();
while(n--)
{
pos = 0;
memset(sl, 0, sizeof(sl));
memset(sr, 0, sizeof(sr));
char ch = getchar();
while(ch != '=')
{
sl[pos++] = ch;
ch = getchar();
}
pos = 0;
ch = getchar();
while(ch != '\n')
{
sr[pos++] = ch;
ch = getchar();
}
//cout << sl << endl << sr << endl;
cnt = 0;
Cal(sl, ml);
cout << "L Finish" << endl;
cnt = 0;
Cal(sr, mr);
cout << "R Finish" << endl;
if(ml.size() != mr.size())
{
cout << "N" << endl;
ml.clear();
mr.clear();
continue;
}
bool flag = true;
map <string, int> :: iterator it = ml.begin();
while(it != ml.end())
{
if(it->second != mr[it->first])
{
flag = false;
cout << "N" << endl;
break;
}
it++;
}
if(flag) cout << "Y" << endl;
ml.clear();
mr.clear();
}
return 0;
}
/*
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au
*/
//2AaC2+2B2Ax2D=2B2DAa+2C+Ax4C2
标签:ch,int,pos,while,&&,CSP201912
From: https://www.cnblogs.com/kevin-chance/p/16727788.html