首页 > 试题广场 >

一封奇怪的信

[编程题]一封奇怪的信
  • 热度指数:8769 时间限制: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

备注:
输入及输出均为字符串
 #include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    int A[26];
    for(int i=0;i<26;i++)
        cin>>A[i];
    cin>>s;

    int count=1,length=0,l;
    for(int i=0;i<s.length();i++)
    {
        l=A[s[i]-'a'];
        if(length+l>100)
        {
            length=l;
            count++;
        }
        else
        {
            length+=l;
        }    
    }
    cout<<count<<" "<<length<<endl;

    return 0;
}

发表于 2018-08-02 19:12:48 回复(2)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] line1 = bf.readLine().split(" ");
        String s = bf.readLine();
        int[] nums = new int[26];
        for (int i = 0; i < line1.length; i++) {
            nums[i] = Integer.parseInt(line1[i]);
        }
        int row_count = 1;//行数
        int cur_width = 0;//每一行的宽度
        for (int i = 0; i < s.length(); i++) {
            int len = nums[s.charAt(i) - 'a'];
            //当前字母写不下了,需要另起一行,重新记录当前行占的宽度
            if (cur_width + len > 100) {
                row_count++;
                cur_width = len;
            } else {
                cur_width += len;
            }
        }
        System.out.println(row_count+" "+cur_width);
    }
}
发表于 2019-08-06 16:35:35 回复(2)

#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    map<int,int> map;
    string s;
    for(int i = 'a'; i <= 'z'; i++)
        cin >> map[i];
    cin >> s;
    int sum = 0;
    int row = 0;
    if(!s.empty()) ++row;
    for(int i = 0; i < s.size(); i++)
    {
        if(sum + map[s[i]] <= 100)
            sum += map[s[i]];
        else
        {
            row++;
            sum = map[s[i]];
        }
    }
    cout << row << " " << sum << endl;
}

发表于 2021-04-04 16:55:35 回复(0)
直接模拟写信过程,当书写到第i个字符时,分为以下三种情况:
1.本行还未到100个字符,继续向本行添加字符,增加本行的字符宽度。
2.本行刚好到达100个字符,如果此时是最后一个字符,则无需另起一行,最后一行的字符宽度就是100;否则需要另起一行,行数自增1,最后一行的字符宽度初始化为0,继续追加字符。
3.在本行写入当前字符会使得宽度超出100,需要另起一行,最后一行的字符宽度初始化为当前字符的宽度。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] widthArr = br.readLine().trim().split(" ");
        int[] width = new int[26];
        for(int i = 0; i < 26; i++) width[i] = Integer.parseInt(widthArr[i]);
        char[] content = br.readLine().trim().toCharArray();
        int rowLen = 0;
        int lines = 1;
        for(int i = 0; i < content.length; i++){
            if(rowLen + width[content[i] - 'a'] < 100){
                // 本行未满,继续在本行添加字符
                rowLen += width[content[i] - 'a'];
            }else if(rowLen + width[content[i] - 'a'] == 100){
                // 如果写完最后一个字符使得最后一行填满,则最后一行的宽度就是rowLen,否则另起一个新行
                if(i != content.length - 1){
                    lines ++;
                    rowLen = 0;
                }
            }else{
                lines ++;
                rowLen = width[content[i] - 'a'];
            }
        }
        System.out.println(lines + " " + rowLen);
    }
}


编辑于 2021-02-15 22:32:25 回复(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)
/*
首先用一个长度为26的整型数组保存每个单词的长度;
当需要知道某个单词的长度时,只需要用该字符减去97,然后将差值作为下标即可
然后以100进行统计,当加上需要添加的字符使得总和大于100,则不添加,否则继续
*/

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int[] arr = new int[26];//26个字符对应的宽度
        for(int i = 0;i<26;i++)
            arr[i] = input.nextInt();
        String str = input.next();
        char[] ch = str.toCharArray();//字符数组
        
        int count = 1;//行数
        int sum = 0;//字符的总和
        int lastsum = 0;//最后一行的和
        for(int i = 0;i<ch.length;){
            int chlength = arr[ch[i]-97];
            if(sum + chlength <= 100){
                sum+=chlength;
                lastsum = sum;
                i++;//把这个放在这里的目的是,为了确保在刚好要超过100的那个字符能参与到下一行的计算
            }else{
                count++;
                sum = 0;//总和置0,重新统计
            }
        }
        
        System.out.print(count + " " + lastsum);
    }
}

发表于 2020-04-25 13:42:52 回复(0)
a,p,q = list(map(int,input().split())),1,0
for i in input():
    if a[ord(i) - 97] + q > 100:
        p,q = p + 1,a[ord(i) - 97]
    else:
        q += a[ord(i) - 97]
print(p,q)

发表于 2020-03-15 11:25:52 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int a[26];
    for(int i=0;i<26;i++)
        cin>>a[i];
    string s;
    cin>>s;
    int l = s.length();
    int n=1,m=0;
    for(int i=0;i<l;i++){
        int j = a[s[i]-'a'];
        if(m+j>100){
            n++;
            m = j;
        }else
            m += j;
    }
    cout<<n<<" "<<m<<endl;
}

发表于 2019-12-08 10:47:04 回复(0)
#include<bits/stdc++.h>

using namespace std;

int main(){
    int val[26];
    for(int i = 0;i < 26;++ i)
        cin >> val[i];
    int row = 1;
    int num = 0;

    string str;
    cin >> str;
    for(int i = 0;i < str.size();++ i){
        if(val[str[i] - 'a'] + num > 100){
            num = val[str[i] - 'a'];
            ++row;
        }else{
            num += val[str[i] - 'a'];
        }
    }
    cout<<row<<" "<<num<<endl;
}

发表于 2019-09-07 15:35:06 回复(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)
#include<stdio.h>
#include<string.h>
inta[27],c[105]={0},d[1005];
charb[1005];
intmain(){
    inti,j,k,m=1;
    chars[]="1abcdefghijklmnopqrstuvwxyz";
    for(i=1;i<=26;i++){
        scanf("%d",&a[i]);
    }
    scanf("%s",b);
    intstr=strlen(b);
    for(i=0;i<str;i++){
        for(j=0;j<27;j++){
            if(b[i]==s[j]){
                b[i]=a[s[j]-'a'+1];
            }
        }
    }
    for(i=0;i<str;i++){
        if(c[m]+b[i]<=100){
            c[m]+=b[i];
        }else{
            m++;
            c[m]+=b[i];
        }
    }
    if(c[m]!=0){
        printf("%d %d\n",m,c[m]);
    }else{
        printf("%d %d\n",m-1,c[m-1]);
    }
    return0;
}
发表于 2019-03-10 18:23:58 回复(0)
py3.5:
a =list(map(int,input().split(' ')))
b =list(input())
n =1
sum=0
fori inrange(len(b)):
    sum=sum+a[ord(b[i]) -ord('a')]
    if(sum> 100):
        n =n+1
        sum=a[ord(b[i]) -ord('a')]
         
print(n,sum)
发表于 2018-08-04 10:47:38 回复(0)
package main

import (
    "fmt"
)

func main() {
    var cnt [26]int
    for i:=0;i<26;i++{
        fmt.Scan(&cnt[i])
    }
    var s string
    fmt.Scan(&s)
    var l,sum int
    for _,ch:=range []byte(s){
        if sum+cnt[ch-'a']>100{
            l++
            sum=cnt[ch-'a']
        }else if sum+cnt[ch-'a']==100{
            l++
            sum=0
        }else{
            sum+=cnt[ch-'a']
        }
    }
    if sum==0{
        fmt.Printf("%d %d",l,100)
    }else{
        fmt.Printf("%d %d",l+1,sum)
    }
}

发表于 2023-03-12 02:52:59 回复(0)
大佬们,我这代码有问题不?自己测试感觉都对,提交之后只通过2个样例。
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main() {
    map<char, int> m;
    string s; int len = 0;
    for (char c = 'a'; 'z' - c  >= 0; c++) {
        cin >> m[c];
    }
    cin >> s;
    for (int i = 0, j = s.size(); i < j; i++) {
        len += m[s[i]];
    }
    cout << (len - 1) / 100 + 1 << ' ' << len % 100 << endl;
}
发表于 2021-08-26 16:36:59 回复(0)
l = list(map(int, input().strip().split()))
s = input()

letters = "abcdefghijklmnopqrstuvwxyz"
c2l = {c: li for c, li in zip(list(letters), l)}
line_length = 0
line_num = 1
for c in s:
    if line_length + c2l[c] <= 100:
        line_length += c2l[c]
    else:
        line_length = c2l[c]
        line_num += 1

print(f"{line_num} {line_length}")

发表于 2021-03-23 14:17:32 回复(0)
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)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000+5;
char str[maxn];
int main() {
    int num[30];
    for(int i=0; i<26; i++) {
        cin>> num[i];
    }
    cin >> str;
    int n = 0, len = 0;
    for(int i=0; i<strlen(str); i++) {
        int tot = num[str[i] - 'a'];
        if(len + tot > 100) {
            n++;
            len = tot;
        }
        else {
            len += tot;
        }
    }
    cout << n + 1 << " " << len << endl;
    return 0;
}

发表于 2020-10-11 20:23:28 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            int[] arr = new int[26];
            for(int i=0; i<26; i++){
                arr[i] = in.nextInt();
            }
            while(in.hasNext()){
                String str = in.next();
                if(str.length() < 1 || str.length() > 1000){
                    return;
                }
                writeNotes(str,arr);
            }
    }
    public static void writeNotes(String str,int[] nums){
        int res = 0;
        if(str.length() < 1 || str.length() > 1000){
            return;
        }
        int count = 1;
        for(int j=0; j<str.length(); j++){
            int index = str.charAt(j) - 'a';
            if((res +  nums[index]) > 100){
                res = 0;
                ++count;
            }
            res +=  nums[index];
        }
        System.out.println(count + " " + res);
    }
}

发表于 2020-09-12 20:21:02 回复(0)
arr = list(map(int, input().split()))
S = input()
MAX = 100
res = 1
cur = 0
for ch in S:
    l = arr[ord(ch) - ord('a')]
    if cur + l > MAX:
        cur = l
        res += 1
    else:
        cur += l
print(res, cur)

发表于 2020-08-07 11:28:44 回复(0)
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String size = input.nextLine();
        String str = input.nextLine();
        message(size,str);
    }
    public static void message(String size,String str){
        int count = 1;
        int last = 0;
        String[] letterSize = size.split(" ");
        char[] ch = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            if(last + Integer.parseInt(letterSize[ch[i] - 'a'] ) > 100) {count++;last = Integer.parseInt(letterSize[ch[i] - 'a'] );}
            else last += Integer.parseInt(letterSize[ch[i] - 'a']);
        }
        System.out.println(count + " " + last);
    }
}


 

发表于 2020-07-17 16:54:42 回复(0)