[ZOJ 3063] Draw Something Cheat

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4706

思路:字符串是一个集合(由0到多个A~Z字符组成),我们可以假设初始集合是多个A,多个B……多个Z组成。用unsigned char nums[26] 来标记它们,nums[i] 表示的是字母('A' + i)的个数,把它初始化为一个很大的数字,255就够了。然后对每一次给出的12个字母,我们取它和现有集合的交集,这样不断的取,就能渐渐取出这n个字符串的交集了,最后的集合自然就是本题目的答案了。

AC代码:

#include <stdio.h>
#include <string.h>

using namespace std;

int test, n;

char lines[16];
unsigned char nums[26], temp[26];

int main() {
    scanf("%d", &test);
    while(test--) {
        memset(nums, 0x7fffffff, sizeof(nums));
        scanf("%d\n", &n);
        for(int j = 0; j < n; j++) {
            gets(lines);
            memset(temp, 0, sizeof(temp));
            for(int i = 0; i < 12; i++) {
                temp[lines[i] - 'A']++;
            }
            for(int i = 0; i < 26; i++) {
                if(nums[i] > temp[i]) {
                    nums[i] = temp[i];
                }
            }
        }
        for(int i = 0; i < 26; i++) {
            while(nums[i] > 0) {
                //printf("%c",'A' + i);
                putchar('A' + i);
                --nums[i];
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/youpeng/p/10807462.html
ZOJ