zoukankan      html  css  js  c++  java
  • UVa 1326

    训练指南p.59

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    
    using namespace std;
    
    const int MAXN = 30;
    
    int N;
    int A[MAXN];
    char str[1010];
    map<int , int> table;
    
    int bitcount( int x )
    {
        int res = 0;
        while ( x )
        {
            if ( x & 1 ) ++res;
            x >>= 1;
        }
        return res;
    }
    
    int main()
    {
        while ( scanf( "%d", &N ) == 1 )
        {
            for ( int i = 0; i < N; ++i )
            {
                scanf( "%s", str );
                A[i] = 0;
                for ( int j = 0; str[j]; ++j )
                    A[i] |= ( 1 << ( str[j] - 'A' ) );
            }
    
            table.clear();
    
            int n1 = N / 2, n2 = N - n1;
            for ( int i = 0; i < ( 1 << n1 ); ++i )
            {
                int tmp = 0;
                for ( int j = 0; j < n1; ++j )
                if ( i & ( 1 << j ) )
                    tmp ^= A[j];
                if ( !table.count(tmp) || bitcount(i) > bitcount( table[tmp] ) ) table[tmp] = i;
            }
    
            int ans = 0;
            for ( int i = 0; i < ( 1 << n2 ); ++i )
            {
                int tmp = 0;
                for ( int j = 0; j < n2; ++j )
                if ( i & ( 1 << j ) ) tmp ^= A[ n1 + j ];
                if ( table.count(tmp) && bitcount(ans) < bitcount(i) + bitcount(table[tmp]) )
                    ans = ( i << n1 ) | table[tmp];
            }
    
            printf("%d
    ", bitcount(ans) );
            bool first = false;
            for ( int i = 0; i < N; ++i )
            if ( ans & ( 1 << i ) )
            {
                if ( first ) putchar(' ');
                printf("%d", i + 1);
                first = true;
            }
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    全文本的检索
    网卡配置
    linux解压命令
    Session
    swoole安装
    Linux 系统磁盘满处理方法
    php写入和读取文件内容
    PHP读取文件夹的文件列表
    php 公历农历互相转换
    PHP实现RESTful风格的API实例
  • 原文地址:https://www.cnblogs.com/GBRgbr/p/3310126.html
Copyright © 2011-2022 走看看