24h購物| | PChome| 登入
2013-07-31 09:35:21| 人氣1,857| 回應0 | 上一篇 | 下一篇

[UVA][字串處理] 848 - Fmt

推薦 0 收藏 0 轉貼0 訂閱站台


  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 at the end of a blank or empty line and 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.



Miguel Revilla 2002-06-15

這題的英文描述相當不是很好理解,不過就想像一下 OFFICE WORD 或者一般撰寫英文文章時,
盡可能讓同一段落的英文並排。

並且不會將一個英文單字分成兩個部分,即跨行。

怎麼判斷同一個段落?這是一個很重要的關鍵。

同一個段落是要看第一行非空字串表示一個段落的開始,接下來直到一個空行或者是一個空字元起頭的字串。
表示一個段落的結束。

題目就是整理段落格式。


#include <stdio.h>
#include <string.h>
using namespace std;
char paragraph[100005];
int paralen;
void printPara() {
    if(paralen == 0)    return ;
    char line[105], word[105];
    int llen = 0, wlen = 0;
    int i, j, k;
    for(i = 0; paragraph[i] == ' '; i++)
        line[llen++] = ' ';
    paragraph[paralen] = '\0';
    //puts(paragraph);
    while(i < paralen) {
        wlen = 0;
        for(; i < paralen && paragraph[i] != ' '; i++)
            word[wlen++] = paragraph[i];
        word[wlen] = '\0';
        if(wlen + llen > 72) {
            while(llen > 0 && line[llen-1] == ' ')
                llen--;
            line[llen] = '\0';
            //if(llen == 0)   continue;
            puts(line);
            llen = 0;
            //ignore space
        }
        for(j = 0; j < wlen; j++)
            line[llen++] = word[j];
        for(; i < paralen && paragraph[i] == ' '; i++)
            line[llen++] = ' ';
        //printf("%d %s %d %d\n", i, word, wlen, llen);
        /*getchar();*/
    }
    if(llen) {
        while(llen > 0 && line[llen-1] == ' ')
            llen--;
        line[llen] = '\0';
        //if(llen == 0)   continue;
        puts(line);
    }
}
int main() {
    /*freopen("in.txt", "r+t", stdin);
    freopen("out.txt", "w+t", stdout);*/
    char line[1005];
    paralen = 0;
    while(gets(line)) {
        //trim tail spaces
        int l = strlen(line)-1;
        while(l >= 0 && line[l] == ' ')    l--;
        line[l+1] = '\0', l++;
        //<end>
        if(l == 0 || line[0] == ' ') {// new paragraph
            //printf("%d\n", paralen);
            printPara();
            paralen = 0;
            if(l == 0)
                puts("");
            else {
                int i;
                for(i = 0; line[i]; i++)
                    paragraph[paralen++] = line[i];
            }
        } else {
            int i;
            if(paralen != 0)
                paragraph[paralen++] = ' ';
            for(i = 0; line[i]; i++)
                paragraph[paralen++] = line[i];
        }
    }
    printPara();
    return 0;
}

台長: Morris
人氣(1,857) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: UVA |
此分類下一篇:[UVA][關節點] 610 - Street Directions
此分類上一篇:[UVA][模擬] 168 - Theseus and the Minotaur

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文