题目链接:https://codeforces.com/contest/1379/problem/A

题意

给出一个由 '?' 和小写字母组成的字符串,可以将 '?' 替换为小写字母,判断是否存在一种替换方案使得字符串只包含一个 'abacaba' 子串。

题解

首先计算原字符串中所求子串的个数:

  • 如果个数多于一,则不存在满足要求的替换方案
  • 如果个数等于一,将所有 '?' 替换为 'abc‘ 外的字母即可
  • 如果个数少于一,判断是否有可以替换的子串,以及替换后是否只有一个子串,如 'abaca?acaba' 替换后有两个子串

代码

#include <bits/stdc++.h>
using namespace std;
const string str = "abacaba"; bool can_replace(string s) {
for (int i = 0; i < str.size(); ++i)
if (s[i] != str[i] and s[i] != '?')
return false;
return true;
} int count_str(string s) {
int res = 0;
for (int i = 0; i + str.size() - 1 < s.size(); ++i)
if (s.substr(i, str.size()) == str)
++res;
return res;
} void solve() {
int n; string s; cin >> n >> s;
int cnt = count_str(s);
if (cnt > 1) {
cout << "No" << "\n";
} else if (cnt == 1) {
cas:;
for (char &c : s)
if (c == '?') c = 'z';
cout << "Yes" << "\n" << s << "\n";
} else {
for (int i = 0; i + str.size() - 1 < s.size(); ++i) {
string t = s.substr(i, str.size());
if (can_replace(t)) {
s.replace(i, str.size(), str);
if (count_str(s) == 1) goto cas;
s.replace(i, str.size(), t);
}
}
cout << "No" << "\n";
}
} int main() {
int t; cin >> t;
while (t--) solve();
}

Codeforces Round #657 (Div. 2) A. Acacius and String(字符串)的更多相关文章

  1. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  2. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  3. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  4. Codeforces Round #877 (Div. 2) B. - Nikita and string

    题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...

  5. Codeforces Round #657 (Div. 2) C. Choosing flowers(贪心)

    题目链接:https://codeforces.com/contest/1379/problem/C 题意 有 $m$ 种花,每种花数量无限,第一次购买一种花收益为 $a_i$,之后每一次购买收益为 ...

  6. Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(数论)

    题目链接:https://codeforces.com/contest/1379/problem/B 题意 给出三个正整数 $l,r,m$,判断在区间 $[l,r]$ 内是否有 $a,b,c$ 满足存 ...

  7. Codeforces Round #354 (Div. 2) C. Vasya and String

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  8. Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题

    B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...

  9. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题

    A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

随机推荐

  1. version can neither be null, empty nor blank

    在用mybatis-generator逆向生成mapper和DAO的时候,出现了这个错误. mybatis-generator:generate 原因是在pom.xml中我的mysql依赖没有写版本号 ...

  2. Haproxy-1.8.20 编译安装:

    1 ) haproxy-1.8.20 : # 1.1 ) 安装Haproxy的依赖关系: yum install gcc gcc-c++ glibc glibc-devel pcre pcre-dev ...

  3. Go中由WaitGroup引发对内存对齐思考

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码时14.4 WaitGroup使用大家都会,但是其中是怎么实现的我们 ...

  4. 如何实现一个简易版的 Spring - 如何实现 Constructor 注入

    前言 本文是「如何实现一个简易版的 Spring」系列的第二篇,在 第一篇 介绍了如何实现一个基于 XML 的简单 Setter 注入,这篇来看看要如何去实现一个简单的 Constructor 注入功 ...

  5. Java JDBC的 url 配置信息和Mybatis核心配置文件(MySQL 的配置信息)

    JDBC 连接数据库的 url driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/smbms?uesSSL=true&u ...

  6. mount: /dev/sdxx already mounted or /xxxx busy解决方法

    异常现象: 解决方法: 1.    輸入root的密碼,進入單用戶2.    重新掛載/目錄,使其變為可讀可寫 # mount –o rw,remount / 3.    修改/etc/fstab文件 ...

  7. vue-cli3x4x修改本地端口port

    一.推荐方法 "scripts": { "serve": "vue-cli-service serve --port 3000", &quo ...

  8. 中断与系统调用深度分析(以网络编程接口SocketAPI为例)

    1.从计算机CPU与I/O设备的交互方式谈起 计算机CPU与I/O设备的交互方式有最早的程序查询(也叫轮询)方式,发展到后来的程序中断方式,DMA方式等.简单来说,最早的程序查询方式的机制是,CPU若 ...

  9. 使用Spring的RestTemplate进行接口调用

    引自:http://www.zimug.com/ 1.常见的http服务的通信方式 经常使用的方式有HttpClient.OkHttp.RestTemplate.其中RestTemplate是一种更优 ...

  10. Python+Selenium+Unittest实现PO模式web自动化框架(1)

    1.什么是PO模式? PO是Page Object的缩写 PO模式是自动化测试项目开发实践的最佳设计模式之一,讲页面定位和业务操作分开,也就是把对象的定位和测试脚本分开,从而提供可维护性. 主要有以下 ...