Codeforces Round #657 (Div. 2) A. Acacius and String(暴力)
Acacius is studying strings theory. Today he came with the following problem.
You are given a string ss of length nn consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string tt of length mm in the string ss of length nn as a substring is a index ii (1≤i≤n−m+11≤i≤n−m+1 ) such that string s[i..i+m−1]s[i..i+m−1] consisting of mm consecutive symbols of ss starting from ii -th equals to string tt . For example string "ababa" has two occurrences of a string "aba" as a substring with i=1i=1 and i=3i=3 , but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
Input
First line of input contains an integer TT (1≤T≤50001≤T≤5000 ), number of test cases. TT pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer nn (7≤n≤507≤n≤50 ), length of a string ss .
The second line of a test case description contains string ss of length nn consisting of lowercase English letters and question marks.
Output
For each test case output an answer for it.
In case if there is no way to replace question marks in string ss with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of nn lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
首先要确定原串中abacaba出现的次数:出现两次及以上输出NO;出现一次的话首先把有问号的地方随便填上z之类的字母然后输出YES和新串。如果没有出现过abacaba,就枚举每个可能的位置,看看这个位置往后的7个字母,能否通过补全?变成abacaba,如果能的话,还要注意这样填是否会造成多余的abacaba的出现,比如???????bacaba以及???c???caba这样的例子,如果会的话恢复原状继续遍历。
代码写的有点挫==仅供参考。
#include <bits/stdc++.h> using namespace std; char s[66]; int n; int match(int x) { string temp = "abacaba"; int cnt1 = 0, cnt2 = 0; for(int i = x; i <= x + 6; i++) { if(s[i] == temp[i - x]) cnt1++; if(s[i] == temp[i - x] || s[i] == '?') cnt2++; } if(cnt1 == 7) return 2;//完全匹配 else if(cnt1 != 7 && cnt2 == 7) return 1;//可以通过补充?的字母来匹配 else return 0;//无法匹配 } int main() { int t; cin >> t; while(t--) { cin >> n; scanf("%s", s); bool ok = 0; int cnt = 0; for(int i = 0; i < n - 7 + 1; i++) if(match(i) == 2) cnt++; if(cnt > 1) { cout << "NO" << endl; continue; } else if(cnt == 1) { for(int i = 0; i < n; i++) if(s[i] == '?') s[i] = 'z'; cout << "YES" << endl; printf("%s\n", s); continue; } else//有可能通过补充来匹配 { for(int i = 0; i < n - 7 + 1; i++) { if(match(i) == 1) { string temp = "abacaba"; vector<char> v; for(int j = i; j <= i + 6; j++) { v.push_back(s[j]); s[j] = temp[j - i]; } bool no = 0; for(int k = max(0, i - 6); k <= min(n - 1, i + 12); k++)//检查这样填是否会造成两个以上abacaba出现 { if(match(k) == 2 && k != i)//排除原位置 还有abacaba { no = 1; break; } } if(no)//i这个位置不可行 { for(int j = i; j <= i + 6; j++) s[j] = v[j - i]; continue; } for(int i = 0; i < n; i++) if(s[i] == '?') s[i] = 'z'; ok = 1; cout << "YES" << endl; printf("%s\n", s); break; } } if(!ok) cout << "NO" << endl; } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!