Title Link: http://ac.jobdu.com/problem.php?pid=1082
Detailed Links: https://github.com/zpfbuaa/JobduInCPlusPlus
Reference code:
////1082 Proxy Server. cpp//Jobdu////Created by Pengfei_zheng on 03/05/2017.//copyright©2017 Pengfei_zheng. All rights reserved.//#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<cstring>#include<cmath>#include<climits>#defineMax_size 1001#defineMax_server 5001//#define DEBUGusing namespacestd;intN, M;structserver{//define the struct of server intx, Y, Z, W; BOOL operator== (ConstServer &a)Const{//reload the = = operator return(x==a.x && y==a.y && z==a.z && w==A.W); }}; Server S1[max_size]; Server S2[max_server]; intMain () {#ifdef Debug Freopen ("/users/pengfei_zheng/desktop/input.txt","R", stdin);#endif while(SCANF ("%d", &n)! =EOF) { for(inti =0; I < n; i++) {scanf ("%d.%d.%d.%d",&s1[i].x,&s1[i].y,&s1[i].z,&S1[I].W); } scanf ("%d",&m); for(inti =0; I < m; i++) {scanf ("%d.%d.%d.%d",&s2[i].x,&s2[i].y,&s2[i].z,&S2[I].W); } if(1==n) {//Special Case inti; for(i =0; I < m; i++){ if(s1[0]==S2[i]) { Break; } } if(i<m) printf ("-1\n"); Elseprintf"0\n"); } Else{ BOOLLoopflag =true; intstart=0, ans =0, idx =0; intMaxidx; while(loopflag) {Maxidx= -1; Loopflag=false; for(inti =0; I < n; i++) {idx=start; while(IDX < m &&!) (s1[i]==S2[idx])) {idx++; } maxidx= max (MAXIDX,IDX);//find the last appear server } if(Maxidx < m) {//judge whether this server can reach all of the other serversans++; Loopflag=true; } Start= Maxidx +1;//Update the start IDX} printf ("%d\n", ans); } } return 0;}/************************************************************** problem:1082 User:zpfbuaa language:c++ Re sult:accepted time:10 Ms memory:1612 kb****************************************************************/
Topic 1082: Proxy server (greedy algorithm)