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;
    }
  • 相关阅读:
    MongoDB 2.4、备份
    MongoDB 2.3复制(副本集)
    MongoDB 2.2安全
    isMobile 一个简单的JS库,用来检测移动设备
    修改ECSHOP,支持图片云存储化(分离到专用图片服务器)
    压缩代码加速ecshop程序页面加载速度
    ecshop改造读写分离
    ecshop在nginx下实现负载均衡
    运用@media实现网页自适应中的几个关键分辨率
    在ECSHOP首页今日特价(促销商品)增加倒计时效果
  • 原文地址:https://www.cnblogs.com/stepping/p/7344880.html
Copyright © 2011-2022 走看看