Test Instructions: There is a set of N strings (n≤1000,n is even) that requires finding a string of the shortest length (not within the Set) S, so that exactly half of the string in the set is less than or equal to s, and the other half is greater than S. If there is more than one solution, the output dictionary order is required to minimize the Solution.
solution: Originally I want to analyze the situation with if else implementation, but a lot of details, especially easy to wrong. It turned out that way. ╮ (╯_╰) ╭ then look at the search line, because the dictionary order is required to the smallest, that is, as small as possible characters as small as Possible. And to set exactly half of the string is less than or equal to s, the other half is greater than s, that is, after sorting the >= in the middle of the left side of the string and < middle to the right of the String. Then we can sort the middle of the 2 strings from the front start sweep, the occurrence of a different character, the enumeration of ' A ' ~ ' Z ' to see if the conditions are met. If this length of the string is not satisfied, then this bit to fill the current matching the minimum character, and then the next search, that is, like bfs, each of the search is complete to the next bit.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 8 Const intn=1010, l=510;9 intn;Ten Chars[l]; one structnode{Chars[l];intl;} a[n]; a - BOOLCMP (node X,node Y) {returnstrcmp (x.s,y.s) <0;} - voidBFsintXintYintI) the { - if(i>a[y].l&&i>a[x].l)return;//&& - intt; - if(i<a[x].l) t=a[x].s[i]-'A'; + Elset=0; - for(intj=t;j< -; j + +) + { as[i]='A'+j; at if(STRCMP (a[x].s,s) <=0&& strcmp (a[y].s,s) >0)return; - } -s[i]='A'+t;// -BFS (x,y,i+1); - } - intMain () in { - while(1) to { +scanf"%d",&n); - if(!n) break; the for(intI=1; i<=n;i++) * { $scanf"%s", a[i].s);Panax NotoginsengA[i].l=strlen (a[i].s); - } theSort (a +1, A +1+n,cmp); +Memset (s),' /',sizeof(s)); a intX= (1+n)/2, y=x+1; the BOOLok=false; + intt=0; - for(intI=0; i<a[x].l;i++) $ { $ intu=a[x].s[i]-'A', v=a[y].s[i]-'A'; - if(u!=v) {bfs (x,y,i); break;} -s[i]=a[x].s[i]; the } -printf"%s\n", s);Wuyi } the return 0; -}
"uva 1610" Party Games (algorithmic Efficiency-constructs Dfs)