Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 460 & page = show_problem & problem = 4072
Question:
Select as many strings as possible for a string consisting of n upper-case letters, so that each upper-case letter can appear an even number of times.
Ideas:
Once we see the time limit: 18.000 seconds, we wrote one directly without any optimization, and there was more than 9 s. It is estimated that it was the longest time-consuming AC in history.
Then I thought about how to optimize it and found that the number of occurrences of all letters can be expressed in binary. 0 indicates an even number, and 1 indicates an odd number. In this way, all the selected string states are performed or computed once. If the result is 0, all strings are even.
In this way, it is reduced from 9s to 1.692 S.
The competition guide introduces the more efficient "encounter in the middle": divide the string into two parts. First, calculate the XOR value obtained from all the combinations of the First N/two strings, save it in settings map, and then find the same value as the previous one after n/2 characters after enumeration.
// Ultraviolet A 1326 Jurassic remains // compress bit operations # include <cstdio> # include <cstring> # include <iostream> # include <cctype> using namespace STD; int N; char STR [30]; int st [30]; bool vis [30]; int DFS (INT cur, int CNT, int Sta) {If (cur = N) {If (! Sta) return CNT; Return-1;} If (cur <n) {vis [cur] = true; int res = DFS (cur + 1, CNT + 1, sta ^ st [cur]); If (res! =-1) return res; vis [cur] = false; Res = DFS (cur + 1, CNT, Sta); If (res! =-1) return res;} int main () {While (~ Scanf ("% d", & N) {memset (St, 0, sizeof (ST); For (INT I = 0; I <n; ++ I) {scanf ("% s", STR); For (Int J = 0; STR [J]; ++ J) {st [I] ^ = (1 <(STR [J]-'A') ;}} memset (VIS, 0, sizeof (VIS )); printf ("% d \ n", DFS (0, 0, 0); bool first = true; For (INT I = 0; I <n; ++ I) if (vis [I]) {first? First = false: putchar (''); printf (" % d ", I + 1);} putchar ('\ n');} return 0 ;}
Code 2: Encounter (recursive version ):
#include<cstdio>#include<cstring>#include<map>#include<iostream>using namespace std;const int MAXN = 30;int n, vis;int st[MAXN];char str[MAXN];map<int, int>table;map<int, int>::iterator it;int ansCnt, ansVis;inline int bitCount(int x){int cnt = 0;while(x>0){ if(x&1) ++cnt;x >>= 1;}return cnt;}void dfs1(int cur, int n, int vis, int sta){it = table.find(sta);if(it != table.end()){if(bitCount(it->second) < bitCount(vis)){it->second = vis;}}else{table[sta] = vis;}if(cur < n){dfs1(cur+1, n, vis|(1<<cur), sta^st[cur]);dfs1(cur+1, n, vis, sta);}}void dfs2(int cur, int n, int vis, int sta){it = table.find(sta);if(it != table.end()){int cnt = bitCount(vis+it->second);if(cnt > ansCnt){ansCnt = cnt;ansVis = vis+table[sta];}}if(cur < n){dfs2(cur+1, n, vis|(1<<cur),sta^st[cur]);dfs2(cur+1, n, vis, sta);}}int main(){int i,j;while(~scanf("%d", &n)){memset(st, 0, sizeof(st));table.clear();for(i=0; i<n; ++i){scanf("%s", str);for(j=0; str[j]; ++j){st[i] ^= (1<<(str[j]-'A'));}}dfs1(0, (n>>1), 0, 0);ansCnt=0, ansVis=0;dfs2(n/2, n, 0, 0);printf("%d\n", ansCnt);bool first=true;for(i=0; i<n; ++i)if((ansVis>>i)&1){first ? first=false : putchar(' ');printf("%d", i+1);}putchar('\n');}return 0;}
Method of encounter in version 3 (directly enumerating binary States without recursion ):
# Include <cstdio> # include <cstring> # include <map> # include <iostream> using namespace STD; const int maxn = 30; int N, VIS; int st [maxn]; char STR [maxn]; Map <int, int> table; Map <int, int>: iterator it; int anscnt, ansvis; inline int bitcount (int x) {int CNT = 0; while (x> 0) {If (X & 1) + + CNT; X >>=1 ;} return CNT;} int main () {int I, j; while (~ Scanf ("% d % * C", & N) {memset (St, 0, sizeof (ST); table. clear (); for (I = 0; I <n; ++ I) {gets (STR); For (j = 0; STR [J]; ++ J) {st [I] ^ = (1 <(STR [J]-'A '));}} // n/2 combination statuses before enumeration int end = (1 <(n> 1); for (I = 0; I <end; ++ I) {int sta = 0; For (j = 0; j <(n> 1); ++ J) if (I & (1 <j )) {sta ^ = sT [J];} It = table. find (STA); If (it! = Table. end () {If (bitcount (IT-> second) <bitcount (I) {It-> second = I ;}} else {table [sta] = I ;}} anscnt = 0, ansvis = 0; end = (1 <(n-n/2); for (I = 0; I <end; ++ I) {int sta = 0; For (j = (n> 1); j <n; ++ J) if (I & (1 <(J-(n> 1) {sta ^ = sT [J];} It = table. find (STA); If (it! = Table. end () {int vis = I <(n> 1); int CNT = bitcount (vis + It-> second); If (CNT> anscnt) {anscnt = CNT; ansvis = vis + It-> second ;}} printf ("% d \ n", anscnt); bool first = true; for (I = 0; I <n; ++ I) if (ansvis> I) & 1) {first? First = false: putchar (''); printf (" % d ", I + 1);} putchar ('\ n');} return 0 ;}