Acwing6123.哞叫时间

今天做了道特别抽象的题,对于没有接触过oj,只懂背模板的我,这道题可以说是降维打击了。

#include <bits/stdc++.h>

using namespace std;

const int N = 20010;
int n;
int F;
char s[N];
int cnt[26][26];
bool st[26][26];

void update(int l ,int r,int v){
    l = max(l,0);
    r = min(r,n-1);
    for(int i = l;i+2<=r;i++){
        char a = s[i]; char b = s[i+1]; char c = s[i+2];
        if(a!= b && b == c){
            cnt[a][b] += v;
            if(cnt[a][b] >= F){
                st[a][b] = true;
            }
        }
    }
}

int main(){
    cin>>n>>F;
    scanf("%s",s);
    for(int i =0;i<n;i++) s[i] -= 'a';
    update(0,n-1,1);
    for(int i =0;i<n;i++){
        char t = s[i];
        update(i-2,i+2,-1);
        for(int j = 0;j<26;j++){
           if(j != t){
               s[i] = j;
               update(i-2,i+2,1);
               update(i-2,i+2,-1);
           }
        }
        s[i] = t;
        update(i-2,i+2,1);
    }
    
    int res = 0;
    for(int i = 0;i<26;i++){
        for(int j = 0;j<26;j++){
            if(st[i][j]) res++;        
        }
    }
    cout<<res<<endl;
    
    for(int i = 0;i<26;i++){
        for(int j = 0;j<26;j++){
            if(st[i][j]) printf("%c%c%c\n",i+'a',j+'a',j+'a');        
        }
    }
    return 0;
}

这里还有一个大哥的解法我觉得也特别有意思:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    cin.tie(0) -> sync_with_stdio(0);
    int n, f;
    cin >> n >> f;
    string s;
    cin >> s;
    vector<string> ans;
    for(char c1 = 'a'; c1 <= 'z'; c1++)
    {
        for(char c2 = 'a'; c2 <= 'z'; c2 ++) // 枚举所有询问
        {
            if(c2 == c1) continue;
            int cnt = 0, flag = 0;
            auto t = s;
            for(int i = 0; i + 2 < n; i ++) // 找原本就存在的模式串
            {
                if(t[i] == c1 and t[i + 1] == t[i + 2] and t[i + 1] == c2)
                {
                    t[i] = t[i + 1] = t[i + 2] = '#'; // 防止交叉,用占位符替代
                    cnt ++;
                }
            }
            for(int i = 0; i + 2 < n; i ++) // 重新找相差一个字符的子串
            {
                if(t[i] == '#' or t[i + 1] == '#' or t[i + 2] == '#') continue;
                if((t[i] == c1 and t[i + 1] == c2) 
                    or (t[i] == c1 and t[i + 2] == c2)
                    or (t[i + 1] == t[i + 2] and t[i + 1] == c2))
                        flag = 1;
            }
            if(cnt + flag >= f)
            {
                string t;
                t += c1; t += c2, t += c2;
                ans.push_back(t);
            }
        }
    }
    cout << ans.size() << '\n';
    for(auto i : ans) cout << i << '\n';
}

作者:OrangeOvO
链接:https://www.acwing.com/solution/content/267023/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值