PTA 7-29 删除字符串中的子串

作者: smatrcHendsa | 来源:发表于2019-03-17 10:04 被阅读0次

    https://pintia.cn/problem-sets/14/problems/809
    so ugly...

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    
    int main() {
        char a[100], b[99];
        int fl[100];
        for (int i = 0; i < 100; i++) {
            fl[i] = 1;
        }
        
        int cnt = 0; 
        while (1) {
            char c;
            scanf("%c", &c);
            if (c == '\n') {
                a[cnt] = '\0';
                break;
            }
            a[cnt++] = c;
        }
         cnt = 0; 
        while (1) {
            char c;
            scanf("%c", &c);
            if (c == '\n') {
                b[cnt] = '\0';
                break;
            }
            b[cnt++] = c;
        }
        
        //printf("%d ", strlen(b));
        
        bool flag = true;
        while (flag) {
            flag = false;
            for (int i = 0; i < strlen(a); i++) {
                int k = i;
                
                int j = 0;
                
                while (j < strlen(b)) {
                    if (!fl[k + j]) {
                        k++;
                        continue;
                    }
                    if (a[k + j] == b[j])
                        j++;
                    else
                        break;
                }
                
                if (j == strlen(b)) {
                    flag = true;
                    j = 0;
                    k = i;
                    while (j < strlen(b)) {
                        if (!fl[k + j]) {
                            k++;
                            continue;
                        }
                        if (a[k + j] == b[j]) {
                            fl[k + j] = 0;
                            j++;
                        }
                        else
                            break;
                    }
                }
            }
        }
        
        for (int i = 0; i < strlen(a); i++) {
            if (fl[i])
                printf("%c", a[i]);
        }
        printf("\n");
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:PTA 7-29 删除字符串中的子串

        本文链接:https://www.haomeiwen.com/subject/ssvymqtx.html