CodeForces 518B Tanya and Postcard 计数

来源:互联网 发布:席晓辉 知乎 编辑:程序博客网 时间:2024/05/29 15:10

先来错误代码, Wrong answer test 8:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <map>using namespace std;const int maxn = 200000 + 24;char s[maxn], t[maxn], ans[maxn];map<char, int> mp;int main() {scanf("%s%s", s, t);mp.clear();int len = strlen(t);for(int i = 0; i < len; ++i) {mp[t[i]]++;}int ans1 = 0, ans2 = 0;len = strlen(s);for(int i = 0; i < len; ++i) {if(mp[s[i]] > 0) {ans1++;mp[s[i]]--;}else if(mp[s[i]] == 0) {if(s[i] >= 'a' && s[i] <= 'z' && mp[s[i]-32] > 0) { ans2++; mp[s[i]-32]--; }if(s[i] >= 'A' && s[i] <= 'Z' && mp[s[i]+32] > 0) { ans2++; mp[s[i]+32]--; }}}printf("%d %d\n", ans1, ans2);}

一直wrong answer test 8,最后终于找出了一组样例通不过:

aaaaAAAA

AAAAaaBB

答案应该是: 6 0然而以上代码运行出结果为 4 2;

所以应该先找出YAY!的值,跑两次循环就可以:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <map>using namespace std;const int maxn = 200000 + 24;char s[maxn], t[maxn], ans[maxn];map<char, int> mp;int main() {scanf("%s%s", s, t);mp.clear();int len = strlen(t);for(int i = 0; i < len; ++i) {mp[t[i]]++;}int ans1 = 0, ans2 = 0;len = strlen(s);for(int i = 0; i < len; ++i) {if(mp[s[i]] > 0) {ans1++;mp[s[i]]--;s[i] = 0;}}for(int i = 0; i < len; ++i) {if(s[i] >= 'a' && s[i] <= 'z' && mp[s[i]-32] > 0) { ans2++; mp[s[i]-32]--; }if(s[i] >= 'A' && s[i] <= 'Z' && mp[s[i]+32] > 0) { ans2++; mp[s[i]+32]--; }}printf("%d %d\n", ans1, ans2);}


0 0