Problem C. Evaluation Google APAC 2017 University Test Round C

这一题现将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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值