1002 Binary Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 276 Accepted Submission(s): 59
题解:
(一)当 s=1,k为偶数时 ,输出0;k为奇数时,输出1;
(二)当 s全为1,k=1时,末尾字符变成0输出即可;
(三) 记录全为0的块数 ct;
当ct<=k 全1
当ct
因为10序列有特殊性变为全一可有以下情况:
一次:10——>11
两次:10——>01——>11
#include
#include
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
ll n, k;
cin >> n >> k;
string s;
cin >> s;
if (k == 0)
{
cout << s << "\n";
continue;
}
if (n == 1)
{
if (k % 2 == 1)
{
cout << (s[0] == '0' ? '1' : '0') << "\n";
}
else
{
cout << s[0] << "\n";
}
continue;
}
int len = s.length();
int cnt1 = 0, cnt2 = 0; // 0个数 0块数
for (int i = 0; i < len;)
{
if (s[i] == '0')
{
cnt2++;
while (s[i] == '0' && i < len)
{
cnt1++;
i++;
}
}
else
{
i++;
}
}
// cout << cnt1 << " " << cnt2 << "\n";
if (cnt2 == 0 && k == 1)
{
for (int i = 0; i < len - 1; i++)
{
cout << 1;
}
cout << 0 << "\n";
}
else
{
for (int i = 0; i < len;)
{
if (s[i] == '0')
{
k--;
while (s[i] == '0' && i < len)
{
s[i] = '1';
i++;
}
if (k == 0)
{
break;
}
}
else
{
i++;
}
}
cout << s << "\n";
}
}
return 0;
}
1004 Card Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 36
#include
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5 + 10;
const ll mod = 998244353;
ll qmi(ll m, ll k, ll p) {
ll res = 1 % p, t = m;
while (k) {
if (k & 1) res = res * t % p;
t = t * t % p;
k >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
ll n;
cin >> n;
ll ans = qmi(2, n - 1, mod) - 1;
cout << ans << '\n';
}
return 0;
}
1009 String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 43
题解:统计连续相同字符序列的个数m,用长度n减去m即可
#include
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5 + 10;
const ll mod = 212370440130137957ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
int ans = 0, n = s.size();
for (int i = 0; i < n; i++) {
int cnt = 0;
int j = i + 1;
while (s[i] == s[j]) {
cnt++;
j++;
}
ans += cnt;
i = j - 1;
}
cout << ans << '\n';
}
return 0;
}