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,转载请注明来源
  • 相关阅读:
    Linux 共享库
    使用Visual Studio(VS)开发Qt程序代码提示功能的实现(转)
    ZOJ 3469 Food Delivery(区间DP)
    POJ 2955 Brackets (区间DP)
    HDU 3555 Bomb(数位DP)
    HDU 2089 不要62(数位DP)
    UESTC 1307 windy数(数位DP)
    HDU 4352 XHXJ's LIS(数位DP)
    POJ 3252 Round Numbers(数位DP)
    HDU 2476 String painter (区间DP)
  • 原文地址:https://www.cnblogs.com/zhber/p/4035961.html
Copyright © 2011-2022 走看看