当前位置: 首页 > news >正文

UVa 235 Typesetting

题目描述

本题要求实现一个文本排版系统,支持可变宽度字体和不同字号。输入包含字体宽度表和多个段落,需要根据给定的行宽对每个段落进行排版,并输出排版结果。

问题分析

输入格式

  1. 字体宽度表

    • 第一行:字符数量 N
    • 接下来 N 行:每行包含一个字符和6个整数,表示该字符在6种字体下10点字号时的宽度
  2. 段落数据

    • 每段以 “L W” 开头,L 是文本行数,W 是允许的行宽
    • 后续 L 行是段落文本
    • 以 “0 0” 结束输入

特殊标记

  • *fN:切换字体(N=1-6)
  • *sN:切换字号(N=1-99)

输出要求

对于每个段落的每一行:

  • 行号
  • 该行的第一个和最后一个单词
  • 行尾的空白空间(如果单词超出行宽则为负值)

算法思路

关键点

  1. 字符宽度计算

    • 实际宽度 = (10点字号宽度 × 当前字号 + 5) / 10
    • 使用整数运算避免浮点精度问题
  2. 单词处理流程

    • 遇到特殊标记时更新字体和字号
    • 计算每个单词的宽度
    • 判断单词是否能放入当前行
    • 处理超长单词(单独占一行)
  3. 行构建策略

    • 尽可能多地放入单词
    • 单词间插入空格宽度
    • 记录每行的首尾单词

核心步骤

  1. 初始化:读取字体表,建立字符到索引的映射
  2. 段落处理
    • 合并所有输入行
    • 按单词处理文本流
    • 跟踪当前字体、字号和行状态
  3. 输出格式:按要求输出段落号和每行信息

代码实现

// Typesetting
// UVa ID: 235
// Verdict: Accepted
// Submission Date: 2025-10-06
// UVa Run Time: 0.010s
//
// 版权所有(C)2025,邱秋。metaphysis # yeah dot net#include <bits/stdc++.h>using namespace std;int font[110][10];
map<char, int> idx;
int paragraph = 1;
int L, W;void process(string lines) {int printedLines = 1;int size = 1, point = 10;int currentWidth = 0, spaceWidth;string firstWord, lastWord, word;istringstream iss(lines);cout << "Paragraph " << paragraph++ << '\n';while (iss >> word) {if (word[0] == '*') {if (word[1] == 'f') size = stoi(word.substr(2));if (word[1] == 's') point = stoi(word.substr(2));continue;}int wordWidth = 0;for (int i = 0; i < word.length(); i++)// use integer instead of round function, or you will get 'Wrong Answer'wordWidth += (font[idx[word[i]]][size] * point + 5) / 10;if ((currentWidth && currentWidth + spaceWidth + wordWidth >= W) || wordWidth >= W) {if (currentWidth) {cout << "Line " << printedLines++ << ": " << firstWord << " ... " << lastWord;cout << " (" << (W - currentWidth) << " whitespace)\n";currentWidth = 0;}if (wordWidth >= W) {cout << "Line " << printedLines++ << ": " << word;if (wordWidth == W) cout << " ... " << word;cout << " (" << (W - wordWidth) << " whitespace)\n";wordWidth = 0;}}if (currentWidth) currentWidth += spaceWidth;else firstWord = word;if (wordWidth) {currentWidth += wordWidth;lastWord = word;}// use integer instead of round function, or you will get 'Wrong Answer'spaceWidth = (font[idx[' ']][size] * point + 5) / 10;}if (currentWidth) {cout << "Line " << printedLines++ << ": " << firstWord << " ... " << lastWord;cout << " (" << (W - currentWidth) << " whitespace)\n";}
}int main(int argc, char *argv[]) {string line;getline(cin, line);int n = stoi(line);for (int i = 1; i <= n; i++) {getline(cin, line);istringstream iss(line);if (line.front() == ' ') idx.insert(make_pair(' ', i));else {char character;iss >> character;idx.insert(make_pair(character, i));}for (int j = 1; j <= 6; j++) iss >> font[i][j];}while (getline(cin, line)) {istringstream iss(line);iss >> L >> W;if (L == 0) break;string allLines;    for (int i = 1; i <= L; i++) {getline(cin, line);allLines += line + ' ';}process(allLines);}return 0;
}

总结

本题主要考察字符串处理和模拟能力,需要注意:

  • 字符宽度的整数计算方法
  • 特殊标记的识别和处理
  • 行构建时的边界条件判断
  • 输出格式的精确控制

通过合理的数据结构和清晰的逻辑流程,可以高效解决这类文本处理问题。

http://www.dtcms.com/a/453435.html

相关文章:

  • 东莞营销网站建设哪个平台好十大app排行榜
  • asp网站开发工具现在的企业一般用的什么邮箱
  • 企业区块链重新崛起
  • 【SSH】同一局域网下windows使用Xshell SSH连接另一台 ubuntu 22.04 电脑
  • [随手记] docker 镜像拉取记录
  • Ruoyi 赋能,百度天气不止当下:打造面向未来的预报实战
  • 网站搭建流程负责人长春制作网站哪家好
  • 日语学习-日语知识点小记-进阶-JLPT-N1阶段应用练习(7):语法 +考え方20+2022年7月N1
  • 天天爱天天做网站广告联盟没有网站怎么做
  • 1688 关键词搜索接口深度开发:从精准匹配到供应链筛选的技术实现
  • 网站多域名怎么做绿色网站模板大全
  • 深入理解 Vue 3 组件间数据传递的多种方式
  • 华威桥网站建设wordpress去除更新
  • .net 网站生成安装文件目录asp运行wordpress
  • 免费手机网站模板网站开发设计框图
  • 自助建站官网wordpress免费主机空间
  • systemd-resolved.service实验实战2
  • 基于 GEE 的 GWCCI 指数大豆识别与制图研究
  • Python argparse库介绍(内置的命令行参数解析库)(未检查)
  • JMeter之 json提取器与json path语法
  • 深度学习之神经网络2(Neural Network)
  • 做UI设计的网站网站界面设计尺寸
  • Python全栈(基础篇)——Day06:后端内容(定义函数+调用函数+实战演示+每日一题)
  • C++算法·贪心例题讲解
  • 成都正规集团网站制作维护商业综合体
  • 网站做生鲜线下推广建议wordpress可视化编辑主题
  • 《投资-81》价值投资者的认知升级与交易规则重构 - 第一层:商业模式详解、常见的优质模式商业模式、价值投资选择的标准
  • UI设计(一)应用商店展示图标封面标题——东方仙盟筑基期
  • 空间制作网站网站建设目标计划书
  • JAVA算法练习题day34