P1098 [NOIP 2007 提高组] 字符串的展开

原题链接 https://www.luogu.com.cn/problem/P1098

题目描述

展开字符串中的字符区间,如 a-d。仅当两端同为数字或同为小写字母,且右端更大时展开;两端相邻时仅删除连字符。

输入 第一行是 p1、p2、p3。p1 为 1、2、3 时分别按小写、大写、星号填充(前两种保留数字),p2 为每个填充字符的重复次数,p3 为 1 时正序、为 2 时逆序。第二行是原字符串。

输出 一行展开后的字符串。

输入输出样例 1

输入

1 2 1
abcs-w1234-9s-4zz

输出

abcsttuuvvw1234556677889s-4zz

输入输出样例 2

输入

2 3 2
a-d-d

输出

aCCCBBBd-d

题解

p1,p2,p3=map(int, input().split())
s=input().strip()
res=[]
for i,ch in enumerate(s):
    if ch=="-" and 0<i<len(s)-1:
        a=s[i-1]
        b=s[i+1]
        same= (a.isdigit()and b.isdigit()) or (a.islower() and b.islower())
        if same  and ord(a)<ord(b):
            mid = [ chr(c) for c in range(ord(a)+1,ord(b))]
            if p3==2:
                mid.reverse()
            if p1==1:
                res.append("".join(c*p2 for c in mid))
            elif p1==2:
                res.append("".join(c.upper()*p2 for c in mid))
            else:
                res.append("*" * (p2 * len(mid)))
            continue

    res.append(ch)

print("".join(res))