洛谷 P1098 [NOIP2007 提高组] 字符串的展开 Python题解

洛谷 P1098 [NOIP2007 提高组] 字符串的展开 Python题解,第1张

p1, p2, p3 = map(int, input().split())
strr = input()
"""
    p1 - 1 填充小写 -2 填充大写 -3 填充p2对应数字的星号
    p2 - 填充的字符数
    p3 - 1 原来顺序 -2 填充的逆序输出
    如果中间没有间隔则直接删除减号 
    如果右边小于左边则不变
    当‘-’两端的字符分别是数字和字母时,原样输出。
    字符串首尾的‘-’需要原样输出。
    连续的‘-’需要原样输出。
"""
res = ''
for i in range(len(strr)):
    if strr[i] != "-":
        res += strr[i]
    else:
        if ord(strr[i - 1]) >= ord(strr[i + 1]) or i == 0 or i == len(strr) - 1 or strr[i + 1] == "-" \
                or strr[i - 1] == "-" or ("0" <= strr[i - 1] <= "9" and "a" <= strr[i + 1] <= "z"):
            # 首位是"-"、左边是数字右边是字母、连续多个"-"、左边的大于右边的(假如右边是数字左边是字母也符合这条件)、 原样输出即可
            res += strr[i]
            continue
        if ord(strr[i + 1]) - ord(strr[i - 1]) == 1:
            continue
        cou = ord(strr[i + 1]) - ord(strr[i - 1])
        if "a" <= strr[i - 1] <= "z":  # 如果要是字母
            if p1 == 1 and p3 == 1:
                for j in range(1, cou):
                    res += p2 * chr(ord(strr[i - 1]) + j)
            if p1 == 1 and p3 == 2:
                for j in range(cou - 1, 0, -1):
                    res += p2 * chr(ord(strr[i - 1]) + j)
            if p1 == 2 and p3 == 1:
                for j in range(1, cou):
                    res += p2 * chr(ord(strr[i - 1]) - 32 + j)
            if p1 == 2 and p3 == 2:
                for j in range(cou - 1, 0, -1):
                    res += p2 * chr(ord(strr[i - 1]) - 32 + j)
        if "0" <= strr[i - 1] <= "9":  # 如果要是数字 数字是不区分大小写的
            if p3 == 1 and p1 != 3:
                for j in range(1, cou):
                    res += p2 * chr(ord(strr[i - 1]) + j)
            if p3 == 2 and p1 != 3:
                for j in range(cou - 1, 0, -1):
                    res += p2 * chr(ord(strr[i - 1]) + j)
        if p1 == 3:  # "*"随便输出
            for j in range(1, cou):
                res += p2 * '*'
print(res)

其实对于洛谷题单算法1-1模拟与高精度来说所有题目都不算难,主要是看你能不能理解什么叫模拟,什么叫高精度,理解了之后只不过就是代码翻译的过程

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/942007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存