QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100 Interactive

#194. 密码

Statistics

我又忘记密码了!我正坐在电脑前输入错误的密码。我只记得我的密码仅包含小写字母。幸运的是,登录系统给出的反馈不仅仅是“密码错误”。它还会告诉我,我的输入中作为密码的(不一定连续的)子序列出现的最长前缀的长度。形式化地,对于密码 $P=p_1 p_2 \dots p_N$ 和输入 $Q=q_1 q_2 \dots q_N$,登录系统的回答是最大的 $L$,使得存在下标 $1 \le k_1 < k_2 < \dots < k_L \le N$,满足对于所有 $1 \le i \le L$ 都有 $q_i = p_{k_i}$。系统还会告诉我 $N$(密码的长度)以及 $S$(意味着我的密码仅使用字母表中的前 $S$ 个字母)。例如,$S=4$ 意味着我的密码仅包含 $a, b, c$ 和 $d$(但不一定全部包含)。

请帮我找回我的密码!

实现细节

本题为交互题。你需要实现以下函数:

C: char* guess(int n, int s); C++: string guess(int n, int s);

  • 参数:如上所述的 $N$ 和 $S$。
  • 返回值:正确的密码。

你的程序可以调用以下函数:

C: int query(char* str); C++: int query(string str);

  • 参数:一个长度为 $1$ 到 $N$ 的字符串,由字母表中的前 $S$ 个字母组成。
  • 返回值:一个介于 $0$ 和 str 长度之间的整数,表示登录系统对你查询的回答。
  • 为了避免编译错误,在调用此函数之前,你应该在全局作用域中按照上述格式声明该函数。
  • 对于每个测试用例,你最多可以调用此函数 $50,000$ 次。

你的程序可以定义其他辅助函数。

样例评测程序

我们提供了一个 grader.cgrader.cpp 供你在本地测试代码。样例评测程序会从文件 password.in 中读取输入,格式如下:

  • 第 1 行:$N$ $S$
  • 第 2 行:密码

你可以将评测程序与你的代码一起编译,然后运行生成的二进制文件,以测试你的猜测策略对给定输入的表现。

子任务

测试用例按组计分。各子任务如下:

子任务 分值 输入限制
1 10% $N \le S \le 26$;密码中的所有字母均不相同
2 20% $2 \le N \le 100$ 且 $2 \le S \le 4$
3 20% $2 \le N \le 2,000$ 且 $2 \le S \le 20$
4 30% $2 \le N \le 3,500$
5 20% $2 \le N \le 5,000$

样例

输入格式 1

guess(3, 2)

输出格式 1

query("ab") -> 2
query("abb") -> 2
query("bab") -> 1
query("aab") -> 3

说明

假设密码为 aab。评测程序调用 guess(3, 2)。此时,guess(3, 2) 应返回 "aab"

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.