• 牛客练习赛31 D 最小相似度


    最小相似度

    链接

    分析:

      转化为求1的个数,这样两个串不同的位置的个数就是1的个数。那么对于一个二进制串x,它的贡献就是max{x与s[i]异或后0的个数}=>max{m-x与s[i]异或后1的个数}=>m-min{x与s[i]异或后1的个数}。

      即我们确定的x应该满足,与所有的串异或后1的个数,最大的1的个数,尽量小。

      一共有$2^m$个串,所以从将给定的n个串出发,到其他的所有状态跑一遍最短路。

      设距离最远的距离为y,那么答案就是m-y。

    代码:

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<cctype>
    #include<set>
    #include<queue>
    #include<vector>
    #include<map>
    using namespace std;`  `  `  `  `  `  
    typedef long long LL;
    
    inline int read() {
        int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
        for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
    }
    
    const int N = (1 << 20) + 10;
    int f[N], q[N];
    char s[30];
    
    int main() {
        int n = read(), m = read(), L = 1, R = 0, ans = 0;
        memset(f, -1, sizeof(f));
        for (int i = 1; i <= n; ++i) {
            scanf("%s", s);
            int now = 0;
            for (int j = m - 1; ~j; --j) 
                now = now * 2 + (s[j] - '0');
            if (f[now] == -1) q[++ R] = now, f[now] = 0;
        }
        while (L <= R) {
            int u = q[L ++];
            ans = max(ans, f[u]);
            for (int i = 0; i < m; ++i) {
                int v = u ^ (1 << i);
                if (f[v] == -1) f[v] = f[u] + 1, q[++ R] = v;
            }
        }
        cout << m - ans;
        return 0;
    }
  • 相关阅读:
    Dependent Parameters in Concurrent Program using Special Value Set
    USER Management | Role Categories | Roles | Indirect Responsibilities
    asp.net core 1.0初识
    ASP.NET Core管道深度剖析
    linux图机界面机制
    类对象管理方式
    UNICODE串转换成char类型串的四种方法
    进程句柄表与创建句柄表
    虚拟化技术
    Windows基于Apache的svn服务器配置
  • 原文地址:https://www.cnblogs.com/mjtcn/p/10502687.html
Copyright © 2020-2023  润新知