zoukankan      html  css  js  c++  java
  • 2017杭电多校第六场1008 Kirinriki

    传送门

    Kirinriki

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1012    Accepted Submission(s): 400


    Problem Description
    We define the distance of two strings A and B with same length n is
    disA,B=i=0n1|AiBn1i|
    The difference between the two characters is defined as the difference in ASCII.
    You should find the maximum length of two non-overlapping substrings in given string S, and the distance between them are less then or equal to m.
     

    Input
    The first line of the input gives the number of test cases T; T test cases follow.
    Each case begins with one line with one integers m : the limit distance of substring.
    Then a string S follow.

    Limits
    T100
    0m5000
    Each character in the string is lowercase letter, 2|S|5000
    |S|20000
     

    Output
    For each test case output one interge denotes the answer : the maximum length of the substring.
     

    Sample Input
    1 5 abcdefedcb
     

    Sample Output
    5
    Hint
    [0, 4] abcde [5, 9] fedcb The distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5
     

    Source
     

    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  6107 6106 6105 6104 6103 
     

    Statistic | Submit | Discuss | Note



    题意:
    已知字符串S,要求不重叠的最长子串长度,并满足两子串距离最大不超过m


    /*
    思路:由于字符串很短,所以可以枚举前缀和后缀
    在枚举的子串内采用尺取法将区间等分,利用sum不大于m的条件双指针同时遍历两个区间,更新最大值即可
    
    */
    #include<bits/stdc++.h>
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    
    
    const int maxn=5e3+10;
    int m,nl,ans;//nl表示字符串长度,ans代表最后返回的子串长度
    char s[maxn];
    
    void solve()
    {
        for(int i=2;i<=nl;i++)//i表示从总串中取出的子串长度
        {
            int o=i/2;
            int l=0,n=0,sum=0;
            for(int j=0;j<o;j++)
            {
                sum+=abs(s[j]-s[i-j-1]);
                if(sum<=m)n++,ans=max(ans,n);
                else
                {
                    sum-=abs(s[l]-s[i-l-1]);
                    sum-=abs(s[j]-s[i-j-1]);
                    l++;
                    j--;
                    n--;
                }
            }
        }
    }
    
    
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %s",&m,s);
            nl=strlen(s);
            ans=0;
            solve();
            reverse(s,s+nl);
            solve();
            printf("%d
    ",ans);
    
        }
        return 0;
    }

  • 相关阅读:
    c语言求最大公约数和最小公倍数(转)
    git 提交去除每次输账号密码
    phpstorm使用zen coding 快速编辑补全html/css代码
    YII2.0使用ActiveForm表单(转)
    php面向对象之trait
    php操作redis(转)
    模块
    列表生成式 与生成器表达式
    三元表达式,递归,内置函数
    面向过程的编程思想
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387429.html
Copyright © 2011-2022 走看看