1. 程式人生 > >[ZOJ 3063] Draw Something Cheat

[ZOJ 3063] Draw Something Cheat

lines ret 我們 一個 char som mes memset ets

題目鏈接: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;
}

[ZOJ 3063] Draw Something Cheat