题目2 : 字符串压缩
描写叙述
你的硬盘上有一个神奇的文件占用了大量空间。你决定将其压缩以节省空间。不幸的是,你还没有安装不论什么压缩软件,所以你决定自己编写一个压缩程序。你发现这是一个文本文件,包含非常多行。
每行是一个长度恰好为L的字符串,并且字符串可能有反复。行的顺序并不重要。换言之,打乱顺序之后仍然能够觉得文件内容和原来同样。
比如,这个文件的内容能够是这种:
bar
car
bat
cat
cat
经过一段时间观察,你发现同一列的字符往往是同样的。于是你设计了一个简单的压缩框架。首先以某种策略调整行的顺序。然后把全部字符串依照先列后行的顺序变换成单个字符串。比如上面的例子。不调整顺序则直接变换成:
bcbccaaaaarrttt
然后使用游程编码(RLE)的到压缩变换后的字符串:
1b1c1b2c5a2r3t
当然也能够先调换顺序:
car
cat
cat
bat
bar
这种压缩字符串为:
3c2b5a1r3t1r
比不调整顺序的稍短一些。
如今,你已经得到了两个不同的压缩字符串,你想知道他们解压后的文件是否同样,请写一个程序解决问题。
输入
第一行是一个整数T (T <= 30),表示測试数据组数。
每组測试数据占三行。
第一行为整数L。表示原始文件里每一行字符串的长度。第二行和第三行各自是两个压缩字符串。格式如c1 n1 c2 n2 … cMnM,表示字符ci连续出现了ni次。详细格式见例子。输入字符串仅仅含a到z的小写字母。确保压缩字符串合法有效,且不为空。
输出
对每组測试数据。首先输出”Case x: ”。当中x表示測试数据编号。假设两个压缩字符串相应于同样的文件内容。则输出”Yes”,否则输出”No”。
数据范围
小数据:1<=L<=10, 1<=ni<=100,压缩字符串长度不超过10^4
大数据:1<=L<=1000, 1<=ni<=10^9,压缩字符串长度不超过10^6
2 3 1b1c1b2c5a2r3t 3c2b5a1r3t1r 2 20a20b10a20b10a 20a20b20a20b例子输出
Case 1: Yes Case 2: No