A. 123233
给定一个6位正整数,要求其中“1”刚好出现一次,“2”刚好出现两次,“3”刚好出现三次。满足打印“Yes”,否则打印“No”。
我用字符串做的,就是直接找“1”“2”“3”各出现了几次,感觉自己还是做复杂了,但是我又不想写取余取模。
#include <bits/stdc++.h>
using namespace std;
void solve()
{
string s; cin >> s;
int one = 0; int twi = 0; int tri = 0;
for (int i = 0; i < 6; i++)
{
if (s[i] == '1') one++;
if (s[i] == '2') twi++;
if (s[i] == '3') tri++;
}
if ( (one == 1) && (twi == 2) && (tri == 3) )
{
cout << "Yes";
return;
}
cout << "No";
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}
B. Hurdle Parsing
给定一个字符串,字符串开头结尾都是“|”,中间是几组被“|”隔开的“-”,类似这样:
|---|-|----|-|-----|求每组段横杠的数量,打印出来,数据间隔一个空格。
我还是用字符串做的,第一个跳过去,后面遇到“|”就记下这一格的位置i,减去上一次遇到“|”的位置j,再减去一去掉重复计数。然后打印。
#include <bits/stdc++.h>
using namespace std;
void solve()
{
string s; cin >> s;
int t = 0; int len = 0;
for (int i = 1; i < s.length(); i++)
{
if (s[i] == '|')
{
len = i - t - 1;
cout << len << " ";
t = i;
}
}
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}
C. Move Segment
给定一个字符串S,全是‘0’跟‘1’,其中被‘0’分割开的‘1’算一个小串,小串由一个及以上个连续的‘1’组成。再告诉你一个数字K。
现在定义一个长度相同的字符串T,现在把第K个‘1’小串移到第K-1个‘1’小串那里去。
思路是:先判断字符串以0或1开头,从而判断第k个小串前面是第几个‘0’串。然后按题意改数,k小串所在的原位置全改成0;前面按k的长度,从k-1小串结束后的第一个‘0’处全改成1。
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n = 0; string s; int k = 0; cin >> n >> k >> s;
vector<int> one; vector<int> zero;
for (int i = 0; i < n; i++)
{
if (i == 0 || s[i] != s[i-1])
{
if (s[i]=='0')
{
zero.push_back(i);//找0下标
}
else
{
one.push_back(i);//找1下标
}
}
}
int cnt = 0;
if (s[0] == '0')
{
int i;
for (i = one[k-1]/*实际上是第k个1串的开头下标*/; i < n && s[i] != '0'; i++)
{
s[i] = '0';
cnt++;
}
for (i = zero[k-1]; i < zero[k-1] + cnt; i++)
{
s[i]='1';
}
}
else
{
int i;
for (i = one[k-1]; i < n && s[i] != '0'; i++)
{
s[i] = '0';
cnt++;
}
for (i = zero[k-2]; i < zero[k-2] + cnt; i++)
{
s[i]='1';
}
}
cout<<s;
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}
笑点解析:
没读懂题,甚至不愿意好好研究范例,而且题读快了,k的意思竟然没发现
D. Strang Mirroring
给定一个字符串S,全是大小写字母。首先,将 S 中的大小写字母全部反转,得到S',然后将原S和反转后的S'合并成新的S。现有Q个查询,每个查询询问的是第k个字符是什么。
看了看题解,人家用的是二进制奇偶判断,高级,高级。
先输入字符串s和查询问题个数q,然后用s的重复次数推断大小写是否翻转,最后输出。
#include <bits/stdc++.h>
#define ull long long int
using namespace std;
char flip(char c){
if('a'<=c && c<='z'){
return (c-'a')+'A';
}
else{
return (c-'A')+'a';
}
}
void solve()
{
string s; int q = 0; long long k = 0; cin >> s >> q;
ull len = 0; len = s.size();
for (int i = 0; i < q; i++)
{
if (i != 0) cout << " ";
cin >> k; k -= 1;
long long cnt = 0; cnt = k / len; //from 0 to count, so it's right
long long o = 0; o = k % len;
if (__builtin_popcountll(cnt) % 2)//even is original, odd is opposite
cout << flip(s[o]);
else
cout << s[o];
}
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}
笑点解析:
想用递归做,但是我菜,不会处理数据。
摆了,E题以后再说⑧