zoukankan      html  css  js  c++  java
  • cf443B Kolya and Tandem Repeat

    B. Kolya and Tandem Repeat
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.

    Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?

    See notes for definition of a tandem repeat.

    Input

    The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.

    Output

    Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.

    Sample test(s)
    input
    aaba
    2
    
    output
    6
    
    input
    aaabbbb
    2
    
    output
    6
    
    input
    abracadabra
    10
    
    output
    20
    
    Note

    A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.

    In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.

    其实就是个很水的模拟……只要看懂题意应该就没什么问题

    不过要注意k>s.length的情况

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,len,k,ans;
    int main()
    {
        string s;
        char ch[1001];
        cin>>s;
        scanf("%d",&k);
        len=s.length();
        for (int i=len;i>=1;i--)
          ch[i]=s[i-1];
        for (int s=1;s<=len;s++)
        for (int i=1;i<=len-s+1;i++)
          {
            if (s-1+2*i>len+k) break;
            bool mark=0;
            for (int j=s+i;j<=min(len,s+i*2-1);j++)
              if (ch[j-i]!=ch[j]) {mark=1;break; }
            if (!mark) ans=max(i,ans);
          }
        if (k>len) ans=max(ans,(k+len)>>1);
        printf("%d",ans*2);
    }
    

      

    ——by zhber,转载请注明来源
  • 相关阅读:
    nginx 服务企业应用
    3D模型展示以及体积、表面积计算
    php实现MySQL读写分离
    three.js实现3D模型展示
    thinkphp5.1+think-queue
    GIT记住远端仓库地址密码
    php实现采集(仅做参考)
    phpStudy集成环境apche+openssl配置本地https
    HTTP与HTTPS区别
    在父页面用Iframe加载子页面时,将父页面的title替换成子页面title
  • 原文地址:https://www.cnblogs.com/zhber/p/4035961.html
Copyright © 2011-2022 走看看