LA 2965 Jurassic Remains / 中途相遇法

來源:互聯網
上載者:User

求盡量多的字串 每種大寫字母出現偶數次

每個字串可以看成一個長度為26 出現奇數次對應位置為1 偶數為0

就是求一些字串 他們的異或為0

n最大為24

2^24逾時

可以枚舉前一半n/2所以的子集 存在map裡

然後枚舉後一半看是否有和它相同的 相同的異或就為0

枚舉一半時間可以接受

#include <cstdio>#include <cstring>#include <map>using namespace std;const int maxn = 30;map <int, int> table;int n, a[maxn];int bitcount(int x){return x == 0 ? 0 : bitcount(x/2) + (x&1);}int main(){char s[1000];while(scanf("%d", &n) == 1 && n){for(int i = 0; i < n; i++){a[i] = 0;scanf("%s", s);for(int j = 0; s[j]; j++)a[i] ^= (1<<(s[j]-'A'));}int n1 = n/2;int n2 = n-n1;table.clear();for(int i = 0; i < (1<<n1); i++)//前半 枚舉了所有n1個元素的子集 {int x = 0;for(int j = 0; j < n1; j++){if(i & (1<<j))x ^= a[j];}if(!table.count(x) || bitcount(table[x]) < bitcount(i))//bitcount求i的2進位中1的個數 table[x] = i;}int ans = 0;for(int i = 0; i < (1<<n2); i++){int x = 0;for(int j = 0; j < n2; j++){if(i & (1<<j))x ^= a[n1+j];}if(table.count(x) && bitcount(ans) < bitcount(table[x]) + bitcount(i))ans = (i<<n1)^table[x];}printf("%d\n", bitcount(ans));for(int i = 0; i < n; i++){if(ans & (1<<i))printf("%d ", i+1);}printf("\n");}return 0;}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.