2019牛客暑期多校第六场题解ABDJ

2019牛客暑期多校第六场题解ABDJ

A.Garbage Classification

传送门

题意:给你两个串,第一个串s由小写字母组成,第二个串t由dwh组成,长度为26,分别表示字母a到z代表的字符。现在要你判断:

  • 如果字符串中‘h’的数量至少占s串长度的25%,输出  “Harmful”
  • 如果字符串中‘h’的数量最多占s串长度的10%,输出  “Recyclable”
  • 否则,如果字符串中‘d’的数量至少是‘w’的两倍,输出 “Dry”
  • 否则输出 “Wet”

题解:判断即可。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e3 + 10;
char s[N],t[30];
int main() {
    int T, cnt = 1;
    for (scanf("%d",&T);T--;) {
        scanf("%s%s",s,t);
        int len = strlen(s);
        printf("Case #%d: ", cnt++);
        int num[30] = {0},w = 0, h = 0, d = 0;
        for (int i = 0; i < len; i++) num[s[i]-'a']++;
        for (int i = 0; i < 26; i++) {
            if (t[i] == 'w') w+=num[i];
            if (t[i] == 'h') h+=num[i];
            if (t[i] == 'd') d+=num[i];
        } 
        if (h*4 >= len) printf("Harmful
");
        else if (h*10 <= len) printf("Recyclable
");
        else if (d >= 2 * w) printf("Dry
");
        else printf("Wet
");
    }
    return 0;
}
View Code

B.Shorten IPv6 Address

传送门

题意:给你一个128长度的二进制串,要转化成IPv6地址的形式,例如 “0:0:123:4567:89ab:0:0:0”(忽略前导0),若有连续两个及以上的0,那么可以把那一段写成“::”,但是注意一个地址中最多有1个“::”。要求输出最短的形式,如果长度相同输出字典序最小的结果。

题解:显然我们将最长连续0的那一段转化为“::”比转化把它短的要优,当有长度相同的连续0时,显然转化中间的比转化两边的优(转化中间的比转化两边的长度要少1),相同且都在首尾或者且都在中间时,显然选转化后面那个更优(‘0’字典序比‘:’小)。

       可以将二进制转化为十六进制比较也可以转化为十进制比较,因为C++可以直接%x输出十六进制数所以用十进制比较方便。

       想法是对的但是写丑了WA了一下午嘤嘤嘤(╥╯^╰╥)

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define maxn 2
const int N = 1e5 + 10;
char s[200];
int cel(int id) {
    int x = 0;
    for (int i = id; i < id + 16; i++) x = x * 2 + s[i]-'0';
    return x;
}
int main() {
    int T,t=1;
    for (scanf("%d",&T);T--;) {
        scanf("%s",s);
        int pos = -1,num = 1;
        int a[10] = {0};
        for (int i = 0; i < 128; i+=16) 
            a[i/16] = cel(i); 
        for (int i = 7; i >= 0; i--) {
            int cnt = 0;
            if (!a[i]) {
                while(i>=0 && !a[i]) i--,cnt++;
                i++;
                if (cnt == num && pos+num == 8 && i) pos = i,num = cnt;
                if (cnt > num) pos = i,num = cnt;
            }
        }
        printf("Case #%d: ",t++);
        for (int i = 0; i < 8; i++) {
            if ( i == pos) {
                if (pos == 0) printf(":");
                printf(":");
                i+=num-1;
            }else {
                printf("%x",a[i]);
                if (i!=7) printf(":");
            }
        }
        printf("
");
    }
    return 0;
}
十进制
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define maxn 2
const int N = 1e5 + 10;
char s[200],ans[10][10];
bool _is0[10];
char cel(int id) {
    int x = 0;
    for (int i = id; i < id + 4; i++) x = x * 2 + s[i]-'0';
    if (x < 10) return x+'0';
    return x-10+'a';
}
bool judge0(int x){
    int i;
    for (i = 0; ans[x][i+1]; i++) {
        if (ans[x][i] != '0') return false;
        for(int j = i; ans[x][j]; j++) ans[x][j] = ans[x][j+1];
            i--;
    }
    return ans[x][i]=='0';
}
int main() {
    int T,t=1;
    for (scanf("%d",&T);T--;) {
        scanf("%s",s);
        int pos = -1;
        int num = 1;
        for (int i = 0, j = 0,k = 0; i < 128; i+=4) {
            ans[k][j++] = cel(i);
            if(j == 4) ans[k][j] = '

相关推荐