ADV-336 字符串匹配

ADV-336 字符串匹配

用string

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    
    
    string s, choice;
    int mark, n;
    cin >> s >> mark >> n;
    while (n--){
    
    
        cin >> choice;
        if (mark == 1){
    
    
            if (choice.find(s) != -1)
                cout << choice << endl;
        }
        else {
    
    
            string r_choice = choice, r_s = s;
            transform(r_choice.begin(), r_choice.end(), r_choice.begin(), ::tolower);
            transform(r_s.begin(), r_s.end(), r_s.begin(), ::tolower);
            if (r_choice.find(r_s) != -1)
                cout << choice << endl;
        }
    }
    return 0;
}

朴素

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
    
    
    char s[110], choice[110], r_choice[110];
    scanf("%s", s);
    int mark, n, s_len = int(strlen(s));
    scanf("%d%d", &mark, &n);
    while (n--){
    
    
        scanf("%s", choice);
        int c_len = int(strlen(choice));
        strcpy(r_choice, choice);
        if (mark == 0){
    
    
            for (int i = 0; i < s_len; i++) s[i] = tolower(s[i]);
            for (int i = 0; i < c_len; i++) choice[i] = tolower(choice[i]);
        }
        int i = 0, j = 0;
        while (i < c_len && j < s_len){
    
    
            if (s[j] == choice[i]){
    
    
                i++;
                j++;
            }
            else {
    
    
                i = i-j+1;
                j = 0;
            }
        }
        if (j >= s_len) printf("%s\n", r_choice);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43359312/article/details/105488027