A题:
假如输入字符串长度不够11的话直接NO,超过11的话从头开始找找到剩下11个字符假如有8的话就YES,否则NO
#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int end; cin >> end;
char s[108];
cin >> s;
int ok = 0;
if (end < 11) {
cout << "NO" << endl;
continue;
}
else {
for (int i = 0; i <= end - 11; i++) {
if (s[i] == '8') {
cout << "YES" << endl;
ok = 1;
break;
}
}
}
if (ok == 0) {
cout << "NO" << endl;
}
}
return 0;
}
B题:
emmm这道题我写的有点蠢了,不过没关系能a就好,主要是看那6个数乘积好像都不一样,知道任意两个乘积就知道那两个数,那么可以先询问1,2 和 2,3,看那四个哪个一样就知道前三个的顺序了,以此类推即可做出来
#include<bits/stdc++.h>
using namespace std;
map<int, int> M;
int a[6] = { 4,8,15,16,23,42 };
int res[10];
int main() {
M[4] = M[8] = M[15] = M[16] = M[23] = M[42] = 1;
int flag = 1, index = 0;
int old1, old2, new1, new2;
int inp;
cout << "? " << 1 << " " << 2 << endl;
fflush(stdout);
cin >> inp;
for (int i = 0,ok=0; i < 6&& ok==0; i++) {
if (M[a[i]] == 1 && inp%a[i] == 0) {
for (int j = i + 1; j < 6; j++) {
if (M[a[j]] == 1 && a[i] * a[j] == inp) {
old1 = a[i];
old2 = a[j];
ok = 1;
break;
}
}
}
}
cout << "? " << 2 << " " << 3 << endl;
fflush(stdout);
cin >> inp;
for (int i = 0, ok = 0; i < 6 && ok == 0; i++) {
if (M[a[i]] == 1 && inp%a[i] == 0) {
for (int j = i + 1; j < 6; j++) {
if (M[a[j]] == 1 && a[i] * a[j] == inp) {
new1 = a[i];
new2 = a[j];
ok = 1;
break;
}
}
}
}
if (old1 == new1) {
res[2] = old1;
res[1] = old2;
res[3] = new2;
}
else if(old1==new2){
res[1] = old2;
res[2] = old1;
res[3] = new1;
}
else if (old2 == new1) {
res[1] = old1;
res[2] = old2;
res[3] = new2;
}
else {
res[1] = old1;
res[2] = old2;
res[3] = new1;
}
cout << "? " << 3 << " " << 4 << endl;
fflush(stdout);
cin >> inp;
for (int i = 0, ok = 0; i < 6 && ok == 0; i++) {
if (M[a[i]] == 1 && inp%a[i] == 0) {
for (int j = i + 1; j < 6; j++) {
if (M[a[j]] == 1 && a[i] * a[j] == inp) {
new1 = a[i];
new2 = a[j];
ok = 1;
break;
}
}
}
}
if (new1 == res[3]) res[4] = new2;
else res[4] = new1;
cout << "? " << 4 << " " << 5 << endl;
fflush(stdout);
cin >> inp;
for (int i = 0, ok = 0; i < 6 && ok == 0; i++) {
if (M[a[i]] == 1 && inp%a[i] == 0) {
for (int j = i + 1; j < 6; j++) {
if (M[a[j]] == 1 && a[i] * a[j] == inp) {
new1 = a[i];
new2 = a[j];
ok = 1;
break;
}
}
}
}
if (new1 == res[4]) res[5] = new2;
else res[5] = new1;
for (int i = 1; i <= 5; i++) M[res[i]] = 0;
for (int i = 0; i < 6; i++) {
if (M[a[i]] == 1) {
res[6] = a[i];
}
}
cout << "!";
for (int i = 1; i <= 6; i++) {
cout << " " << res[i];
}
// system("pause");
return 0;
}
C题:
一道并查集板子题
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+7;
int fat[maxn],rk[maxn];
int trace(int man){
return fat[man]==man?man:fat[man]=trace(fat[man]);
}
void combine(int leader,int man){
int trace_leader=trace(leader),trace_man=trace(man);
if(trace_leader!=trace_man){
fat[trace_man]=trace_leader;
rk[trace_leader]+=rk[trace_man];
rk[trace_man]=0;
return;
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) fat[i]=i,rk[i]=1;
while(m--){
int cnt; cin>>cnt;
int inp1,inp2;
if(cnt==0){
continue;
}
scanf("%d",&inp1);
cnt--;
while(cnt--){
scanf("%d",&inp2);
combine(inp1,inp2);
inp1=inp2;
}
}
for(int i=1;i<=n;i++){
trace(i);
}
printf("%d",rk[fat[1]]);
for(int i=2;i<=n;i++){
printf(" %d",rk[fat[i]]);
}
return 0;
}
D题:
好像也是一道贪心题,就是首先找最深的层数,然后除2,假如是奇数的话再+1,得出来的就是红色或蓝色的最深层数,然后贪心的填数字就可以了。
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
char s[maxn];
int main(){
int n; cin>>n;
char qqq; scanf("%c",&qqq);
int max=0,dep=0;
for(int i=0;i<n;i++){
scanf("%c",s+i);
if(s[i]=='('){
dep++;
max=max<dep?dep:max;
}
else dep--;
}
int flag;
if(max%2==0){
flag=max/2;
}
else{
flag=max/2;
flag++;
}
dep=0;
for(int i=0;i<n;i++){
if(s[i]=='('){
if(dep<flag){
dep++;
printf("1");
}
else{
printf("0");
}
}
else{
if(dep>0){
dep--;
printf("1");
}
else printf("0");
}
}
return 0;
}