本文共 1150 字,大约阅读时间需要 3 分钟。
为了解决这个问题,我们需要找到一个字符串 s,使其满足以下条件:
s 是二进制字符串。s 的长度不超过 2 * |t|。t 是 s 的一个子序列。s 的周期尽可能小。为了构造满足条件的字符串 s,我们可以分以下几步进行:
t 中所有字符都是相同的(全0或全1),则 s 就直接等于 t,周期为1。t 不是全0或全1,那么我们构造一个新的字符串 s,其中每个字符重复两次,并按原顺序排列。这样,s 的周期为2,且 t 是 s 的子序列。这种方法确保了 s 的长度不超过 2 * |t|,并且 s 的周期尽可能小。
#includeusing namespace std;string constructS(const string &t) { bool all_zero = true; bool all_one = true; for (char c : t) { if (c == '0') { all_one = false; } else { all_zero = false; } } if (all_zero || all_one) { return t; } string s; for (char c : t) { s += c; s += c; } return s;}int main() { int T; cin >> T; for (int _ = 0; _ < T; ++_) { string t; cin >> t; if (t.size() <= 2) { cout << t << endl; } else { string s = constructS(t); cout << s << endl; } }}
T 和每个测试用例的字符串 t。t 是否全0或全1。如果是,直接输出 t。s:如果 t 不是全0或全1,构造一个新的字符串 s,其中每个字符重复两次,并按原顺序排列。s。这种方法确保了 s 的周期尽可能小,同时满足所有给定的条件。
转载地址:http://xboiz.baihongyu.com/