【Codeforces332E】Binary Key

来源:互联网 发布:华为 电信软件产品线 编辑:程序博客网 时间:2024/05/29 15:17

题意:

  • 给出两个字符串和一个正整数,要求构造长度为串,满足以下要求:
    • 将其不断复制到长度大于等于
    • 对应跑这个串,如果当前位置为,则将当前对应的字符加入一个串的结尾。(开始为空串)
    • 最后得到的串相等。
  • 输出满足要求的字典序最小的串。

题解:

  • 按照模的值分类,尽量让字典序小,通过哈希判断是否可行即可。

代码:

#include <bits/stdc++.h>#define gc getchar()#define ll long long#define A 2333#define N 1000009#define M 2009using namespace std;char s1[N],s2[M],ans[M],now[M];int len1,len2,k;unsigned long long ha[N],Ha[M];int read(){    int x=1;    char ch;    while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;    int s=ch-'0';    while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0';    return s*x;}void check(int t){    for (int i=len2-1;~i;i--)        Ha[i]=(i+t<len2?Ha[i+t]:0)*A+s2[i];    int rest=t-1;    for (int j=k-1;~j;j--)        if (rest>=0&&ha[j]==Ha[rest])        {            now[j]='1';            rest--;        }        else now[j]='0';    if (rest>=0) now[0]=0;}int main(){    //ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);    gets(s1);    len1=strlen(s1);    gets(s2);    len2=strlen(s2);    k=read();    for (int i=len1-1;~i;i--)        ha[i]=(i+k<len1?ha[i+k]:0)*A+s1[i];    for (int i=1;i<=200;i++)    {        if (i*(len1/k)+max(0,i-(k-len1%k))>len2) continue;        if (i*(len1/k)+min(len1%k,i)<len2) continue;        check(i);        if (now[0]&&(!ans[0]||strcmp(now,ans)==-1)) strcpy(ans,now);    }    ans[k]='\0';    if (!ans[0]) cout<<0<<endl;    else cout<<ans<<endl;    return 0;}
原创粉丝点击