列表

详情


NC24290. [USACO 2016 Dec B]Block Game

描述

Farmer John is trying to teach his cows to read by giving them a set of N spelling boards typically used with preschoolers (1≤N≤100). Each board has a word and an image on each side. For example, one side might have the word 'cat' along with a picture of a cat, and the other side might have the word 'dog' along with a picture of a dog. When the boards are lying on the ground, N words are therefore shown. By flipping over some of the boards, a different set of N words can be exposed.
To help the cows with their spelling, Farmer John wants to fashion a number of wooden blocks, each embossed with a single letter of the alphabet. He wants to make sufficiently many blocks of each letter so that no matter which set of N words is exposed on the upward-facing boards, the cows will be able to spell all of these words using the blocks. For example, if N=3 and the words 'box', 'cat', and 'car' were facing upward, the cows would need at least one 'b' block, one 'o' block, one 'x' block, two 'c' blocks, two 'a' blocks, one 't' block, and one 'r' block.

Please help the Farmer John determine the minimum number of blocks for each letter of the alphabet that he needs to provide, so that irrespective of which face of each board is showing, the cows can spell all N visible words.

输入描述

Line 1 contains the integer N.
The next N lines each contain 2 words separated by a space, giving the two words on opposite sides of a board. Each word is a string of at most 10 lowercase letters.

输出描述

Please output 26 lines. The first output line should contain a number specifying the number of copies of 'a' blocks needed. The next line should specify the number of 'b' blocks needed, and so on.

示例1

输入:

3
fox box
dog cat
car bus

输出:

2
2
2
1
0
1
1
0
0
0
0
0
0
0
2
0
0
1
1
1
1
0
0
1
0
0

说明:

In this example, there are N=3N=3 boards, giving 23=823=8 possibilities for the set of upward-facing words:

fox dog car
fox dog bus
fox cat car
fox cat bus
box dog car
box dog bus
box cat car
box cat bus

We need enough blocks for each letter of the alphabet so that we can spell all three words, irrespective of which of these eight scenarios occurs.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java(javac 1.8) 解法, 执行用时: 52ms, 内存消耗: 12368K, 提交时间: 2020-08-05 10:05:40

import java.util.*;

public class Main {
    public static int[] getNumbers(int n,String[][] words){
        int[] ans = new int[26];

        for(int i=0;i<n;i++){
            int[] a1 = new int[26];
            int[] a2 = new int[26];

            for(char c:words[i][0].toCharArray()){
                a1[c-'a']++;
            }
            for(char c:words[i][1].toCharArray()){
                a2[c-'a']++;
            }
            for(int j=0;j<26;j++){
                ans[j] += Math.max(a1[j],a2[j]);
            }
        }

        return ans;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        String[][] words = new String[n][2];

        for (int i = 0; i < n; i++) {
            words[i][0] = sc.next();
            words[i][1] = sc.next();
        }

        int[] ans = getNumbers(n,words);
        for(int i=0;i<26;i++) System.out.println(ans[i]);

        sc.close();
    }
}

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 404K, 提交时间: 2020-08-06 13:36:01

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=4e5+7;
const int INF=0x3f3f3f3f;
const int T=1e4+1;
map<char,int>op;
map<char,int>M;
map<char,int>N;
int  main()
{
  int n;
  cin>>n;
  for (int i=1;i<=2*n;++i)
  {
      string s;
      cin>>s;
      for(int j=0;j<s.size();++j)
      {
          if (s[j]>='a'&&s[j]<='z')
          {
              if (i%2==1)++M[s[j]];
              if (i%2==0)++N[s[j]];
          }
      }
      if (i%2==0)for (char i='a';i<='z';++i)op[i]+=max(M[i],N[i]);
      if(i%2==0){M.clear();N.clear();}
  }
  for (int i=0;i<26;++i)
  {
      cout<<op[char(i+'a')]<<endl;
  }
  return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 504K, 提交时间: 2020-08-05 14:58:58

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int N=50005;
int cnt[30];
int main(){
	std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);
	int n,a[30];
	cin>>n;
	string str;
	for(int i=0;i<n;i++){
		memset(a,0,sizeof(a));
		cin>>str;
		for(auto j:str){
			a[j-'a']++;
			cnt[j-'a']++;
		}
		str.clear();
		cin>>str;
		for(auto j:str){
			if(a[j-'a']) a[j-'a']--;
			else cnt[j-'a']++;
		}
	}
	for(int i=0;i<26;i++){
		cout<<cnt[i]<<endl;
	}
	return 0;
}

上一题