首页 > 代码库 > D - Simple String CSU - 1550

D - Simple String CSU - 1550

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550

很久都没补这题,最近想学网络流,就看看,队友以前用网络流过的,Orz,

但是这题只需要简单的判断,可能想起来有点麻烦。

考虑一定要从A串取出n个,B串也一定要取出n个,那么A和C的交集一定要大于等于n,同理B。

同时为了得到C串,还需要A + B和C的交集一定要大于等于n * 2

怎么说呢。可以画个图表示下,反正找不到反例。就先码一波。

技术分享
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 123456;
char str[maxn], sub1[maxn], sub2[maxn];
int hasStr[222], hasSub1[222], hasSub2[222];
void work() {
    int lenstr = strlen(str + 1);
    memset(hasStr, 0, sizeof hasStr);
    memset(hasSub1, 0, sizeof hasSub1);
    memset(hasSub2, 0, sizeof hasSub2);
    for (int i = 1; i <= lenstr; ++i) hasStr[str[i]]++;
    for (int i = 1; i <= lenstr; ++i) hasSub1[sub1[i]]++;
    for (int i = 1; i <= lenstr; ++i) hasSub2[sub2[i]]++;
    int ans1 = 0, ans2 = 0, ans3 = 0;
    for (int i = A; i <= Z; ++i) {
        ans1 += min(hasStr[i], hasSub1[i]);
        ans2 += min(hasStr[i], hasSub2[i]);
        ans3 += min(hasStr[i], hasSub1[i] + hasSub2[i]);
    }
    if (ans1 < lenstr / 2 || ans2 < lenstr / 2 || ans3 < lenstr) {
        printf("NO\n");
    } else printf("YES\n");
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    while (scanf("%s%s%s", sub1 + 1, sub2 + 1, str + 1) > 0) work();
    return 0;
}
View Code

 

D - Simple String CSU - 1550