UVA245 WF5184 POJ1885 Uncompress【文本】

Uncompress
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 976 Accepted: 337

Description

A simple scheme for creating a compressed version of a text file can be used for files which contain no digit characters. The compression scheme requires making a list of the words in the uncompressed file. When a non-alphabetic character is encountered in the uncompressed file, it is copied directly into the compressed file. When a word is encountered in the uncompressed file, it is copied directly into the compressed file only if this is the first occurrence of the word. In that case, the word is put at the front of the list. If it is not the first occurrence, the word is not copied to the compressed file. Instead, its position in the list is copied into the compressed file and the word is moved to the front of the list. The numbering of list positions begins at 1.
Write a program that takes a compressed file as input and generates a reproduction of the original uncompressed file as output. You can assume that no word contains more than 50 characters and that the original uncompressed file contains no digit characters.
For the purposes of this problem, a word is defined to be a maximal sequence of upper- and lower-case letters. Words are case-sensitive - the word abc is not the same as the word Abc. For example,
x-ray contains 2 words: x and ray
Mary’s contains 2 words:Mary and s
It’s a winner contains 4 words:It and s and a and winner

Input

There will be no more than 10000 different words in the input. The end of the input is signified by the number 0 on a line by itself. The terminating 0 merely indicates the end of the input and should not be part of the output produced by your program.

Output

Output the the original uncompressed file.

Sample Input

Dear Sally,

Please, please do it–1 would 4
Mary very, 1 much. And 4 6
8 everything in 5’s power to make
14 pay off for you.

– Thank 2 18 18–
0

Sample Output

Dear Sally,

Please, please do it–it would please
Mary very, very much. And Mary would
do everything in Mary’s power to make
it pay off for you.

– Thank you very much–

Source

World Finals 1995

World Finals >> 1995 - Nashville

問題鏈接UVA245 WF5184 POJ1885 Uncompress
問題簡述:(略)
問題分析
    簡單的文本處理題。數據表示是關鍵,不解釋。
程序說明
    這裏給出2個程序,前一種是推薦的。一般而言,數據規模已知的情況下用數組(結構數組)效率會比較好。動態存儲分配的時間與存儲的成本都會相對高一些。
參考鏈接:(略)
題記:(略)

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

/* UVA245 WF5184 POJ1885 Uncompress */

#include <iostream>
#include <stdio.h>
#include <ctype.h>

using namespace std;

const int N = 10000;
struct Node {
    string word;
    int next;
} word[N];
int wcnt = 0, head;

int main()
{
    char c;
    c = getchar();
    for(;;) {
        if(c == '0') break;
        else if(isdigit(c)) {
            int num = 0;
            while(isdigit(c)) {
                num *= 10;
                num += c - '0';
                c = getchar();
            }

            int cur = head, prev;
            for(int i = 1; i < num; i++) {
                prev = cur;
                cur = word[cur].next;
            }

            printf("%s", word[cur].word.c_str());

            if(cur != head) {
                word[prev].next = word[cur].next;
                word[cur].next = head;
                head = cur;
            }
        } else if(isalpha(c)) {
            string t;
            while(isalpha(c)) {
                t += c;
                c = getchar();
            }
            printf("%s", t.c_str());
            word[wcnt].word = t;
            word[wcnt].next = head;
            head = wcnt++;
        } else {
            putchar(c);
            c = getchar();
        }
    }

    return 0;
}

AC的C++語言程序(動態存儲分配)如下:

/* UVA245 WF5184 POJ1885 Uncompress */

#include <iostream>
#include <stdio.h>
#include <ctype.h>

using namespace std;

struct Node {
    string word;
    Node *next;
} *head;

int main()
{
    char c;
    c = getchar();
    for(;;) {
        if(c == '0') break;
        else if(isdigit(c)) {
            int num = 0;
            while(isdigit(c)) {
                num *= 10;
                num += c - '0';
                c = getchar();
            }

            Node *cur = head, *prev;
            for(int i = 1; i < num; i++) {
                prev = cur;
                cur = cur->next;
            }

            printf("%s", cur->word.c_str());

            if(cur != head) {
                prev->next = cur->next;
                cur->next = head;
                head = cur;
            }
        } else if(isalpha(c)) {
            string word;
            while(isalpha(c)) {
                word += c;
                c = getchar();
            }

            printf("%s", word.c_str());

            Node *node = new Node;
            node->word = word;
            node->next = head;
            head = node;
        } else {
            putchar(c);
            c = getchar();
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章