1379A - Acacius and String - CodeForces Solution


brute force implementation strings *1500

Please click on ads to support us..

Python Code:

T = "abacaba"

def count(s):
    i = 0
    j = 7
    c = 0
    while i < len(s) and j >= 0:
        if s[i:j] == "abacaba":
            c += 1
            i+=1
            j+=1
        else:
            i+=1
            j+=1
    return c

def solve():
    n = int(input())
    s = input()
    F = False

    if count(s) == 1:
        F = True
        print("Yes")
        sc = s.replace('?', 'd')
        print(sc)
        return

    for i in range(n - len(T) + 1):
        sc = list(s)
        ok = True
        for j in range(len(T)):
            if sc[i + j] != '?' and sc[i + j] != T[j]:
                ok = False
                break
            sc[i + j] = T[j]

        if ok and count(''.join(sc)) == 1:
            sc = ''.join(sc)
            sc = sc.replace('?', 'd')
            F = True
            print("Yes")
            print(sc)
            return

    if not F:
        print("No")
        return

t = int(input())
for _ in range(t):
    solve()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define int long long
#define ar array<int, 2>
#define arr array<int, 3>
const int N = 2e5 + 5, M = 2 * N;
const int inf = 0x3f3f3f3f;
int mod = 998244353; //1e9+7;
int t, n, m, k;
string a = "abacaba";
signed main()
{
    ios;
#ifdef DEBUG
    freopen("../1.in", "r", stdin);
#endif
    //  核心技巧在于那个tmp的 再验证。。的手法。。
    // 就是匹配这个string  abacaba
    // 问 是否能实现当且仅当 匹配一次。。
    // 刚开始还想到dfs
    // 实际上只要从左往右 一截一截的去匹配验证就可以了
    // 1 匹配。2 验证是否能够唯一。
    cin >> t;
    while (t--)
    {
        cin >> n;
        string s;
        cin >> s;
        string ans;
        for (int i = 0; i <= n - 7; ++i)
        {
            string tmp = s;
            bool f = 1;
            for (int j = 0; j < 7; ++j)
            {
                if (tmp[i + j] != '?' && tmp[i + j] != a[j])
                {
                    f = 0;
                    break;
                }
                else
                    tmp[i + j] = a[j];
            }
            if (!f)
                continue;
            for (int i = 0; i <n; ++i)
                if (tmp[i] == '?')
                    tmp[i] = 'z';
            for (int j = 0; j <= n - 7; ++j)
                if (i != j && tmp.substr(j, 7) == a)
                {
                    f = 0;
                    break;
                }
            if (f)
            {
                ans = tmp;
                break;
            }
        }

        cout << (ans.size() ? "YES" : "NO") << endl;
        if (ans.size())
            cout << ans << endl;
    }
};


Comments

Submit
0 Comments
More Questions

952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word