[CodeForces332E]Binary Key

Problem

题目给出一个加密前的字符串长度为p和加密后的字符串长度为s,让你求一个长度为K字典序最小的密钥。
密钥是循环的,第i位为1表示加密前的第i为是有用的否则是没用的。

Solution

首先枚举秘钥中一共有x个1(1<=x<=min(s,k))
一个秘钥有x个1,也就是能确定加密串每个位置所对应秘钥的第几次循环。
并且贪心从后往前找每一个位置即可。

Notice

容易出错

Code

#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
string st1, st2, xx[2005], yy[2005], ans;
int sqz() 
{
    getline(cin, st1), getline(cin, st2);
    int len1 = st1.length(), len2 = st2.length();
    int k = read(), t = min(k, len2);
    rep(i, 0, len1 - 1) xx[i % k] += st1[i];
    ans = "2";
    rep(i, 1, t)
    {
        rep(j, 0, t - 1) yy[j] = "";
        rep(j, 0, len2 - 1) yy[j % i] += st2[j];
        int now = i; string zz;
        per(j, k - 1, 0)
            if (now && xx[j] == yy[now - 1])
                zz += "1", now--;
            else zz += "0";
        if (!now)
        {
            reverse(zz.begin(), zz.end());
            ans = min(ans, zz);
        }
    }
    if (ans == "2") ans = "0";
    cout << ans << endl;
}

转载于:https://www.cnblogs.com/WizardCowboy/p/7681311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值