您的位置:首页 > 编程语言 > Lua

Problem C. Evaluation Google APAC 2017 University Test Round C

2017-02-03 12:48 519 查看
这一题现将string expression转换成一个图,在判断是否存在拓扑序就OK了。我特判了一下a<->a的环。

拓扑序初始化(将入度=0的点push进队列)过程中,要判断这些点是否都为函数。如果只是变量是不成立的,因为该变量无法被计算出。

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>

using namespace std;

//2017 RoundC Problem C. Evaluation
int T;
int N;
int idx;
const int maxn=2010;
//string lines[maxn];
map<string,int>stoi;
vector<int>mp[maxn];
int funnode[maxn];
int deg[maxn];
bool ans;
vector<string> findall(string line)
{
vector<string>vec=vector<string>();
int st=line.find("(");
int ed0=line.find(")");
if(ed0-st==1)//no para
{
return vec;
}
int ed1=line.find_first_of(",");
if(ed1==-1)//one para
{
vec.push_back(line.substr(st+1,ed0-st-1));
return vec;
}
else
{
bool exit=false;
line=line.substr(st+1);
while(!exit)
{
int ed=line.find_first_of(",");
if(ed==-1)
{
ed=line.find_first_of(")");
exit=true;
//break;
}
//cout<<"ed "<<ed<<endl;
//vec.push_back(line.substr(st+1,ed-st-1));
//cout<<line.substr(0,ed)<<endl;
vec.push_back(line.substr(0,ed));
st=line.find_first_of(",");
if(st==-1)
{
break;
}
//cout<<"st "<<st<<endl;
st++;
line=line.substr(st);
//cout<<"line "<<line<<endl;
}
}
return vec;
}
int getid(string node)
{
int nodeid=stoi[node];
//cout<<node<<" getid1 "<<nodeid<<endl;
if(nodeid==0)
{
stoi[node]=idx;
nodeid=idx;
idx++;
}
//cout<<node<<" getid2 "<<nodeid<<endl;
return nodeid;
}
void toporder()
{
int counted=0;
queue<int>q;
for(int i=1;i<idx;i++)
{
//cout<<deg[i]<<endl;
if(deg[i]==0)
{
if(funnode[i]==1)
{
q.push(i);
}
else
{
//cout<<"here1"<<endl;
ans=false;
return;
}
}
}
if(q.empty())
{
//cout<<"here2"<<endl;
ans=false;
return;
}
while(!q.empty())
{
int u=q.front();
q.pop();
counted++;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
deg[v]--;
if(deg[v]==0)
{
q.push(v);
}
}
}
if(counted!=idx-1)
{
//cout<<"here3"<<endl;
ans=false;
}
else
{
ans=true;
}
}
int main()
{
freopen("C-large-practice.in","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d", &T);
for(int ca=1;ca<=T;ca++)
{
scanf("%d",&N);
memset(mp,0,sizeof(mp));
memset(funnode,0,sizeof(funnode));
memset(deg,0,sizeof(deg));
stoi.clear();
idx=1;
ans=true;
for(int i=0;i<N;i++)
{
string line;
cin>>line;
string left=line.substr(0,line.find("=",0));
//cout<<"l "<<left<<endl;
int leftnode=getid(left);
vector<string>right=findall(line);
if(right.size()==0)
{
string funright=line.substr(line.find("=")+1);
//cout<<"fr "<<funright<<endl;
int funrightnode=getid(funright);
mp[funrightnode].push_back(leftnode);
funnode[funrightnode]=1;
deg[leftnode]++;
}
else
{
for(int i=0;i<right.size();i++)
{
int rightnode=getid(right[i]);
mp[rightnode].push_back(leftnode);
deg[leftnode]++;//indegree
if(rightnode==leftnode)//there exists a circle a <-> a
{
ans=false;
}
//cout<<"r "<<right[i]<<endl;
}
}
}
//cout<<"idx "<<idx<<endl;
if(ans==false)
{
printf("Case #%d: BAD\n",ca);
}
else
{
toporder();
printf("Case #%d: %s\n",ca,ans==true?"GOOD":"BAD");
}
//cout<<"======"<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: