codeforces 518B. Tanya and Postcard

题意:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就将 s 这个数量加到 YAY! 的答案里,少于的话就加 t 中有的数量;如果 t 中有跟 s 相同的字母但是大小写不对应(例如A-a,z-Z),就加到 WHOOPS 的答案里。

解题思路:开两个大小为52的数组cs[],ct[],分别存a~z和A~Z的字母的个数,遍历两个字符串,使cs,ct数组分别有值

      遍历两个数组,将相同下标(比如i)时两个数组中存储数据的最小值加到yay变量中,然后将cs[i],ct[i]分别减去那个最小值;、

      遍历cs数组的0~25,ct数组的26~51,将相同下标(比如i)时两个数组中存储数据的最小值加到whoop变量中

      遍历ct数组的0~25,cs数组的26~51,将相同下标(比如i)时两个数组中存储数据的最小值加到whoop变量中

代码如下:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 #define maxlen 200001
 7 #define N 52
 8 char s[maxlen],t[maxlen];
 9 int cs[N],ct[N];
10 int getid(char ch){
11     if(ch>='a'&&ch<='z')
12         return ch-'a';
13     else
14         return ch-'A'+26;
15 }
16 int main()
17 {
18     int ls,lt;
19     while(scanf("%s%s",s,t)!=EOF){
20         getchar();
21         ls=strlen(s);
22         lt=strlen(t);
23         memset(cs,0,sizeof(cs));
24         for(int i=0;i<ls;i++){
25             cs[getid(s[i])]+=1;
26         }
27         memset(ct,0,sizeof(ct));
28         for(int i=0;i<lt;i++){
29             ct[getid(t[i])]+=1;
30         }
31         int yay=0,whoop=0;
32         int p;
33         for(int i=0;i<N;i++){
34             p=min(cs[i],ct[i]);
35             cs[i]-=p;
36             ct[i]-=p;
37             yay+=p;
38         }
39         for(int i=0;i<26;i++){
40             p=min(cs[i], ct[i+26]) + min(cs[i+26], ct[i]);
41             whoop+=p;
42         }
43         printf("%d %d
",yay,whoop);
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/PJQOOO/p/4353135.html