UVA245 WF5184 POJ1885 Uncompress【文本】

509 篇文章 7 订阅
281 篇文章 4 订阅

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值