Codeforces Round #811 (Div. 3)D. Color with Occurrences
题目大意:给出一个文章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的更多相关文章
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Codeforces Round #485 (Div. 2) A. Infinity Gauntlet
Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...
- Codeforces Round 254 (Div. 2)
layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- Excelize 发布 2.3.0 版本, Go 语言 Excel 文档基础库
Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取.写入由 Microsoft Exc ...
- 基于SpringSecurity的@PreAuthorize实现自定义权限校验方法
一.前言 在我们一般的web系统中必不可少的就是权限的配置,也有经典的RBAC权限模型,是基于角色的权限控制.这是目前最常被开发者使用也是相对易用.通用权限模型.当然SpringSecurity已经实 ...
- Ubuntu20.04配置 ES7.17.0集群
Ubuntu20.04配置 ES7.17.0集群 ES能做什么? elasticsearch简写es,es是一个高扩展.开源的全文检索和分析引擎,它可以准实时地快速存储.搜索.分析海量的数据. Ubu ...
- centOS7.x修改root密码
方法一: 在开机的时候选中这一行(注意光标要进入虚拟机),然后按下e键 然后找到这一行(linux 16开头的)其中的ro,将其改为 rw init=sysroot/bin/sh 按ctrl+x执行 ...
- python随机值生成的常用方法
一.随机整数1.包含上下限:[a, b] import random #1.随机整数:包含上下限:[a, b] for i in range(10): print(random.randint(0,5 ...
- 项目管理构建工具——Maven(基础篇)
项目管理构建工具--Maven(基础篇) 在前面的内容中我们学习了JDBC并且接触到了jar包概念 在后面我们的实际开发中会接触到很多jar包,jar包的导入需要到互联网上进行就会导致操作繁琐 Mav ...
- dp-位移模型(数字三角形演变)
由数字三角形问题演变而来下面的题: https://www.cnblogs.com/sxq-study/p/12303589.html 一:规定位移方向 题目: Hello Kitty想摘点花生送给她 ...
- 写个续集,填坑来了!关于“Thread.sleep(0)这一行‘看似无用’的代码”里面留下的坑。
"我报名参加金石计划1期挑战--瓜分10万奖池,这是我的第2篇文章,点击查看活动详情" 你好呀,我是居家十三天只出了一次小区门的歪歪. 这篇文章是来填坑的,我以前写文章的时候也会去 ...
- 《Win10——常用快捷键》
Win10--常用快捷键 Ctrl+C:复制 Ctrl+V:粘贴 Ctrl+A:全选 Ctrl+X:剪切 Ctrl+D:删除 Ctrl+Z:撤销 Ctrl+Y:反撤销 Ctrl+Shift ...
- vue项目中使用百度富文本编辑器ueditor
第一步,安装依赖,并且把ueditor整个文件夹放入public里边 第二步,在你需要编辑的地方引入,或者main.js中全局引入 XX.vue文件中写入下面代码,创建编辑器. <vue-ued ...