HDU 1936 Emoticons :-) (贪心)

题目

http://acm.hdu.edu.cn/showproblem.php?pid=1936

题意

给n个表情,m个字符串(带空格),求最少需要破坏多少个字符(替换成空格),使得字符串中不存在表情

解法

对于每个字符串,搜索出每个表情出现的位置,记录成一个个区间,于是就转换成,区间选点问题,选择最少的点覆盖全部区间。
对所有区间先按右端点从小到大排序,右端点相同时按左端点从大到小排序,第一次选择第一个区间的右端点,对于后面的区间,若\(p[i].x > temp\), 则\(ans+1,temp = p[i].y\)

代码

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int N = 110;
struct Node {
    int x, y;
} p[N];
int cmp(Node a, Node b) {
    if(a.y == b.y)
        return a.x > b.x;
    return a.y < b.y;
}
int main() {
    string E[N], t;
    int n, m;
    while(cin >> n >> m && n) {
        for(int i = 0; i < n; i++)
            cin >> E[i];
        getchar();
        int ans = 0;
        while(m--) {
            getline(cin, t);
            int lent = t.size();
            int cnt = 0;
            for(int i = 0; i < n; i++) {
                int lene = E[i].size();
                int pos = 0;
                while(pos < lent && t.find(E[i], pos) != t.npos) {
                    p[cnt].x = t.find(E[i], pos);
                    p[cnt].y = p[cnt].x + lene - 1;
                    pos = p[cnt].x + 1;
                    cnt ++;
                }
            }
            if(cnt == 0)
                continue;
            sort(p, p + cnt, cmp);
            ans ++;
            int temp = p[0].y;
            for(int i = 0; i < cnt; i++) {
                if(p[i].x > temp) {
                    ans ++;
                    temp = p[i].y;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Source

ACM South American Programming Contest 2007

转载于:https://www.cnblogs.com/acm_record/p/4752288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值