• AcWing第126场周赛 - 思维+字符串处理+递归


    AcWing 5280. 替换字符

    直接暴力,但是需要注意初始化

    #include 
    using namespace std;
    #define ll long long
    #define sf(x) scanf("%d", &x);
    #define de(x) cout << x << " ";
    #define Pu puts("");
    const int N = 1e5 + 9, mod = 1e9 + 7;
    char a[N], b[N];
    int n, m, ans;
    int u[N], k;
    void fun(int x) {
        int res = 0;
        for (int i = x; i <= x + n - 1; i++) {
            if (a[i - x + 1] == b[i])
                res++;
        }
        if ((n - res) < ans) {  // 如果当前子序列,需要需改的数量小于ans,则进行修改
            k = 0;
            for (int i = x; i <= x + n - 1; i++) {
                if (a[i - x + 1] != b[i]) {
                    k++;
                    u[k] = i - x + 1;
                }
            }
            ans = k;
        }
    }
    void init() {  // ans初始化
        ans = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] != b[i]) {
                ans++;
            }
        }
        k = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] != b[i]) {
                u[++k] = i;
            }
        }
    }
    int main() {
        cin >> n >> m;
        cin >> (a + 1);
        cin >> (b + 1);
        init();
        for (int i = 2; i <= m - n + 1; i++) {
            fun(i);
        }
        cout << ans << endl;
        for (int i = 1; i <= k; i++) {
            cout << u[i] << " ";
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    AcWing 5281. 扩展字符串

    这个题目参考的题解,很好的一个递归题目
    参考题解
    分情况进行讨论

    #include 
    using namespace std;
    #define ll long long
    #define sf(x) scanf("%d", &x);
    #define de(x) cout << x << " ";
    #define Pu puts("");
    const int N = 1e5 + 9, mod = 1e9 + 7;
    char a[N], b[N];
    int n, m, ans;
    int u[N], k;
    void fun(int x) {
        int res = 0;
        for (int i = x; i <= x + n - 1; i++) {
            if (a[i - x + 1] == b[i])
                res++;
        }
        if ((n - res) < ans) {  // 如果当前子序列,需要需改的数量小于ans,则进行修改
            k = 0;
            for (int i = x; i <= x + n - 1; i++) {
                if (a[i - x + 1] != b[i]) {
                    k++;
                    u[k] = i - x + 1;
                }
            }
            ans = k;
        }
    }
    void init() {  // ans初始化
        ans = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] != b[i]) {
                ans++;
            }
        }
        k = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] != b[i]) {
                u[++k] = i;
            }
        }
    }
    int main() {
        cin >> n >> m;
        cin >> (a + 1);
        cin >> (b + 1);
        init();
        for (int i = 2; i <= m - n + 1; i++) {
            fun(i);
        }
        cout << ans << endl;
        for (int i = 1; i <= k; i++) {
            cout << u[i] << " ";
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  • 相关阅读:
    Medical transformer源码解读
    k8s 基础
    pytorch 初始化
    wordpress制作主题步骤
    通过Office Web Viewer站点在线展示Office文档内容
    Windows与网络基础-16-Windows共享
    死磕它七年“腾讯限量版”Java架构笔记,要个40k不过分吧?
    Spring容器&Bean生命周期常见接口
    零基础入门学习Python第二阶04SQL详解03
    C#报错 功能“结构字段初始化表达式“在C#7.3中不可用。请使用10.0或更高的语言版本。
  • 原文地址:https://blog.csdn.net/weixin_52490191/article/details/133993354