zoukankan      html  css  js  c++  java
  • hdu 6103 Kirinriki(尺取法)

    Kirinriki

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

    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
     
     

    题意:

    给出字符串s,寻找其两个长度相同且不重叠的子串,满足其每位的ascil差值之和不大于m,且长度最长。

    题解:

    枚举头尾,向里缩进,使用取尺法

     
    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<vector>
    #include<cmath>
    #include<cstring>
    #include<bits/stdc++.h>
    
    using namespace std;
    int t,m,l,r,ans,len;
    char ch[5005];
    
    void solve(int x,int y) //x,y表示起始时两个初始的头尾
    {
       int l=0,r=0,dis=0; //l表示头x向里缩进l位,r表示尾y向里缩进r位
       while(x+r<y-r) //两个子串不能重合
       {
           if (dis+abs(ch[x+r]-ch[y-r])<=m)
           {
               dis+=abs(ch[x+r]-ch[y-r]);
               r++;
               ans=max(ans,r-l);
           } else
           {
               dis-=abs(ch[x+l]-ch[y-l]);
               l++;
           }
       }
    }
    
    int main()
    {
        scanf("%d",&t);
        for(;t>0;t--)
        {
            scanf("%d",&m);
            scanf("%s",&ch);
            len=strlen(ch);
            ans=0;
            for(int i=0;i<len;i++)  //i表示两个子串从起始位置相差i位
            {
                solve(0,len-i-1);//头为0,尾为len-i-1,向里缩进
                solve(0+i,len-1);//头为0+i,尾为len-1,向里缩进
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    shell 10流程控制
    shell 9test命令
    shell 8字符串与文件内容处理
    shell 7输入输出
    shell 6基本运算符
    JS-JQ实现TAB选项卡
    JS-JQ实现页面滚动时元素智能定位(顶部-其他部位)
    js获取框架(IFrame)的内容
    codeforces 660C C. Hard Process(二分)
    codeforces 660B B. Seating On Bus(模拟)
  • 原文地址:https://www.cnblogs.com/stepping/p/7344880.html
Copyright © 2011-2022 走看看