ZJNU 1350 - 分子式 (模拟)
作者:互联网
题面
思路
模拟题,计算给定的分子式种类即可
数据还行,没有卡人的点和什么不合法的点,虽然写得不舒服但一发过
判断函数分开写,便于调用;然后对于分子式的每一层括号内部可以通过递归来实现处理
最后我们需要处理的只有两种:单个原子式(与数字)和括号内的分子式(与数字)
#include<bits/stdc++.h>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
#define all(a) (a).begin(),(a).end()
#define SUM(a) accumulate(all(a),0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define mst(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const ll mod=998244353;
const int dx[8]={0,1,0,-1,1,1,-1,-1},dy[8]={1,0,-1,0,1,-1,1,-1};
void debug(){cerr<<'\n';}template<typename T,typename... Args>void debug(T x,Args... args){cerr<<"[ "<<x<< " ] , ";debug(args...);}
mt19937 mt19937random(std::chrono::system_clock::now().time_since_epoch().count());
ll getRandom(ll l,ll r){return uniform_int_distribution<ll>(l,r)(mt19937random);}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll qmul(ll a,ll b){ll r=0;while(b){if(b&1)r=(r+a)%mod;b>>=1;a=(a+a)%mod;}return r;}
ll qpow(ll a,ll n){ll r=1;while(n){if(n&1)r=(r*a)%mod;n>>=1;a=(a*a)%mod;}return r;}
ll qpow(ll a,ll n,ll p){ll r=1;while(n){if(n&1)r=(r*a)%p;n>>=1;a=(a*a)%p;}return r;}
unordered_map<string,int> mp;
void input()
{
string s;
while(getline(cin,s))
{
if(s=="END_OF_FIRST_PART")
return;
stringstream ss;
ss<<s;
ss>>s;
int d;
ss>>d;
mp[s]=d;
}
}
string s;
int len;
inline bool upp(int p){
return s[p]>='A'&&s[p]<='Z';
}
inline bool low(int p){
return s[p]>='a'&&s[p]<='z';
}
inline bool dig(int p){
return s[p]>='0'&&s[p]<='9';
}
inline bool pr(int p){
return p<len;
}
int sol(int i)
{
//assert(upp(i));
int r=0;
while(true)
{
if(!pr(i)||s[i]==')')
return r;
if(s[i]=='(')
{
int d=sol(i+1);
if(d==-1)
return -1;
i++;
int kh=1;
while(kh>0)
{
if(s[i]=='(')
kh++;
else if(s[i]==')')
kh--;
i++;
}
if(pr(i)&&dig(i))
{
int t=0;
while(pr(i)&&dig(i))
t=t*10+s[i]-'0',i++;
r+=t*d;
//cout<<t<<'\n';
}
else
r+=d;
}
else
{
string ss="";
ss+=s[i];
i++;
if(pr(i)&&low(i))
ss+=s[i],i++;
if(!mp.count(ss))
return -1;
int d=mp[ss];
//cout<<ss<<'\n';
if(pr(i)&&dig(i))
{
int t=0;
while(pr(i)&&dig(i))
t=t*10+s[i]-'0',i++;
r+=t*d;
}
else
r+=d;
}
//cout<<i<<" "<<r<<'\n';
}
return r;
}
void solve()
{
input();
while(getline(cin,s))
{
if(s=="0")
return;
len=s.size();
int d=sol(0);
if(d==-1)
cout<<"UNKNOWN\n";
else
cout<<d<<'\n';
}
}
int main()
{
closeSync;
//multiCase
{
solve();
}
return 0;
}
标签:const,int,分子式,1350,ZJNU,return,mod,ll,define 来源: https://www.cnblogs.com/stelayuri/p/15138980.html