每日一水第39弹(WF 2010B)

都比模拟题

map值为空的时候会产生未定义的行为,切记切记。

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=77168#problem/B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <bits/stdc++.h>
const int N = 222;
std::map<std::string, char> mp;
std::map<std::string, int> mp2;
int a[N], b[N];
char c[N];
bool equal[N][N];
int w(char ch)
{
    if(ch == '$') return 11;
    if(ch == '-') return 10;
    return ch - '0';
}
bool check_c(std::string s)
{
    int num = 0;
    int n = (int)s.length() - 2;
    for(int i = 0; i < n; i++) {
        num += ((n - i - 1) % 10 + 1) * w(s[i]) % 11;
    }
    num %= 11;
    return w(s[n]) == num;
}
bool check_k(std::string s)
{
    int num = 0;
    int n = (int)s.length() - 2;
    for(int i = 0; i <= n; i++) {
        num += ((n - (i + 1) + 1) % 9 + 1) * w(s[i]) % 11;
    }
    num %= 11;
    return w(s[n + 1]) == num;
}
std::string str;
std::string gao(char num[], int n)
{
    if(n / 6 < 3) {
        return "bad code";
    }
    int tn = n;
    if((tn + 1) % 6) {
        return "bad code";
    }
    str = "";
    std::string s = "";
    for(int i = 0; i < n; i++) {
        if((i + 1) % 6 == 0) {
            if(c[i] == '1') {
                return "bad code";
            }
            if(mp.find(s) == mp.end()) {
                return "bad code";
            }
            if(i == 5) {
                if(mp[s] != '$') {
                    return "bad code";
                }
            } else //加了这个else就WA 
                str += mp[s];
            s = "";
            continue;
        }
        s += c[i];
    }
    if(mp.find(s) == mp.end()){
        return "bad code";
    }
    if(mp[s] != '$') {
        return "bad code";
    }
    int m = (int)str.length();
    if(!check_c(str)) {
        return "bad C";
    }
    if(!check_k(str)) {
        return "bad K";
    }
    return str;
}
int main()
{
    mp["00001"] = '0';
    mp["10001"] = '1';
    mp["01001"] = '2';
    mp["11000"] = '3';
    mp["00101"] = '4';
    mp["10100"] = '5';
    mp["01100"] = '6';
    mp["00011"] = '7';
    mp["10010"] = '8';
    mp["10000"] = '9';
    mp["00100"] = '-';
    mp["00110"] = '$';
    mp2["bad code"] = 1;
    mp2["bad C"] = 3;
    mp2["bad K"] = 2;
    int n, ca = 1;
    while(scanf("%d", &n) == 1) {
        if(n == 0) {
            break;
        }
        for(int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            b[i] = -1;
        }
        memset(equal, false, sizeof(equal));
        bool flag = true;
        memset(b, 0, sizeof(b));
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) if(a[i] >= a[j]){
                double k = 1.0 * a[i] / a[j];
                if(k - 1.05 / 0.95 < 1e-6) {
                    equal[i][j] = equal[j][i] = true;
                } else if(k - (2.1 / 0.95) < 1e-6 && k - 1.9 / 1.05 > -1e-6){
                    b[i] = 1;
                    b[j] = 0;
                } else {
                    flag = false;
                }
            }
        }
        if(!flag) {
            printf("Case %d: bad code\n", ca++);
            continue;
        }
        for(int i = 0; i < n; i++) if(b[i] != -1){
            for(int j = 0; j < n; j++) if(equal[i][j]) {
                b[j] = b[i];
            }
        }
        for(int i = 0; i < n; i++) {
            c[i] = b[i] + '0';
        }
        printf("Case %d: ", ca++);
        std::string ret = gao(c, n);
        if(mp2.find(ret) == mp2.end()) {
            for(int i = 1; i < (int)ret.length() - 2; i++)
                printf("%c", ret[i]);
            puts("");
        }
        else {
            std::reverse(c, c + n);
            std::string ret2 = gao(c, n);
            if(mp2.find(ret2) == mp2.end()) {
                for(int i = 1; i < (int)ret2.length() - 2; i++)
                    printf("%c", ret2[i]);
                puts("");
                continue;
            }
 
            if(mp2[ret] >mp2[ret2]) {
                printf("%s\n", ret.c_str());
            } else {
                printf("%s\n", ret2.c_str());
            }
        }
    }
    return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注