UVA848 Fmt文本处理

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA848 Fmt文本处理相关的知识,希望对你有一定的参考价值。

The unix fmt program reads lines of text, combining and breaking lines so as to create an output file with lines as close to without exceeding 72 characters long as possible. The rules for combining and breaking lines are as follows.
• A new line may be started anywhere there is a space in the input. If a new line is started, there will be no trailing blanks at the end of the previous line or at the beginning of the new line.
• A line break in the input may be eliminated in the output, provided it is not followed by a space or another line break. If a line break is eliminated, it is replaced by a space.
• Spaces never appear at the end of a line.
• If a sequence of characters longer than 72 characters appears without a space or line break, it appears by itself on a line.
Sample Input

   Unix fmt
   
The unix fmt program reads lines of text, combining
and breaking lines so as to create an
output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.

   1. A new line may be started anywhere there is a space in the input.
If a new line is started, there will be no trailing blanks at the
end of the previous line or at the beginning of the new line.

   2. A line break in the input may be eliminated in the output, provided
it is not followed by a space or another line break. If a line
break is eliminated, it is replaced by a space.

Sample Output

Unix fmt

The unix fmt program reads lines of text, combining and breaking lines
so as to create an output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.

   1. A new line may be started anywhere there is a space in the
input. If a new line is started, there will be no trailing blanks at
the end of the previous line or at the beginning of the new line.

   2. A line break in the input may be eliminated in the output,
provided it is not followed by a space or another line break. If a
line break is eliminated, it is replaced by a space.

问题链接UVA848 Fmt
问题简述:(略)
问题分析:文本处理问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA848 Fmt */

#include <bits/stdc++.h>

using namespace std;

void output(string & s)
{
    while (s.size() > 0 && s.back() == ' ')
        s.pop_back();
    puts(s.c_str());
}

int main()
{
    string line, last;
    while (getline(cin, line)) {
        if (line.empty()) {
            if (!last.empty()) output(last);
            output(line);
            last = line;
        } else {
            if (line[0] == ' ') {
                if (!last.empty()) output(last);
                last.clear();
            } else if (!last.empty())
                line = ' ' + line;
            for (int i = 0; i < (int)line.size(); i++) {
                if (line[i] == ' ') last += ' ';
                else {
                    int j = i;
                    while (j < (int)line.size() && line[j] != ' ') j++;
                    if (last.size() + j - i > 72) {
                        if (!last.empty()) {
                            output(last);
                            last.clear();
                        }
                    }
                    last += line.substr(i, j - i);
                    i = j - 1;
                }
            }
        }
    }
    if (!last.empty()) output(last);

    return 0;
}

以上是关于UVA848 Fmt文本处理的主要内容,如果未能解决你的问题,请参考以下文章

UVA620 Cellular Structure文本处理

代码片段 - Golang 实现简单的 Web 服务器

UVA11548 Blackboard Bonanza文本处理

UVA10508 Word Morphing文本处理

1 代码片段1

golang代码片段(摘抄)