Topic Link: Dividing a String
Test instructions: Given a string of 2*n (n<=20) and the weight contained by the characters in each position, divide the string into two subsequence S1, T1, S1=T1, and ABS (WEIGHT1-WEIGHT2) as small as possible.
Analysis: The string is divided into the first half and the second half of the process, for the first half of the string from left to right enumeration of each character belongs to the subsequence S1 or T1, the sub-sequence belonging to S1 is set to a, the sub-sequence of the T1 is set to B, the length of a is not greater than B, then only if A is For the second half of the right-to-left enumeration, requires B is not greater than A and B is a prefix, then set their extra part is C and C ', then C and C ' order reciprocal s1=t1. So the map saves the string C and the Upside C ' (that is, c) and sorts it out.
When working with string C, it is best to refer to the STL map at 14258ms, and the same code is T, and then write a hashmap,10842ms is properly AC.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#defineLL Long Longusing namespacestd;Const intINF =0x3f3f3f3f;Const intSEED =171;ConstLL MOD =100000000000007ll;Const intN =1500000+7;inta[ -],b[ -];vector<int>Val[n];Charstr[ -];intnuma,numb,num,x[ -],y[ -];intGethash (Charstr[]) {LL res=str[0]; intlen=strlen (str); for(intI=1; i<len;i++) {res= (res*seed+str[i]+mod)%MOD; } returnres%N;}structhashmap{intHead[n],next[n],id[n],key[n]; inttot; voidinit () {memset (key,-1,sizeof(key)); memset (ID,0,sizeof(ID)); memset (Head,-1,sizeof(head)); Tot=0; } intFindCharstr[]) { intu=Gethash (str); for(intI=head[u];~i;i=Head[i]) { if(Key[i]==u)returnId[i]; } return 0; } voidInsertCharStr[],intx) {intu=Gethash (str); Key[tot]=u;id[tot]=x;next[tot]=head[u];head[u]=tot++; }}hash;intSolveintAintx) { intsz=val[a].size (); if(sz==0)returninf; intlow=0, high=sz-1, mid,ans=-1; while(low<=High ) {Mid= (Low+high) >>1; if(val[a][mid]>=x) {ans=mid; High=mid-1; } ElseLow=mid+1; } if(ans==-1)returnABS (x-val[a][sz-1]); Else if(ans==0)returnABS (x-val[a][0]); Else returnMIN (ABS (X-val[a][ans]), ABS (x-val[a][ans-1]));}intMain () {intN; while(SCANF ("%d",&N) {scanf ("%s", str); Num=0;//mp.clear ();Hash.init (); for(intI=0;i<2*n;i++) scanf ("%d", &a[i]), b[i]=str[i]-'a', num+=B[i]; if(num&1) {puts ("-1");Continue; } for(intI=0;i<1<<n;i++) val[i].clear (); intsz=1; for(ints=0;s<1<<n;s++) { inttmp=__builtin_popcount (s); if(tmp>n-tmp)Continue; Numa=numb=num=0; for(intj=0; j<n;j++) { if(s& (1<<j)) {X[numa++]=B[j]; Num-=A[j]; } Else{y[numb++]=B[j]; Num+=A[j]; } } if(numa<=numb) { intflag=0; for(intk=0; k<numa;k++)if(x[k]!=Y[k]) {Flag=1; Break; } if(flag)Continue; intsiz=0; for(intk=numa;k<numb;k++) str[siz++]='0'+Y[k]; Str[siz]=0; //if (!mp[str]) mp[str]=sz++; inttemp=hash.find (str); if(!temp) {Hash.insert (STR,SZ); Temp=sz++; } intsuf=temp; Val[suf].push_back (num); } } for(intI=0; i<sz;i++) sort (Val[i].begin (), Val[i].end ()); intans=inf; for(ints=0;s<1<<n;s++) { inttmp=__builtin_popcount (s); if(tmp>n-tmp)Continue; Numa=numb=num=0; for(intj=n-1; j>=0; j--) { if(s& (1<<j)) {X[numa++]=b[n+J]; Num-=a[n+J]; } Else{y[numb++]=b[n+J]; Num+=a[n+J]; } } if(numa<=numb) { intflag=0; for(intk=0; k<numa;k++)if(x[k]!=Y[k]) {Flag=1; Break; } if(flag)Continue; intsiz=0; for(intk=numb-1; k>=numa;k--) str[siz++]='0'+Y[k]; Str[siz]=0; //if (!mp[str]) continue; inttemp=hash.find (str); if(!temp)Continue; intsuf=temp; Ans=min (Solve (suf,num), ans); } } if(Ans==inf) puts ("-1"); Elseprintf"%d\n", ans); }}
View Code
Hdu 4963 (Midway Encounter method)