TonyYin's Blog

Back

题意#

X 和 Y 各有一个权值 vx,vyv_x, v_y.

现在从最低位开始依次比较。

比如 vx=37v_x=37vy=28v_y=28

  • 首先比较最后一位,(7,8)(7,8),Y 暂时领先。
  • 再加上前一位,(37,28)(37,28),X 暂时领先。
  • 比较结束。

如果我们用 X 代表小 X 暂时领先,Y 代表小 Y 暂时领先,那么可以写下一个字符串 XY

再比如,vx=137v_x=137vy=47v_y=47

如果我们再用 Z 表示小 X 与小 Y 的点赞数暂时一样,那么写下的字符串应该为 XYZ

给定最后的字符串 sis_i,请构造一种可能的 (vxvy)(v_x v_y)

若不存在构造方案,输出 1-1.

为了方便输出,用前导零补足位数。

对于 100%100\% 的数据,si{X,Y,Z}s_i \in \{\texttt{X},\texttt{Y},\texttt{Z}\}1len(s)1061 \le \text{len}(s) \le 10^6

XY

37
28
plaintext
XYZ

137
047
plaintext
ZZZZZZ

000000
000000
plaintext
XYZXYZ

-1
plaintext

题解#

如果 ZZ 不是连续的后缀,则无解。

否则用 0/10/1 构造即可。

const int MAXN = 1e6 + 10;
string s;
int a[MAXN], b[MAXN];
int main() {
	cin >> s;
	int n = s.length(), flag = 0;
	for(int i = 0; i < n; i++) {
		if(flag && s[i] != 'Z') {
			cout << -1 << endl;
			return 0;
		}
		if(s[i] == 'X') a[i] = 1, b[i] = 0;
		else if(s[i] == 'Y') a[i] = 0, b[i] = 1;
		else if(s[i] == 'Z') flag = 1;
	}
	for(int i = 0; i < n; i++) cout << a[i]; cout << endl;
	for(int i = 0; i < n; i++) cout << b[i]; cout << endl;
	return 0;
}
cpp
【洛谷-P5595】-【XR-4】歌唱比赛
https://www.tonyyin.top/blog/oi-solution/p5595
Author TonyYin
Published at June 19, 2022
Comment seems to stuck. Try to refresh?✨