QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#697826 | #9430. Left Shifting 2 | He_ng | WA | 0ms | 3640kb | C++20 | 1.4kb | 2024-11-01 15:56:27 | 2024-11-01 15:56:28 |
Judging History
answer
#include <bits/stdc++.h>
const int P = 998244353;
typedef long long ll;
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
using namespace std;
void solve() {
string s;
cin >> s;
int n = s.size();
int cnt1 = 0;
string x;
// 寻找第一个满足 cnt >= 2 且 cnt 为偶数的字符段
rep(i, 0, n - 1) {
int pos = i;
while (pos < n && s[pos] == s[i]) pos++;
int cnt = pos - i;
if (cnt % 2 == 0 && cnt >= 2) {
cnt1 = i; // 标记位置
break;
}
i = pos - 1; // 跳过已经检查的区段
}
// 如果找到满足条件的 cnt1,则构造新字符串 x,否则直接使用原字符串 s
if (cnt1 > 0) {
x = s.substr(cnt1) + s.substr(0, cnt1);
} else {
x = s;
}
int ans = 0;
// 计算最终答案,统计新字符串 x 中所有 cnt >= 2 的字符段
rep(i, 0, n - 1) {
int pos = i;
while (pos < n && x[pos] == x[i]) pos++;
int cnt = pos - i;
ans += cnt / 2; // 每个偶数个数的字符段对答案贡献 cnt / 2
i = pos - 1; // 跳过当前区段
}
cout << ans << endl;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
/*
3
abccbbbbd
abcde
x
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3640kb
input:
3 abccbbbbd abcde x
output:
3 0 0
result:
wrong answer 1st lines differ - expected: '2', found: '3'