codeforces 518B. Tanya and Postcard 解题报告

本文解析了CodeForces平台上的518B题,详细介绍了如何通过对比两个字符串s和t来计算YAY!和WHOOPS的出现次数。提供了完整的C++代码实现,帮助读者理解题意及算法思路。

题目链接:http://codeforces.com/problemset/problem/518/B

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

  举个例子就很容易明白了。例如 s 和 t 分别为:

ncMeXssLHS
uwyeMcaFatpInZVdEYpwJQSnVxLK

 字符s、n、c、M、e、L、S 两者都有,于是 ans_YAY = 6;XssH中 X 在字符串 t 中 有小写字母x,其他的ssH 在字符串 t 中根本就没有!

   其实...........做virtual 的时候没有看懂题目 = =!是直接看测试数据看出来的。

    If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS".

  这句话不仅内有乾坤,还意味深长呢~~~~~看懂题目是很重要滴!

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 2e5 + 5;
 9 const int maxl = 52;
10 char s[maxn], t[maxn];
11 int cs[maxl], ct[maxl];
12 
13 inline int get_id(char ch)
14 {
15     if (ch >= 'a' && ch <= 'z')
16         return ch - 'a';
17     return ch-'A' + 26;   // 大写字母从下标26开始
18 }
19 int main()
20 {
21     #ifndef ONLINE_JUDGE
22         freopen("in.txt", "r", stdin);
23     #endif // ONLINE_JUDGE
24 
25     while (scanf("%s%s", s, t) != EOF) {
26         memset(cs, 0, sizeof(cs));
27         int ls = strlen(s), lt = strlen(t);
28         for (int i = 0; i < ls; i++)
29            cs[get_id(s[i])]++;
30         memset(ct, 0, sizeof(ct));
31         for (int i = 0; i < lt; i++)
32            ct[get_id(t[i])]++;
33 
34         int ans_YAY = 0;
35         for (int i = 0; i < maxl; i++) {
36             int minuend = min(cs[i], ct[i]);
37             cs[i] -= minuend, ct[i] -= minuend;
38             ans_YAY += minuend;
39         }
40         int ans_WHOOPS = 0;
41         for (int i = 0; i < 26; i++) {
42             int added = min(cs[i], ct[i+26]) + min(cs[i+26], ct[i]);
43             ans_WHOOPS += added;
44         }
45         printf("%d %d\n", ans_YAY, ans_WHOOPS);
46     }
47     return 0;
48 }

 

   

转载于:https://www.cnblogs.com/windysai/p/4301048.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值