首页 > 试题广场 >

一封奇怪的信

[编程题]一封奇怪的信
  • 热度指数:8775 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现在你需要用一台奇怪的打字机书写一封书信。信的每行只能容纳宽度为100的字符,也就是说如果写下某个字符会导致行宽超过100,那么就要另起一行书写
信的内容由a-z的26个小写字母构成,而每个字母的宽度均会事先约定。例如字符宽度约定为[1,2,3,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],那么就代表'a'到'd'四个字母的宽度分别是1,2,3,4,而'e'到'z'的宽度均为5
那么按照上述规则将给定内容S书写成一封信后,这封信共有几行?最后一行宽度是多少?

输入描述:
输入为两行:
第一行是存储了每个字符宽度的字符串,包含26个数字,以1个空格分隔,每个数字均小于等于10
第二行是存储了待输入字符的字符串S,字符串S的长度在1到1000之间


输出描述:
输出为信的行数以及最后一行字符宽度,中间以1个空格分隔
示例1

输入

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
helloworld

输出

1 50

说明

"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5"规定每个字符宽度为5
"helloworld"是输入的字符串S
由于S共包含10个字符,也即共占用50个字符宽度,因此可以写在同一行
示例2

输入

5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10 10 10 5 5 5 5 5 5 5 5
hahahahahahahaha

输出

2 20

说明

"5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10 10 10 5 5 5 5 5 5 5 5"规定了每个字符宽度
"hahahahahahahaha"是输入的字符串S
由于h宽度为10,a宽度为5,因此'hahahahahahah'占用100字符宽度可以写在第一行,‘aha’写在第二行也即最后一行。因此字符宽度为20

备注:
输入及输出均为字符串
width = list(map(int, input().split(' ')))
char = input()
line = 1
length = 0
for i in char:
    ind = ord(i) - ord('a')
    if length + width[ind] <= 100:
        length += width[ind]
    else:
        line += 1
        length = width[ind]
print(str(line)+" "+str(length))

发表于 2021-02-16 20:39:32 回复(0)
v=list(map(int,input().split()))
s=input()
l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
h=0
count=0
n=0
for i in range(len(s)):
    count=count+v[l.index(s[i])]
    if i+1<len(s):
        if count+v[l.index(s[i+1])]>100:
            count=0
            n=n+1
print('%s %s'%(n+1,count))   

发表于 2020-05-26 10:20:55 回复(0)
没什么好说的。python
nums = list(map(int,input().split()))
ss = input()
flag = []
ans,i,tmp = 1,0,0
while i<len(ss):    
    tmp+=nums[ord(ss[i])-ord('a')]   
    i+=1
    if tmp>100:
        tmp = 0
        ans +=1
        i = i-1
    
print(ans,tmp)

发表于 2020-04-25 00:39:45 回复(0)
python3.5
# python中字母与ascii码的相互转换
# ord(c):返回该字节的值
# chr(i):返回一个字符,
# chr(97)返回字符'a',该方法是ord()的反方法
l = list(map(int,input().split()))
s = list(map(ord,input()))
s = [int(x) - 97 for x in s] #int(ord('a')) = 97
r,count = 0,0
for i in s:
    if count + l[i] < 100:
        count += l[i]
    elif count + l[i] == 100:
        count = 0
        r += 1
    else:
        count = l[i]
        r += 1
if count == 0:
    count = 100
else:
    r += 1
print(r,count)


发表于 2019-08-19 21:47:40 回复(0)
""""
求和 及 条件判断
"""

if __name__ == "__main__":
    wide = list(map(int, input().strip().split()))
    s = input().strip()
    line = 0
    count = 0
    for c in s:
        if count == 0: line = 1
        count += wide[ord(c) - ord('a')]
        if count > 100:
            line += 1
            count = wide[ord(c) - ord('a')]
    print(line, count)

发表于 2019-07-13 10:40:42 回复(0)