题解 | #字符串匹配#

字符串匹配

https://www.nowcoder.com/practice/fbdc522ef958455687654b38a4ca01e0

遇到方括号时判断

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string>
#include<ctype.h>
#include<queue>
using namespace std;


int main(){
    int n;
    // cin >> n;
    string s;
    while(cin >> n){
        string T[n], t[n];
        for(int i=0;i<n;++i){
            cin >> T[i];
            t[i] = T[i];
            for(int j=0;j<T[i].size();++j)
                t[i][j] = tolower(T[i][j]);
        }
        
        cin >> s;
        for(int j=0;j<s.size();++j)
            s[j] = tolower(s[j]);
        
        
        for(int k=0;k<n;++k){
            int i=0, j=0;
            int f = 0, m = t[k].size();
            while(i<m  && j<s.size()){
                if(s[j] == '[')
                    f = 1;
                if(s[j] == t[k][i]){
                    ++j;
                    ++i;
                }else{
                    if(f){
                        while(s[j] != ']' && s[j] != t[k][i]) ++j;
                        if(s[j] == t[k][i]){
                            ++i;
                            while(s[j] != ']') ++j;
                            f = 0; ++j;
                        }
                        else
                            break;
                    }else
                        break;
                }
            }
            if(i>=m ){
                cout << k+1 << ' ' << T[k] << endl;
            }
        }
    }
//     system("pause");
    return 0;
}



全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务