zoukankan      html  css  js  c++  java
  • HDU 6103 17多校6 Kirinriki(双指针维护)

    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
     
     
    启发博客:http://www.cnblogs.com/coded-ream/p/7343946.html
    以下题解和题意摘自此博客

    题目描述:

    找两个不重叠的字符串A,B。 使得dis(A,B)<=m;dis(A,B)=∑i=0n−1|Ai−Bn−1−i|。求最长的字符串长度。

    思路:

    官方题解,双指针维护。简单题。枚举对称中心。

     双指针维护!!以后要搜超时就记这个!!
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<queue>
     5 #include<map>
     6 #include<vector>
     7 #include<cmath>
     8 #include<cstring>
     9 using namespace std;
    10 int m,ans,len;
    11 char str[5005];
    12 
    13 void solve(int x,int y)//x,y表示左右端点
    14 {
    15     int dis=0;
    16     int l=0,r=0;//双指针,r记录的是两段各自的长度,l记录的是头尾各自缩进多少
    17     while(x+r<y-r)
    18     {
    19         if(dis+abs(str[x+r]-str[y-r])<=m)
    20         {
    21             dis+=abs(str[x+r]-str[y-r]);
    22             r++;
    23             ans=max(ans,r-l);
    24         }
    25         else
    26         {
    27             dis-=abs(str[x+l]-str[y-l]);
    28             l++;
    29         }
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     int T;
    36     scanf("%d",&T);
    37     while(T--)
    38     {
    39         scanf("%d",&m);
    40         cin>>str;
    41         ans=0;
    42         len=strlen(str);
    43         for(int i=1;i<len;i++)
    44             solve(0,i);//相当于枚举对称轴在前半段
    45         for(int i=0;i<len-1;i++)
    46             solve(i,len-1);//相当于枚举对称轴在后半段
    47         printf("%d
    ",ans);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    backup1
    backup
    The operation couldn’t be completed. (LaunchServicesError error 0.)
    drawRect & layoutSubviews 调用时间
    在撤销“本地修改”之后再恢复
    APP调用系统相册,使用3DTouch重压,崩溃
    IOS_UIButton去掉系统的按下高亮置灰效果
    navigationItem的leftBarButtonItem和rightBarButtonItem隐藏
    _BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15) 问题的解决
    删除button中除label之外的View
  • 原文地址:https://www.cnblogs.com/Annetree/p/7344732.html
Copyright © 2011-2022 走看看