24h購物| | PChome| 登入
2012-07-15 10:14:15| 人氣4,618| 回應0 | 上一篇 | 下一篇

[UVA][linked list] 245 - Uncompress

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


 Uncompress 

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,

tabular23

There is no upper limit on the number of different words in the input file. The end of the input file 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.

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--




很久沒寫 linked list 了, 有些許的耍白癡

#include <iostream>
#include <ctype.h>
using namespace std;
struct Node {
    string word;
    struct Node *next;
};
int main() {
    string in;
    Node* head = NULL;
    while(getline(cin, in)) {
        if(in == "0")
            break;
        int i, len = in.length();
        for(i = 0; i < len; i++) {
            if(!isalpha(in[i])) {
                if(isdigit(in[i])) {
                    int num = 0;
                    while(isdigit(in[i])) {
                        num = num*10 + in[i]-'0';
                        i++;
                    }
                    i--;
                    Node *now, *prev;
                    now = head;
                    prev = NULL;
                    for(int j = 1; j < num; j++) {
                        prev = now;
                        now = now->next;
                    }
                    cout << now->word;
                    if(prev != NULL) {
                        prev->next = now->next;
                    }
                    if(head != now) {
                        now->next = head;
                    }
                    head = now;
                } else {
                    cout << in[i];
                }
            } else {
                string s = "";
                int j(i);
                while(isalpha(in[j])) {
                    s += in[j];
                    j++;
                }
                i = j-1;
                cout << s;
                Node *now, *prev;
                now = head;
                prev = NULL;
                while(now != NULL) {
                    if(now->word == s)
                        break;
                    prev = now;
                    now = now->next;
                }
                if(now != NULL) {
                    if(prev != NULL) {
                        prev->next = now->next;
                    }
                    now->next = head;
                    head = now;
                } else {
                    Node *now = new Node();
                    now->word = s;
                    now->next = head;
                    head = now;
                }
            }/*
            Node *now, *prev;
            now = head;
            prev = NULL;
            cout << endl;
            while(now != NULL) {
                cout << now->word << "->";
                prev = now;
                now = now->next;
            }
            cout << endl;
            system("pause");*/
        }
        cout << endl;
    }
    return 0;
 }

台長: Morris
人氣(4,618) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dfs] 148 - Anagram checker
此分類上一篇:[UVA] 213 - Message Decoding

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