题目
数据压缩,一个只有字母构成的文本(区分大小写),将所有的单词存到一个字典中,将最新的单词放在最前面(栈),每次遇到重复单词的时候,利用该单词在字典中的位置定义该单词,并将单词列表移到字典最前面。讲一个压缩后的文本解压。
分析
利用vector存储查询输出即可。
说明
注意数字和字母连在一起的情况,ε=(´ο`*)))唉
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
vector<string> word_list;
int main()
{
string buf;
while (getline(cin, buf) && buf[0] != '0') {
int value = 0, state = 0; // 2:number, 1:letter, 0:others
string s;
for (int i = 0; buf[i]; ++ i) {
if (isalpha(buf[i])) {
s = "";
while (isalpha(buf[i]))
s.insert(s.end(), buf[i ++]);
word_list.push_back(s);
i --;
cout << s;
} else if (isdigit(buf[i])) {
value = 0;
while (isdigit(buf[i]))
value = value * 10 + buf[i ++] - '0';
s = word_list[word_list.size() - value];
word_list.erase(word_list.end() - value);
word_list.push_back(s);
i --;
cout << s;
} else
printf("%c", buf[i]);
}
puts("");
}
return 0;
}
739

被折叠的 条评论
为什么被折叠?



