codeforces 518B. Tanya and Postcard 解题报告

时间:2023-03-08 22:21:10
codeforces  518B. Tanya and Postcard  解题报告

题目链接: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".

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

 

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 2e5 + ;
const int maxl = ;
char s[maxn], t[maxn];
int cs[maxl], ct[maxl]; inline int get_id(char ch)
{
if (ch >= 'a' && ch <= 'z')
return ch - 'a';
return ch-'A' + ; // 大写字母从下标26开始
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%s%s", s, t) != EOF) {
memset(cs, , sizeof(cs));
int ls = strlen(s), lt = strlen(t);
for (int i = ; i < ls; i++)
cs[get_id(s[i])]++;
memset(ct, , sizeof(ct));
for (int i = ; i < lt; i++)
ct[get_id(t[i])]++; int ans_YAY = ;
for (int i = ; i < maxl; i++) {
int minuend = min(cs[i], ct[i]);
cs[i] -= minuend, ct[i] -= minuend;
ans_YAY += minuend;
}
int ans_WHOOPS = ;
for (int i = ; i < ; i++) {
int added = min(cs[i], ct[i+]) + min(cs[i+], ct[i]);
ans_WHOOPS += added;
}
printf("%d %d\n", ans_YAY, ans_WHOOPS);
}
return ;
}