[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