题目大意:给出一个文章t和n个字符串s1,s2...sn;

  问能否用这n个字符串将整个文章覆盖;

思路:贪心(最小区间覆盖)

  记录每个字符串能够覆盖的所有位置(起点,终点,编号)

  排序后贪心的求出是否能够将所有点覆盖

 1 # include<iostream>
2 # include<bits/stdc++.h>
3 using namespace std;
4 # define int long long
5 # define endl "\n"
6 const int N = 2e5 + 10, INF = 0x3f3f3f3f3f;
7 struct node {
8 int l, r, id;//起点,终点,编号
9 };
10 string s;
11 bool cmp(node a, node b) {
12 if (a.l != b.l) return a.l < b.l;
13 return a.r < b.r;
14 }
15 vector<node> se;
16 int length;
17
18 bool check(int start, string t) {
19 int maxv = length - start + 1;
20 if (maxv < t.size()) return false;
21 for (int i = start, j = 0; i <= start + t.size() - 1; ++i, ++j) {
22 if (t[j] != s[i]) return false;
23 }
24 return true;
25 }
26
27 void solve() {
28
29 int n;
30 cin >> s >> n;
31 se.clear();
32 length = s.size();
33 s = "?" + s;
34 for (int i = 1; i <= n; ++i) {
35 string t;
36 cin >> t;
37 int len = t.size();
38 for (int j = 1; j <= length; ++j) {
39 if (check(j, t)) se.push_back({j, j + len - 1, i});//记录每个字符串能够覆盖的点
40 }
41 }
42 sort(se.begin(), se.end(), cmp);//以起始点为基准从小到大排序
43 bool ok = false;
44 int start = 1, ed = length;
45 vector<pair<int, int>> ans;
46 for (int i = 0; i < se.size(); ++i) {
47 int j = i, r = -INF;
48
49 int t = 0;
50 while (j < se.size() && se[j].l <= start) {
51 if (se[j].r > r) //寻找能够覆盖区间最长的字符串
         {
52 r = se[j].r;
53 t = j;
54 }
55 j++;
56 }
57 if (r < start) break;
58 start = r+1,i = j-1;
59 ans.push_back({se[t].id,se[t].l});
60 if(r>= ed){
61 ok = true;
62 break;
63 }
64 }
65 if(ok){
66 cout<<ans.size()<<endl;
67 for(auto it:ans)cout<<it.first<<" "<<it.second<<endl;
68 }
69 else cout<<-1<<endl;
70 }
71 int tt;
72 signed main() {
73 ios::sync_with_stdio(false);
74 cin.tie(0);
75 cout.tie(0);
76 cin >> tt;
77 while (tt--)solve();
78
79
80 return 0;
81 }

Codeforces Round #811 (Div. 3)D. Color with Occurrences的更多相关文章

  1. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  2. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  3. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  4. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet

    Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...

  5. Codeforces Round 254 (Div. 2)

    layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  6. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. Linux—进程管理

    Linux 进程管理 1.进程管理介绍 1.1 什么是进程? 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 简而言之 ...

  2. linux 的文件权限案列

    需求: 技术部门人员可以相互查看,但不能删除和修改别人的文件,其他部门人员不能查看,但领导组可以且只能查看. 设计: 技术部: 组 jishu ; 人员 js1, js2 领导:组 lingd ; 人 ...

  3. 无密码正向直连内网linux目标机复现

    无密码正向直连内网linux目标机复现 文章来自sxf大佬klion https://mp.weixin.qq.com/s/GPPvci8qKuvc5d3Q7Cer7Q 场景说明 前期通过一些 Rce ...

  4. 【Elasticsearch】ES选主流程分析

    Raft协议 Raft是分布式系统中的一种共识算法,用于在集群中选举Leader管理集群.Raft协议中有以下角色: Leader(领导者):集群中的领导者,负责管理集群. Candidate(候选者 ...

  5. MySQL到底有没有解决幻读问题?这篇文章彻底给你解答

    MySQL InnoDB引擎在Repeatable Read(可重复读)隔离级别下,到底有没有解决幻读的问题? 网上众说纷纭,有的说解决了,有的说没解决,甚至有些大v的意见都无法达成统一. 今天就深入 ...

  6. 容器化|自建 MySQL 集群迁移到 Kubernetes

    背景 如果你有自建的 MySQL 集群,并且已经感受到了云原生的春风拂面,想将数据迁移到 Kubernetes 上,那么这篇文章可以给你一些思路. 文中将自建 MySQL 集群数据,在线迁移到 Kub ...

  7. 重要参考文档---MySQL 8.0.29 使用yum方式安装,开启navicat远程连接,搭建主从,读写分离(需要使用到ProxySQL,此文不讲述这个)

    yum方式安装 echo "删除系统默认或之前可能安装的其他版本的 mysql" for i in $(rpm -qa|grep mysql);do rpm -e $i --nod ...

  8. Alertmanager篇

    报一直是整个监控系统中的重要组成部分,Prometheus监控系统中,采集与警报是分离的.警报规则在 Prometheus 定义,警报规则触发以后,才会将信息转发到给独立的组件 Alertmanage ...

  9. firewalld防火墙基本使用

    FirewallD 是 CentOS 7 服务器上默认可用的防火墙管理工具.基本上,它是 iptables 的封装,有图形配置工具 firewall-config 和命令行工具 firewall-cm ...

  10. MySql的InnoDB的三层B+树可以存储两千万左右条数据的计算逻辑

    总结/朱季谦 B+树是一种在非叶子节点存放排序好的索引而在叶子节点存放数据的数据结构,值得注意的是,在叶子节点中,存储的并非只是一行表数据,而是以页为单位存储,一个页可以包含多行表记录.非叶子节点存放 ...