NC24290. [USACO 2016 Dec B]Block Game
描述
输入描述
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: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; }