Contest Topic Link
Test Instructions: There are n individuals each holding a gun trying to kill N Monsters, capital letters representing people, and small letters representing monsters. a can only kill a,b can only kill B, as shown in the picture in the title, the gun trajectory can not cross . The numbers of people and monsters are 1 to n, and ask if there is a case that kills all, and if it does, the number of each person killed by the output number 1 to n , if not output " Impossible".
The following: greedy, with recursive implementation, to determine whether the adjacent can constitute a pair, priority to form adjacent, if not recursive to the front to see if it can constitute a pair can.
#include <cstdio>#include<cstring>#include<queue>using namespacestd;intn,e;Chardata[5005*2];intnext[5005*2];BOOLeqCharACharb) {BOOLTa=0, tb=0; if(a>='A'&&a<='Z') {a-='A'; Ta=1; } if(a>='a'&&a<='Z') a-='a'; if(b>='A'&&b<='Z') {b-='A'; TB=1; } if(b>='a'&&b<='Z') b-='a'; if(TA&&TB)return false; if(ta==false&& tb==false)return false; returna==b;}voidSolveints) {//printf ("%d%d\n", s,e);e++; if(e>=n)return; while(e<n&&!eq (data[s],data[e])) {Solve (E); } if(e>=n)return; Next[s]=e; Next[e]=s; E++;}intMain () {scanf ("%d",&N); scanf ("%s", data); N*=2; memset (Next,-1,sizeof(Next)); E=0; while(e<=N) solve (e); for(intI=0; i<n;i++) { //printf ("%d", Next[i]); if(next[i]==-1) {puts ("Impossible"); return 0; } } //puts (""); intgid[5005*2],g=1; for(intI=0; i<n;i++) { if(data[i]>='a'&&data[i]<='Z') {Gid[i]= g++; } } for(intI=0; i<n;i++) { if(data[i]>='A'&&data[i]<='Z') {printf ("%d", Gid[next[i]]); }} puts ("");}
URAL 2019 Pair:normal and Paranormal (greedy)-gdut United seventh Field