P1 幸运数字
题面
如果把一个数字十进制表示的奇数位和偶数位分别相加得到的和相同,小小就认为它是一个幸运的数字。例如:
- 奇数位相加 、偶数位相加 ,因此 不是幸运数字;
- 奇数位相加 、偶数位相加 ,因此 是幸运数字。
对于给定的 和 ,小小希望你求出 中幸运数字的数量。
输入格式
输入数据仅一行,包含空格分隔的两个整数 和 。
输出格式
输出一行一个整数,代表 中幸运数字的数量。
样例输入 1
1 100样例输出 1
9样例输入 2
4096 65536样例输出 2
3454数据规模
- 对于 100% 的数据,满足 。
题解
模拟即可。注意:奇数位和偶数位,无论从左往右数,还是从右往左数,不影响结果。
上代码:
#include <iostream>
using namespace std;
bool check(int x)
{
bool flag = false;
int cnt0 = 0, cnt1 = 0;
while (x)
{
int now = x % 10;
x /= 10;
if (flag) cnt1 += now;
else cnt0 += now;
flag = !flag;
}
return cnt0 == cnt1;
}
int main()
{
int a, b;
cin >> a >> b;
int cnt = 0;
for (int i = a; i <= b; i++)
{
if (check(i))
{
cnt++;
}
}
cout << cnt;
return 0;
}P2 精密计时
题面
小小有一个非常精密的计时器,每秒可以计数 100 次 (两个连续的时钟计数之间恰好间隔百分之一秒)。例如,以下是从 13:01:02.37 到 13:01:03.01 的计时过程:
13:01:02.37 (13 时 1 分 2 秒.37)
13:01:02.38
13:01:02.39
...
13:01:02.98
13:01:02.99 (13 时 1 分 2 秒.99)
13:01:03.00 (13 时 1 分 3 秒.00)
13:01:03.01小小记录了计时器上的两个时刻,你能帮助他计算这两个时刻之间经过了多少个 “百分之一秒”吗?
输入格式
输入数据仅一行,包含空格分开的两个字符串,代表了两个计时器上显示的时刻。时刻中的时、分、秒、百分之一秒均使用两位十进制数字表示,不足两位时在十位添零。时、分、秒之间用冒号 : 分隔,秒和百分之一秒之间用小数点 . 分隔。
输出格式
输出一行一个整数,代表两个时刻之间经过的 “百分之一秒” 数。
样例输入 1
01:02:34.56 01:03:00.10样例输出 1
2554样例输入 2
00:00:00.00 23:59:59.99样例输出 2
8639999数据规模
- 对于 100% 的数据,两个时刻都来自 24 小时制的同一天 (
00:00:00.00到23:59:59.99),且保证后一个时刻晚于前一个时刻。
题解
只要将两个时刻转化为整数就行了。注意可以用 scanf 读入。
本题数据有锅,可以用 cin+getchar 替代 scanf。
上代码:
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int h1, m1, s1, ms1;
int h2, m2, s2, ms2;
scanf("%d:%d:%d.%d", &h1, &m1, &s1, &ms1);
scanf("%d:%d:%d.%d", &h2, &m2, &s2, &ms2);
int t1 = ms1 * 1 + s1 * 100 + m1 * 6000 + h1 * 360000;
int t2 = ms2 * 1 + s2 * 100 + m2 * 6000 + h2 * 360000;
cout << t2 - t1;
return 0;
}P3 图像重建
题面
JWST (詹姆斯·韦伯太空望远镜,James Webb Space Telescope) 是一台红外波段的大型太空望远镜,由美国国家航空航天局 (NASA)、欧洲航天局 (ESA) 和加拿大航天局 (CSA) 共同合作开发。它是哈勃太空望远镜的科学继任者,旨在解决一系列重要的天文学问题,包括宇宙的起源、星系的形成和演化、恒星和行星系统的形成,以及寻找宜居行星和生命迹象。
太空望远镜在采集数据时,并不像我们日常生活中的手机或相机 “一次拍摄成像” 直接从传感器数据得到照片,而是由多次不同时段拍摄的图像拼接而成。在这个问题中,我们也来实现图像的拼接重建。
我们已经对一个区域拍摄了两张黑白图像。这两张图像面向同一区域拍摄,因此我们预期它们有相当一部分都是重叠的。你的任务就是将两张图像经过上下左右平移后尽可能 “重叠” 在一起,满足重叠部分所有像素均完全相同,且重叠部分的面积尽可能大 (即重叠部分的像素数量尽可能多)。
输入格式
输入数据由两张图像的描述组成。两张图像的描述之间有一个空行。
对于每张图像,第一行包含两个整数 和 ,代表了图像的尺寸 (像素的行数和列数)。接下来 行,每行 个像素描述了拍摄的图像,其中 0 表示一个黑色像素,1 表示一个白色像素。每一行的像素由一个空格分隔。
输出格式
输出一行一个整数,即经过最优平移后,重叠部分的像素数量。
样例输入 1
3 3
0 0 0
0 1 1
0 1 1
2 4
1 1 0 0
1 1 0 1样例输出 1
4压缩包中包含更多的样例数据。
数据规模
- 对于 100% 的数据,满足 。
题解
枚举。注意,不要枚举其他内容,只需枚举其中一幅画平移的格数。复杂度:
上代码:
#include <bits/stdc++.h>
using namespace std;
int a[51][51], b[51][51];
int main()
{
int n1, m1;
cin >> n1 >> m1;
for (int i = 1; i <= n1; i++)
{
for (int j = 1; j <= m1; j++)
{
cin >> a[i][j];
}
}
int n2, m2;
cin >> n2 >> m2;
for (int i = 1; i <= n2; i++)
{
for (int j = 1; j <= m2; j++)
{
cin >> b[i][j];
}
}
int ans = 0;
for (int mx = n2; mx >= -n1; mx--)//枚举第二幅画向上的格数
{
for (int my = m2; my >= -m1; my--)//枚举第二幅画向左的格数
{
int sx1 = 1 , sy1 = 1 , ex1 = n1 , ey1 = m1 ;
int sx2 = 1 - mx, sy2 = 1 - my, ex2 = n2 - mx, ey2 = m2 - my;
int sx = max(sx1, sx2);
int sy = max(sy1, sy2);
int ex = min(ex1, ex2);
int ey = min(ey1, ey2);
bool ok = true;
int cnt = 0;
for (int i1 = sx, i2 = (sx + mx); i1 <= ex, i2 <= (ex + mx); i1++, i2++)
{
for (int j1 = sy, j2 = (sy + my); j1 <= ey, j2 <= (ey + my); j1++, j2++)
{
if (a[i1][j1] != b[i2][j2]) ok = false;
cnt++;
}
}
if (ok) ans = max(ans, cnt);
}
}
cout << ans;
return 0;
}P4 程序分析
题面
程序分析技术是一种用于理解和改进计算机程序的方法。它可以帮助我们找出程序中的错误、提高程序的性能、优化代码结构等。其中,静态分析技术在不运行程序的情况下对程序代码进行分析。它可以检查代码的语法、风格、潜在错误等。例如,静态分析可以帮助我们找出程序中未使用的变量、可能的数组越界等问题。
小小设计了一个自己的编程语言,并命名为 X 语言。你能为它设计一个静态分析器吗?
X 语言程序中只有两个整型变量 和 ,且无需定义,可以直接使用。变量 的值从程序外输入 (输入值可以是任何 C++ int 范围内的值), 的初始值是 。一个 X 语言程序由若干行组成,每行恰好包含一条命令,是以下三种命令之一:
- 条件分支:
if (条件) { - 对 赋值:
y = 数字; - 条件结束:
}
其中,“条件” 要么是 “x > 数字”,要么是 “x < 数字”。赋值语句和条件中的 “数字” 都是 1 到 1,000,000,000 之间的常数。if 和赋值的含义同 C++ 语言中的条件和赋值语句。
请你编写一个静态分析器,分析一个 X 语言程序执行结束时,所有可能的 的值。
输入格式
输入数据第一行为整数 ,代表程序的行数。
接下来 行,每行一个命令,描述了一个合法的 X 语言程序:输入的程序保证括号配对,且符合问题描述中的约定。为了便于大家解析 (例如用 cin 或 scanf 读入),输入程序中的 if 后、{ 前、=、<、> 左右都恰好有一个空格,行首可能有若干空格缩进。除此之外,输入不含多余的空格或空白字符。
输出格式
输出一行,从小到大不重复地输出程序结束时,变量 所有可能的值。数字之间由一个空格隔开。
样例输入 1
10
if (x > 1) {
y = 2;
if (x > 10) {
y = 1;
y = 4;
if (x < 5) {
y = 3;
}
}
}样例输出 1
0 2 4压缩包中包含更多的样例数据。
数据规模
- 对于 100% 的数据,满足 。输入数据的每行都不超过 个字符。
题解
思路1 解释执行
用递归结构解释执行该程序,将 依次代入。很显然,会 TLE,所以要进行优化。
对于以下程序:
if (x > 1) {
if (x < 1000000000) {
y = 10;
}
}任何 ,有 。多余的分析是完全浪费的。
我们可以只分析所有条件的数字 ,这样,最多只要分析 个数,每个数需要 的复杂度,共需要 的复杂度,足够优秀。
可以运用类似于表达式计算的算法,利用栈来解析程序。
上代码:
#include <iostream>
#include <utility>
#include <stack>
#include <cstdio>
#include <set>
using namespace std;
pair<int, int> code[1001];
int n;
int excute(int x)
{
stack<pair<int, int> > stk;
for (int i = 1; i <= n; i++)
{
if (code[i].first == 1)
{
int now = -1;
while (!stk.empty() && stk.top().first >= 0)
{
if (stk.top().first == 0 && now == -1) now = stk.top().second;
stk.pop();
}
if (stk.top().first == -1)
{
if (x > stk.top().second)
{
stk.pop();
if (now != -1) stk.push(make_pair(0, now));
}
else
{
stk.pop();
}
}
else
{
if (x < stk.top().second)
{
stk.pop();
if (now != -1) stk.push(make_pair(0, now));
}
else
{
stk.pop();
}
}
}
else
{
stk.push(code[i]);
}
}
if (!stk.empty()) return stk.top().second;
else return 0;
}
int main()
{
cin >> n;
set<int> bv;
for (int i = 1; i <= n; i++)
{
string t;
cin >> t;
if (t == "if")
{
getchar(); getchar(); getchar(); getchar();
char ch = getchar();
if (ch == '<')
{
code[i].first = -2;
getchar();
cin >> code[i].second;
}
if (ch == '>')
{
code[i].first = -1;
getchar();
cin >> code[i].second;
}
bv.insert(code[i].second);
bv.insert(code[i].second - 1);
bv.insert(code[i].second + 1);
getchar(); getchar(); getchar(); getchar();
}
if (t == "y")
{
code[i].first = 0;
getchar(); getchar(); getchar();
cin >> code[i].second;
getchar(); getchar();
}
if (t == "}")
{
code[i].first = 1;
code[i].second = 0;
}
}
set<int> ans;
for (int v : bv)
{
int res = excute(v);
ans.insert(res);
}
for (int nv : ans) cout << nv << ' ';
return 0;
}思路2 范围划分
我们发现,任何一个X语言程序都可以转化为 这样的形式。
这样一来,只要递归将该程序改写为以上形式即可。
复杂度:
代码略,建议大家使用思路1,简单易懂。
P5 文本压缩
题面
数据压缩算法的目的是减少数据的大小,以便更快地传输和存储。我们经常会用到的 zip、rar 等压缩工具,就是利用数据压缩算法把多个文件或者文件夹压缩成一个更小的文件;我们的网页在传输时,通常也使用了 gzip 压缩。有些时候 (例如传输图像、视频时),我们会允许在压缩过程中损失一些精度,以实现更好的压缩比。
在这个问题里,你需要自己设计一个英文文本的无损压缩和解压缩算法。你的程序需要同时实现压缩器和解压缩器两部分功能:
- 压缩器输入一个仅由小写字母组成的字符串,输出一个压缩后的字符串。压缩后的字符串允许使用大写字母、小写字母和数字,但不允许使用其他字符。
- 解压缩器输入一个压缩后的字符串,还原出小写字母的字符串。
注意,在这个问题中,所有给压缩器的输入都来自人工智能 GPT-3.5-turbo 生成的英文文本保留字母 (并转换为小写) 后得到的,也就是说,你可以假设除了偶尔的例外,字符串是由英文单词拼接而成的。这个性质是解决问题的关键——随机序列的压缩比 “有规律” 序列的压缩要困难得多。
输入格式
输入数据第一行为一个大写英文单词,代表压缩/解压缩的任务类型。“COMPRESS” 代表压缩;“DECOMPRESS” 代表解压缩。
第二行一个字符串,是压缩/解压缩任务对应的文本。对于压缩任务,是一个仅包含小写字母的字符串;对于解压缩任务,字符串总是来自你程序之前 COMPRESS 压缩的结果。
输出格式
输出一行一个字符串。对于压缩任务,输出一行为压缩后的文本;对于解压缩任务,输出一行为解压缩后的文本。
样例数据
样例仅给出了压缩任务所需的字符串 (由人工智能 GPT-3.5-turbo 生成),不含样例输出;你可以在给出的样例上实验你的压缩算法。
由于样例过大,在本文中无法排版,请自行找数据。
数据规模
- 对于 100% 的数据,输入字符串的长度 满足 。
评测说明与评分标准
本题的评测机会两次调用你的程序:
- 第一次调用你的程序执行压缩任务,例如输入
代码 COMPRESS aaaaaaaabbbbbbbbbb
假设本次调用,你的程序输出了 a8b10 作为压缩后的文本。这个输出会被评测系统记住。
- 第二次,调用你的程序执行解压缩任务。针对刚才的例子,我们会为你的程序输入
代码 DECOMPRESS a8b10
如果你的程序解压缩输出 aaaaaaaabbbbbbbbbb (即正确解压缩得到原字符串),则被判定为 “还原正确”。
对于每个测试数据,压缩和解压缩分别有 1s 的运行时间限制。
对于每个测试数据,在解压缩能正确得到原字符串的前提下:
- 如果压缩率达到 75% (压缩字符串的长度小于等于输入长度的 3/4),该测试点得满分。
- 如果压缩率达到 80% (压缩字符串的长度小于等于输入长度的 4/5),该测试点得一半分数。
题解
80% 的压缩率
注意到,,我们可以利用进制转换,将 26 进制转 62 进制,就能拿到一半的分数。
代码:
#include <bits/stdc++.h>
#define tc(x) (char)((x<10)?(x+'0'):((x<36)?(x-10+'A'):(x-36+'a')))
#define mk(s) ((s[4]-'a')*1+(s[3]-'a')*26+(s[2]-'a')*676+(s[1]-'a')*17576+(s[0]-'a')*456976)
#define tc2(x) (char)(x+'a')
#define ti(c) (((c>='0')&&(c<='9'))?(c-'0'):(((c>='A')&&(c<='Z'))?(c-'A'+10):(c-'a'+36)))
using namespace std;
string _5_2_4_(string _5_)
{
int x = mk(_5_);
int _4 = x % 62; x /= 62;
int _3 = x % 62; x /= 62;
int _2 = x % 62; x /= 62;
int _1 = x % 62; x /= 62;
string res;
res += tc(_1);
res += tc(_2);
res += tc(_3);
res += tc(_4);
return res;
}
string _4_2_5_(string _4_)
{
int _4 = ti(_4_[3]);
int _3 = ti(_4_[2]);
int _2 = ti(_4_[1]);
int _1 = ti(_4_[0]);
int resi = _4 * 1 + _3 * 62 + _2 * 3844 + _1 * 238328;
string res;
int __5 = resi % 26; resi /= 26;
int __4 = resi % 26; resi /= 26;
int __3 = resi % 26; resi /= 26;
int __2 = resi % 26; resi /= 26;
int __1 = resi % 26; resi /= 26;
res += tc2(__1);
res += tc2(__2);
res += tc2(__3);
res += tc2(__4);
res += tc2(__5);
return res;
}
int main()
{
string t;
cin >> t;
if (t == "COMPRESS")
{
string s;
cin >> s;
int add;
string res;
for (int i = 0; i < s.length(); i += 5)
{
string now;
if ((s.length() - i) <= 5)
{
now = s.substr(i, s.length() - i);
add = (5 - (s.length() - i));
for (int j = 1; j <= add; j++) now += 'a';
}
else now = s.substr(i, 5);
res += _5_2_4_(now);
}
res = (char)(add + '0') + res;
cout << res;
}
if (t == "DECOMPRESS")
{
getchar();
char ad = getchar();
int add = ad - '0';
string s;
cin >> s;
string res;
for (int i = 0; i < s.length(); i += 4)
{
string now = s.substr(i, 4);
res += _4_2_5_(now);
}
while (add--) res.erase(res.end() - 1);
cout << res;
}
return 0;
}75% 的压缩率
英语中,有很多字符串是重复的,例如:forexample、onceuponatime、allinall。只要将这些字符串压缩了,就能达到 75% 的压缩率。
我们可以先压缩,再转进制;也可以反过来。
为什么我们的压缩达不到 rar 的压缩率:rar 是二进制文件。
Update 2025/8/29:
达到 75% 是困难的,每前进 1% 都是进步
提高的思路:先用 LZ77,达到了 95%(浪费多),接着改为浪费的用进制转换,达到 79.6%,再简化短语,得到 78.5%(以下结果)。
onceuponatime,oneday最常见,下面给出一份结合简化版 LZ77、进制转换、常见短语压缩的代码及评测结果。Python def compress(string): code_table = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" window_len = 62 dictionary = "#" * window_len i = 0 n = len(string) ret = [] def b26_b59(b26): code26 = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25} code59 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ3456789" n = len(b26) ret = '' if n % 5 == 4: ret += '9' for i in range(0, n, 5): cur = b26[i : i + 5] if len(cur) == 5: num = code26[cur[4]] + code26[cur[3]] * 26 + code26[cur[2]] * 676 + code26[cur[1]] * 17576 + code26[cur[0]] * 456976 res = "" res = code59[num % 59] + res num //= 59 res = code59[num % 59] + res num //= 59 res = code59[num % 59] + res num //= 59 res = code59[num] + res ret += res else: ret += cur return ret while i < n: if string[i : i + 13] == 'onceuponatime': i += 13 ret.append([False, '1']) dictionary = dictionary[13 :] + 'onceuponatime' continue if string[i : i + 6] == 'oneday': i += 6 ret.append([False, '2']) dictionary = dictionary[6 :] + 'oneday' continue max_j = 0 max_place = -1 for j in range(min(window_len, n - i + 1)): place = dictionary.find(string[i : i + j]) if place != -1: max_j = j max_place = place if max_j >= 5: ret.append([False, '0' + code_table[max_j] + code_table[max_place]]) dictionary = dictionary[max_j :] + string[i : i + max_j] i += max_j else: if len(ret) and ret[-1][0]: ret[-1][1] += string[i] else: ret.append([True, string[i]]) dictionary = dictionary[1 :] + string[i] i += 1 res = "" for i in ret: if i[0]: res += b26_b59(i[1]) else: res += i[1] return res def decompress(string): def b59_b26(b59): code26 = 'abcdefghijklmnopqrstuvwxyz' code59 = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25,'A':26,'B':27,'C':28,'D':29,'E':30,'F':31,'G':32,'H':33,'I':34,'J':35,'K':36,'L':37,'M':38,'N':39,'O':40,'P':41,'Q':42,'R':43,'S':44,'T':45,'U':46,'V':47,'W':48,'X':49,'Y':50,'Z':51,'3':52,'4':53,'5':54,'6':55,'7':56,'8':57,'9':58} n = len(b59) ret = '' for i in range(0, n, 4): cur = b59[i : i + 4] num = code59[cur[3]] + code59[cur[2]] * 59 + code59[cur[1]] * 3481 + code59[cur[0]] * 205379 res = "" res = code26[num % 26] + res num //= 26 res = code26[num % 26] + res num //= 26 res = code26[num % 26] + res num //= 26 res = code26[num % 26] + res num //= 26 res = code26[num] + res ret += res return ret i = 0 n = len(string) dictionary = "#" * 62 res = "" decode_table = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25,'A':26,'B':27,'C':28,'D':29,'E':30,'F':31,'G':32,'H':33,'I':34,'J':35,'K':36,'L':37,'M':38,'N':39,'O':40,'P':41,'Q':42,'R':43,'S':44,'T':45,'U':46,'V':47,'W':48,'X':49,'Y':50,'Z':51,'0':52,'1':53,'2':54,'3':55,'4':56,'5':57,'6':58,'7':59,'8':60,'9':61} while i < n: if string[i] == '0': max_j = decode_table[string[i + 1]] max_place = decode_table[string[i + 2]] seg = dictionary[max_place : max_place + max_j] res += seg dictionary = dictionary[max_j :] + seg i += 3 elif string[i] == '1': seg = 'onceuponatime' res += seg dictionary = dictionary[13 :] + seg i += 1 elif string[i] == '2': seg = 'oneday' res += seg dictionary = dictionary[6 :] + seg i += 1 else: if string[i] == '9': current = '' j = i + 1 while j < n and string[j] != '0' and string[j] != '1' and string[j] != '2': current += string[j] j += 1 seg = b59_b26(current[: -4]) + current[-4 :] res += seg if len(seg) <= 62: dictionary = dictionary[len(seg) :] + seg else: dictionary = seg[-62 :] i = j else: current = '' j = i while j < n and string[j] != '0' and string[j] != '1' and string[j] != '2': current += string[j] j += 1 remainder = len(current) % 4 if remainder: seg = b59_b26(current[: -remainder]) + current[-remainder :] else: seg = b59_b26(current) res += seg if len(seg) <= 62: dictionary = dictionary[len(seg) :] + seg else: dictionary = seg[-62 :] i = j return res typ = input() if typ == 'COMPRESS': print(compress(input())) else: print(decompress(input()))Bash #!/bin/bash for i in {0..9} do echo DECOMPRESS > tmp1 ./p5 < $i.in >> tmp1 echo COMPRESS > tmp2 ./p5 < tmp1 >> tmp2 cmp -s $i.in tmp2 if [ $? -eq 0 ]; then inSize=$(($(wc -c tmp2 | awk '{print $1}') - 11)) outSize=$(($(wc -c tmp1 | awk '{print $1}') - 13)) if (( $outSize * 4 <= $inSize * 3 )); then echo 100 $(awk 'BEGIN{printf "%.2f\n",'$outSize'/'$inSize'*100}')% elif (( $outSize * 5 <= $inSize * 4 )); then echo 50 $(awk 'BEGIN{printf "%.2f\n",'$outSize'/'$inSize'*100}')% else echo 0 $(awk 'BEGIN{printf "%.2f\n",'$outSize'/'$inSize'*100}')% fi else echo 0 ERROR DECOMPRESS fi done rm tmp1 rm tmp2代码 50 78.61% 50 78.73% 50 78.39% 50 78.51% 50 78.54% 50 78.78% 50 78.28% 50 78.55% 50 78.34% 50 78.35%码风丑陋且不 Pythonic,懒得写 C++,请见谅
Update 2025/8/30: 强烈谴责官方 solutions 误人子弟,目前尝试唯一能达到 75% 且能在 1h 内写完的方法是 Huffman + 进制。
C++ #include <iostream> #include <cmath> #include <string> #include <map> #include <algorithm> #include <bitset> using namespace std; const map<char, string> dic = { {'e', "001"}, {'t', "101"}, {'a', "0111"}, {'o', "0101"}, {'n', "0000"}, {'i', "0001"}, {'s', "1110"}, {'r', "1111"}, {'h', "1101"}, {'d', "1001"}, {'l', "01001"}, {'u', "11000"}, {'c', "11001"}, {'m', "10001"}, {'w', "011010"}, {'f', "011011"}, {'g', "011000"}, {'p', "011001"}, {'y', "010001"}, {'b', "100000"}, {'v', "100001"}, {'k', "0100001"}, {'x', "01000001"}, {'j', "010000001"}, {'z', "0100000000"}, {'q', "0100000001"} }; string compress(const string& s) { string huff = ""; for (char char_i : s) { huff += dic.at(char_i); } int n = huff.size(); int diff1 = n % 17; int len1 = ceil(diff1 / 5.0); int diff2 = diff1 % 5; string compressed = ""; const string code_table = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for (int i = 0; i < n; i += 17) { string current = huff.substr(i, 17); if (current.size() == 17) { int summ = 0; for (int j = 16; j >= 0; --j) { summ *= 2; summ += current[j] - '0'; } compressed += code_table[summ % 62]; summ /= 62; compressed += code_table[summ % 62]; summ /= 62; compressed += code_table[summ]; } else { int m = current.size(); for (int j = 0; j < m; j += 5) { string cur = current.substr(j, 5); cur.append(5 - cur.size(), '0'); int summ = 0; for (int j = 4; j >= 0; --j) { summ *= 2; summ += cur[j] - '0'; } compressed += code_table[summ]; } } } compressed += code_table[len1 * 5 + diff2]; return compressed; } string decompress(const string& s) { map<char, int> decode_table; for (int i = 0; i < 62; ++i) { if (i < 26) decode_table['a' + i] = i; else if (i < 52) decode_table['A' + (i - 26)] = i; else decode_table['0' + (i - 52)] = i; } int l_d = decode_table[s.back()]; int l = l_d / 5; int d = l_d % 5; int n = s.size(); string huffman = ""; for (int i = 0; i < n - 1 - l; i += 3) { int summ = decode_table[s[i]] + decode_table[s[i + 1]] * 62 + decode_table[s[i + 2]] * 3844; for (int j = 0; j < 17; ++j) { huffman += (summ % 2) + '0'; summ /= 2; } } for (int i = n - 1 - l; i < n - 1; i++) { int summ = decode_table[s[i]]; if (i == n - 2 && d) { for (int j = 0; j < d; ++j) { huffman += (summ % 2) + '0'; summ /= 2; } } else { for (int j = 0; j < 5; ++j) { huffman += (summ % 2) + '0'; summ /= 2; } } } string decompressed = ""; int i = 0; while (i < huffman.size()) { for (const auto& [k, v] : dic) { if (huffman.substr(i, v.size()) == v) { decompressed += k; i += v.size(); break; } } } return decompressed; } int main() { string command; cin >> command; if (command == "COMPRESS") { string input; cin >> input; cout << compress(input) << endl; } else if (command == "DECOMPRESS") { string input; cin >> input; cout << decompress(input) << endl; } return 0; }评测结果:
代码 100 74.35% 100 74.41% 100 74.21% 100 74.31% 100 74.75% 100 74.45% 100 74.20% 100 74.35% 100 74.39% 100 74.20%至此此场比赛赛后 AK!!!
另:Huffman 的寻找方式:
Python dic = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0} for i in range(10): with open(f'{i}.in', 'r') as f: for char in f.readlines()[1][:-1]: dic[char] += 1 print(dic)结果:
代码 {'e': 41547, 't': 30204, 'a': 29230, 'o': 24370, 'n': 23268, 'i': 22476, 's': 20339, 'r': 19284, 'h': 17799, 'd': 15041, 'l': 13123, 'u': 9150, 'c': 9121, 'm': 8184, 'w': 7331, 'f': 6976, 'g': 6972, 'p': 6540, 'y': 6323, 'b': 4577, 'v': 3387, 'k': 2986, 'x': 709, 'j': 630, 'z': 323, 'q': 258}
P6 电路布线
题面
电路布局布线 (Circuit Layout and Routing) 是电子设计自动化 (EDA) 领域的一个重要概念,它涉及到在电路板或集成电路上安排和连接电子元件的过程。这个过程的目标是在满足电气性能、信号完整性、电磁兼容性等要求的同时,实现对空间、成本和生产工艺的优化。
小小现在需要解决一个简化的电路布线问题,在一个 的方格中进行电路布线。其中:
- 井号
#标记的格子已经被占用,不能布线。 - 加号
+标记的格子会连接到电路的其他部分,必须被布线。在给定的电路布线问题中,至少有一个格子必须被布线。 - 点号
.标记的格子小小有权选择是否布线:布线即将该格标记为加号,不布线即保持为点号。
小小的任务是选择尽可能多的格子进行布线 (将 “.” 的格子标记为 “+”),满足:
- 布线电路连通。即从任意一个已布线的格子,都能通过上、下、左、右移动到相邻已布线格子的方式,到达任意另一个布线的格子。
- 布线不存在短路 (回路),即不存在某个布线的格子能通过 步的上、下、左、右移动到相邻布线格子的方式回到自身,且经过的格子各不相同。
例如,以下是一个电路布线问题,已有三个格子被标记为必须布线 (加号):
#....#
....+#
.+####
.+...#以下展示了一种合法和两种不合法的布线方案:
#+.+.# #.+..# #++..#
+++++# ..+++# .++++#
.+#### .+#### .+####
.++++# .+...# .+...#
合法 不连通 有回路输入格式
输入第一行是两个空格分隔的整数 和 ,代表布线问题格子的行数和列数。
接下来 行,每行 个字符 (#, +, . 中的一个),描述了具体的布线问题。
输入数据保证至少存在一种合法的布线方案。输入数据中至少有一个 +。
输出格式
输出 行,每行 个字符,代表最优的布线方案,其中被布线的格子尽可能多。如有多种可能的方案,输出任意一种即可。
样例输入 1
2 2
+.
..样例输出 1
+.
++样例输入 2
3 5
...+#
..###
....+样例输出 2
++++#
.+###
+++++样例输入 3
5 6
..++..
.#..#.
.#..#.
.#..#.
......样例输出 3
++++++
+#.+#+
+#+.#+
+#++#+
++.+++数据规模
- 对于 40% 的数据,满足 。
- 对于 100% 的数据,满足 。
评分标准
在你的布线方案合法 (连通且无回路) 的前提下:
- 如果你的方案是最优布线方案,即布线的格子最多,该测试点得满分。
- 否则,该测试点得一半分数。
题解
40% 的数据
直接 01 搜索即可。
代码略。
100% 的数据
meet-in-the-middle
折半搜索,我们可以将上、下两部分,分别枚举,最后连在一起。
代码略。
剪枝
- 遇到方案一定不是最优,剪枝。
- 遇到该方案是不合法的,剪枝。
#include <bits/stdc++.h>
using namespace std;
int n, m;
int sx, sy;//有一个+的坐标, 用于给定dfs2的初始数据
int cnt;//用来判联通, 布了线的格子总数
int res;//最大布线格子数
bool vis[7][7];//是否已联通
int mx[4] = {1, -1, 0, 0}, my[4] = {0, 0, 1, -1};
char ans[7][7]/*最终答案*/, t[7][7]/*当前布局*/, s[7][7]/*题中地图*/;
bool dfs2(int x, int y, int prex, int prey)//判断回路+计算联通的格子数
{
if (vis[x][y]) return 0;//有回路
cnt--;//统计有多少格子是不联通的
vis[x][y] = 1;//当前格子已联通
for (int i = 0; i < 4; i++)
{
int nx = mx[i] + x, ny = my[i] + y;//找到新格子
if ((nx == prex && ny == prey/*走回头路了*/) || nx < 1 || ny < 1 || nx > n || ny > m/*越界了*/ || t[nx][ny] != '+'/*未布线*/) continue;
if (!dfs2(nx, ny, x, y)) return 0;//递归下去不合法, 就汇报
}
return 1;
}
void dfs(int x, int y, int tot)//当前布线的格子数
{
if (y == m + 1)
{
y = 1;
x++;
}//重新计算坐标
if ((n - x) * m + m - y + 1/*后面有的格子数*/ + tot <= res) return;/*再怎么样也赶不上, 剪枝*/
memset(vis, 0, sizeof vis);
if (x == n + 1)//是目前最优解且完成布线
{
cnt = tot;
if (dfs2(sx, sy, -1, -1)/*无回路*/ && !cnt/*联通*/)
{
memcpy(ans, t, sizeof t);
res = tot;//设置答案
}
return;
}
if (s[x][y] != '#')//可以布线
{
t[x][y] = '+';
memset(vis, 0, sizeof vis);
cnt = tot;
if (dfs2(sx, sy, -1, -1)/*当前没有回路, 剪枝*/) dfs(x, y + 1, tot + 1);
t[x][y] = s[x][y];
}
if (s[x][y] != '+') dfs(x, y + 1, tot);//可以不布线
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> s[i][j];
if (s[i][j] == '+')
{
sx = i;
sy = j;
}
}
}
memcpy(t, s, sizeof s);
dfs(1, 1, 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++) cout << ans[i][j];
cout << endl;
}
return 0;
}