A:Simple prefix compression
-
总时间限制:
- 2000ms 内存限制:
- 65536kB
-
描述
-
Many databases store the data in the character fields (and especially indices) using prefix compression. This technique compresses a sequence of strings A1, ..., A
N by the following method: if there are strings Ai = a
i,1a
i,2...a
i,p and A
i + 1 = a
i+1,1a
i+1,2...a
i+1,q
such that for some j ≤ min(p, q) a i,1 = a i+1,1, a i,2 = a i+1,2, ... a i,j = a i+1,j, then the second string is stored as [j]a i+1,j+1a i+1,j+2... a i+1,q, where [j] is a single character with code j.
If j = 0, that is, strings do not have any common prefix, then the second string is prefixed with zero byte, and so the total length actually increases.
-
Constraints
1 ≤ N ≤ 10000, 1 ≤ length(Ai) ≤ 255.
输入
- First line of input contains integer number N, with following N lines containing strings A1 ... A N 输出
- Output must contain a single integer -- minimal total length of compressed strings. 样例输入
-
3 abc atest atext
样例输出
-
11
来源
-
Northeastern Europe 2003, Far-Eastern Subregion
-
本题是在刚刚讲完递归的时候布置的作业,但是我没有搞清楚怎么用递归做……所以就是用逐个比较的方法写的,运行时间很长的样子。
内存:6252kB 时间:290ms
-
B:文件结构“图”
-
总时间限制:
- 1000ms 内存限制:
- 65536kB
-
描述
-
在计算机上看到文件系统的结构通常很有用。Microsoft Windows上面的"explorer"程序就是这样的一个例子。但是在有图形界面之前,没有图形化的表示方法的,那时候最好的方式是把目录和文件的结构显示成一个"图"的样子,而且使用缩排的形式来表示目录的结构。比如:
ROOT | dir1 | file1 | file2 | file3 | dir2 | dir3 | file1 file1 file2
这个图说明:ROOT目录包括三个子目录和两个文件。第一个子目录包含3个文件,第二个子目录是空的,第三个子目录包含一个文件。
输入
- 你的任务是写一个程序读取一些测试数据。每组测试数据表示一个计算机的文件结构。每组测试数据以'*'结尾,而所有合理的输入数据以'#'结尾。一组测试数据包括一些文件和目录的名字(虽然在输入中我们没有给出,但是我们总假设ROOT目录是最外层的目录)。在输入中,以']'表示一个目录的内容的结束。目录名字的第一个字母是'd',文件名字的第一个字母是'f'。文件名可能有扩展名也可能没有(比如fmyfile.dat和fmyfile)。文件和目录的名字中都不包括空格,长度都不超过30。一个目录下的子目录个数和文件个数之和不超过30。 输出
-
在显示一个目录中内容的时候,先显示其中的子目录(如果有的话),然后再显示文件(如果有的话)。文件要求按照名字的字母表的顺序显示(目录不用按照名字的字母表顺序显示,只需要按照目录出现的先后显示)。对每一组测试数据,我们要先输出"DATA SET x:",这里x是测试数据的编号(从1开始)。在两组测试数据之间要输出一个空行来隔开。
你需要注意的是,我们使用一个'|'和5个空格来表示出缩排的层次。
样例输入
-
file1 file2 dir3 dir2 file1 file2 ] ] file4 dir1 ] file3 * file2 file1 * #
样例输出
-
DATA SET 1: ROOT | dir3 | | dir2 | | file1 | | file2 | dir1 file1 file2 file3 file4 DATA SET 2: ROOT file1 file2
提示
-
一个目录和它的子目录处于不同的层次
一个目录和它的里面的文件处于同一层次
来源
- 翻译自 Pacific Northwest 1998 的试题
本题在递归里面算是较简单的,个人的基本思路是每遇到一个文件夹就递归进入下一个函数。用set存储文件名。
内存:128kB 时间:1ms
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n;
cin >> n;
char a[256], b[256];
cin >> b;
int total = strlen(b);
while (--n)
{
strcpy(a, b);
cin >> b;
int cnt = 0;
// 以下为逐个比较的过程,按字符串长短分两种情况
if (strlen(b) < strlen(a))
{
for (int i = 0; i < strlen(b); i++)
{
if (a[i] == b[i]) cnt++;
else break;
}
}
else
{
for (int i = 0; i < strlen(a); i++)
{
if (a[i] == b[i]) cnt++;
else break;
}
}
total += strlen(b) - cnt + 1;
}
cout << total << endl;
return 0;
}
#include <iostream>
#include <cstring>
#include <set>
using namespace std;
int total = 1;
char a[31];
void f(int m, char* c) // numbern, m layer
{
for (int j = 0; j < m; j++)
{
cout << "| ";
}
cout << c << endl;
int count = 0;
set<string> filename;
set<string>::iterator it;
char b[31];
if (!m)
{
strcpy(b, a);
if (b[0] == '*')
{
cout << endl;
total++;
return;
}
if (b[0] == 'f')
{
filename.insert(string(b));
}
if (b[0] == 'd')
{
f(m + 1, b); // 递归
}
}
while (cin >> b)
{
if (b[0] == '*')
{
for (it = filename.begin(); it != filename.end(); it++)
{
cout << *it << endl;
}
cout << endl;
total++;
return;
}
if (b[0] == ']')
{
for (it = filename.begin(); it != filename.end(); it++)
{
for (int j = 0; j < m; j++)
{
cout << "| ";
}
cout << *it << endl;
}
return;
}
if (b[0] == 'f')
{
filename.insert(string(b));
}
if (b[0] == 'd')
{
f(m + 1, b); // 递归
}
}
}
int main()
{
while(cin >> a)
{
if (a[0] == '#') break;
cout << "DATA SET " << total << ":" << endl;
f(0, "ROOT");
}
return 0;
}