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)