hdu--6103--Kirinriki

Kirinriki

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


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


题意 :

给出一串字符串,从中选出两个不重叠的子串,使得两个子串满足距离和 <= m 的最长字符串长度,A,B 串中的字符距离计算为 disA,B=i=0n1|AiBn1i||Ai−Bn−1−i|为两个字符之间的asc码的差的绝对值。

解题思路:

这道题是用暴力枚举解的,关键是怎么用暴力,才能不超时,这就需要找到一种可以快速的且不重复的枚举所有情况的暴力方法。观察字符串可以看出,任意的两个不重复的子串在原字符串中,都可以找到唯一的一个对称轴,当我们把所有的对称轴都枚举一遍,就能实现把所有的情况枚举一遍,枚举对称轴比较容易实现,且不容易重复,对于每个对称轴,我们再用尺取法去寻找关于该中心轴对称的两个dis <= m的最长子串

代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using  namespace std;
char s[ 5005];
int main()
{
     int T;
    scanf( "%d", &T);
     while(T--)
    {
         int m;
        scanf( "%d %s", &m, s +  1);
         int len = strlen(s +  1);
         int l1, r1, l2, r2, tmp;
         int ans =  0;
         for( int i =  1; i <= len; i++)
        {
             ///以第i - 1个字符和第i个字符之间的空为对称轴
            l1 = r1 = i -  1;
            l2 = r2 = i;
            tmp =  0;
             while(l1 >=  1 && r2 <= len)
            {
                tmp += abs(s[l1] - s[r2]);
                 while(tmp > m && l1 <= r1 && l2 <= r2)
                    tmp -= abs(s[r1--] - s[l2++]);
                 if(tmp <= m)
                    ans = max(ans, r1 - l1 +  1);
                l1--;
                r2++;
            }
             ///以第i个字符为对称轴
            l1 = r1 = i -  1;
            l2 = r2 = i +  1;
            tmp =  0;
             while(l1 >=  1 && r2 <= len)
            {
                tmp += abs(s[l1] - s[r2]);
                 while(tmp > m && l1 <= r1 && l2 <= r2)
                    tmp -= abs(s[r1--] - s[l2++]);
                 if(tmp <= m)
                    ans = max(ans, r1 - l1 +  1);
                l1--;
                r2++;
            }
        }
        printf( "%d\n", ans);
    }
     return  0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值