字符串3.和最大的连续降序字符

Description
Archana is very fond of strings. She likes to solve many questions related to strings. She comes across a problem which she is unable to solve. Help her to solve. The problem is as follows: Given is a string of length L. Her task is to find the longest string from the given string with characters arranged in descending order of their ASCII code and in arithmetic progression. She wants the common difference should be as low as possible(at least 1) and the characters of the string to be of higher ASCII value.

Input
The first line of input contains an integer T denoting the number of test cases. Each test contains a string s of lengthL.

1<= T <= 100

3<= L <=1000

A<=s[i]<=Z

The string contains minimum three different characters.

Output
For each test case print the longest string.Case 1:Two strings of maximum length are possible- “CBA” and “RPQ”. But he wants the string to be of higher ASCII value therefore, the output is “RPQ”.Case 2:The String of maximum length is “JGDA”.

Sample Input 1
2
ABCPQR
ADGJPRT

Sample Output 1
RQP
JGDA

注:这个题的测试用例是有问题的,本答案按照等差降序、字符串最长、common difference尽可能小、ascll值尽可能大的优先级顺序写可以通过,有的答案将ascll值尽可能大优先级放在common difference尽可能小前面,也可以通过.

def solution(s):
    res_step = 0
    res_s = ""
    res_len = 0
    # 初始化
    init = [0] * 26
    for i in s:
        init[ord(i) - ord('A')] = 1
    for j in range(26):
        if init[j] == 1:
            temp_step = 1
            while temp_step < 26:
                temp_s = ""
                temp_len = 0
                temp = j
                while temp < 26:
                    if init[temp] == 1:
                        temp_len += 1
                        temp_s = chr(temp + ord('A')) + temp_s
                        temp += temp_step
                    else:
                        break
                if temp_len > res_len:
                    res_len = temp_len
                    res_s = temp_s
                    res_step = temp_step
                elif temp_len == res_len:
                    if temp_step < res_step:
                        res_s = temp_s
                        res_step = temp_step
                    elif temp_step == res_step and ord(temp_s[-1]) > ord(res_s[-1]):
                        res_s = temp_s
                temp_step += 1
    return res_s


if __name__ == '__main__':
    n = int(input())
    for _ in range(n):
        s = input().strip()
        result = solution(s)
        print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值