zoukankan      html  css  js  c++  java
  • hdu 6103 Kirinriki (枚举对称中心+双指针)

    Problem Description
    We define the distance of two strings A and B with same length n is
    disA,B=∑(i=0  n1)  |A[i]B[n1i]|
    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
     
    给你一个m值,一个字符串,让你在字符串中找两个不重叠的长度相等的字串,两个字符串首对应尾逐项向中心的acsii码差的绝对值之和不能大于m
    问这两个相等的字符串最长有多长
     
    我们枚举这两个字串的对称中心,对于每个对称中心我们利用双指针进行处理一下就好了
    代码如下:
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 const int maxn = 5000+50;
     5 char s[maxn];
     6 int m,len;
     7 int ans;
     8 void work (int x,int y)
     9 {
    10     int dis=0,l=0,r=0;//左边的串s[x-l]~s[x-r] 右边的串s[y+l]~s[y+r]
    11     while (y+r<len&&x-r>=0){
    12         if (dis+abs(s[x-r]-s[y+r])<=m){
    13             dis+=abs(s[x-r]-s[y+r]);
    14             r++;
    15             ans = max(ans,r-l);
    16         }
    17         else{
    18                 dis-=abs(s[x-l]-s[y+l]);
    19                 l++;
    20         }
    21     }
    22 }
    23 int main()
    24 {
    25     //freopen("de.txt","r",stdin);
    26     int T;
    27     scanf("%d",&T);
    28     while (T--){
    29         scanf("%d",&m);
    30         scanf("%s",s);
    31         len = strlen(s);
    32         ans = 0;
    33         for (int i=0;i<len;++i){
    34             work(i-1,i+1);
    35             work(i,i+1);
    36         }
    37         printf("%d
    ",ans);
    38     }
    39     return 0;
    40 }
     
  • 相关阅读:
    Java:API文档;文档注释中的javadoc标记;官方API;自己动手给项目建一个API文档
    Java:配置环境(Mac)——MySQL
    Java:配置环境(Mac)——Tomcat
    Java:配置环境(Mac)——Eclipse;修改JDK版本后,Eclipse打不开
    Java:配置环境(Mac)——JDK
    Git:九、删除项目
    Git:修改Git Bash默认打开位置(win10)
    操作系统:diskpart常用指令(使用diskpart实现分区管理)
    人生第一次离职
    C++ std::thread概念介绍
  • 原文地址:https://www.cnblogs.com/agenthtb/p/7344799.html
Copyright © 2011-2022 走看看