简体   繁体   English

词典价值

[英]Lexicographical value

i have a string.我有一个字符串。 I need to replace the character '?'我需要替换字符“?” and find the missing character to make it lexicographical string.并找到丢失的字符以使其成为字典字符串。 for Example- if I have string "ab".例如 - 如果我有字符串“ab”。 Its lexicographical value is "aa".Since the first character 'a' is different than second character 'b'.它的字典值是“aa”。因为第一个字符“a”与第二个字符“b”不同。 It gives output -1.它给出 output -1。 If I give string "ta?a".如果我给字符串“ta?a”。 In this string If I replace '?'在这个字符串中如果我替换 '?' with t.与 t。 It become lexicographical.它变成了字典式。 It gives output "tata".它给 output “tata”。 I am giving input s and k.我正在输入 s 和 k。 K is half of length of s. K 是 s 长度的一半。 Please help me in this code.请在这段代码中帮助我。

s=input()
k=input()
for i in range(k):
    if (s[i]>=97) and (s[i]<=123):
        if (s[i]==s[i+k]):
            continue
        else:
             s=-1
    
    else:   
        if(s[i]>=s[i+k]):
            s[i+k]=s[i]
        else:
            s[i]=s[i+k]

return s

I'm not entirely sure I understand your question, but here's my attempt:我不完全确定我理解你的问题,但这是我的尝试:

s = list(input())
k = int(input())
for i in range(k):
    s[i] = s[i+k] if s[i] == '?' else s[i]
    s[i+k] = s[i] if s[i+k] == '?' else s[i+k]

print(''.join(s) if s[:k] == s[k:] else -1)

For inputs ta?a and 2 it will output tata , for ab and 1 it will output -1 .对于输入ta?a2它将 output tata ,对于ab1它将 output -1 Note that the second argument is redundant anyway since you said it's just half the length.请注意,第二个参数无论如何都是多余的,因为您说它只是长度的一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM