swel


私信TA

用户名:dotcpp0682165

访问量:31

签 名:

等  级
排  名 39978
经  验 335
参赛次数 0
文章发表 2
年  龄 0
在职情况 学生
学  校 九院
专  业

  自我简介:

TA的其他文章

解题思路:根据题目要求一步一步来。

注意事项:python中index(value)只能得到第一个重复元素的位置,需要翻转一下。

参考代码:

s = list(input().strip())
command = input().strip().split()

for i in range(0, len(command), 2):
   if command[i] == 'D':
       char_to_delete = command[i+1]
       if char_to_delete in s:
           s.remove(char_to_delete)
       else:
           print('被改的字符不存在')
   elif command[i] == 'I':
       char_to_insert_before = command[i+1]
       char_to_insert = command[i+2]
       if char_to_insert_before in s:
           index = len(s) - 1 - s[::-1].index(char_to_insert_before)
           s.insert(index, char_to_insert)
       else:
           print('被改的字符不存在')
   elif command[i] == 'R':
       char_to_replace = command[i+1]
       new_char = command[i+2]
       s = [new_char if char == char_to_replace else char for char in s]

print(''.join(s))

 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区