Problem C. Evaluation Google APAC 2017 University Test Round C
来源:互联网 发布:红鸟棋牌游戏源码 编辑:程序博客网 时间:2024/06/05 10:18
这一题现将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. Evaluationint 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;}
0 0
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem D. Soldiers Google APAC 2017 University Test Round C
- Round C APAC Test 2017 Problem C. Evaluation
- Problem D. gMatrix Google APAC 2016 University Test Round C
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Problem C. gCampus Google APAC 2016 University Test Round A
- [ Google APAC 2015 University Graduates Test ] Round C APAC Test
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Problem A. Monster Path Google APAC 2017 University Test Round C
- Problem B. Safe Squares Google APAC 2017 University Test Round C
- Problem C. Codejamon Cipher Google APAC 2017 University Test Round D
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- Problem C. Not So Random Google APAC 2017 University Test Practice Round
- Round C APAC Test 2017 Problem C. Evaluation(拓扑排序)
- Problem A. Vote Google APAC 2017 University Test Round D
- PLSQL Trigger Dynamic SQL
- BT——专门为大容量文件的共享而设计的网络协议
- 对于大流量网站的解决方案
- Python产生随机数
- VMware下CentOS7网络重启出错解决方案
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Android状态栏微技巧,带你真正理解沉浸式模式
- 【BZOJ】2657: [Zjoi2012]旅游(journey)
- 1036. 跟奥巴马一起编程(15)
- Python多线程编程
- FTP协议讲解
- 【笔试练习】美团笔试题(一)
- Android笔记->Executors应用学习
- 1037. 在霍格沃茨找零钱(20)