题解 | #字符串匹配#

字符串匹配

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

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            String[] strs = new String[n];
            for (int i = 0; i < n; i++) {
                strs[i] = scanner.next();
            }

            String regex = scanner.next();
            Pattern pattern = Pattern.compile(regex.toLowerCase());

            for (int i = 0; i < n; i++) {
                Matcher matcher = pattern.matcher(strs[i].toLowerCase());
                if (matcher.find())
                    System.out.println(i + 1 + " " + strs[i]);
            }

        }
    }

}

直接把模式串视为正则表达式

全部评论

相关推荐

用微笑面对困难:只要你保证项目和获奖都是真的就行尤其是“对战,总负责人”啊这些套职,基本上队员,打杂的都这么写
点赞 评论 收藏
分享
09-17 10:53
四川大学 C++
牛客91242815...:会写标书没有任何卵用,鉴定为横向垃圾导师的受害者
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务