import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
/**
* 问题描述
给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行。你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符;当选项关闭时,表示同一个字母的大写和小写看作相同的字符。
输入格式
输入的第一行包含一个字符串S,由大小写英文字母组成。
第二行包含一个数字,表示大小写敏感的选项,当数字为0时表示大小写不敏感,当数字为1时表示大小写敏感。
第三行包含一个整数n,表示给出的文字的行数。
接下来n行,每行包含一个字符串,字符串由大小写英文字母组成,不含空格和其他字符。
输出格式
输出多行,每行包含一个字符串,按出现的顺序依次给出那些包含了字符串S的行。
样例输入
Hello
1
5
HelloWorld
HiHiHelloHiHi
GrepIsAGreatTool
HELLO
HELLOisNOTHello
样例输出
HelloWorld
HiHiHelloHiHi
HELLOisNOTHello
样例说明
在上面的样例中,第四个字符串虽然也是Hello,但是大小写不正确。如果将输入的第二行改为0,则第四个字符串应该输出。
评测用例规模与约定
1<=n<=100,每个字符串的长度不超过100。
* @author llp
*
*/
public class MatchString {
public static List<String> list = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String src = sc.nextLine();
int check = Integer.parseInt(sc.nextLine());
int n = Integer.parseInt(sc.nextLine());
for(int i = 0 ; i < n ; i++) {
if(check == 0)
//当check为0的时候 表示大小写不敏感
MatchCaseIngore(src,sc.nextLine());
else
//当check为1的时候 表示大小写敏感
MatchCaseSensitive(src,sc.nextLine());
}
//遍历list 输出匹配的字符串
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
//大小写敏感匹配
public static void MatchCaseSensitive(String src,String desc) {
int j = 0;
int count;
int temp = 0;
boolean flag = true;
while(j < desc.length() && flag) {
count = 0;
temp++;
for(int i = 0 ; i < src.length() ; i++) {
if(src.charAt(i) == desc.charAt(j)) {
j++;
count++;
if(count == src.length()) {
list.add(desc);
flag = false;
break;
}
}
else {
j = temp;
break;
}
}
}
}
//大小写不敏感匹配
public static void MatchCaseIngore(String src,String desc) {
int j = 0;
int count;
int temp = 0;
boolean flag = true;
while(j < desc.length() && flag) {
count = 0;
temp++;
for(int i = 0 ; i < src.length() ; i++) {
if(src.charAt(i) == desc.charAt(j) || Math.abs((int)src.charAt(i) - (int)desc.charAt(j)) == 32) {
j++;
count ++;
if(count == src.length()) {
list.add(desc);
flag = false;
break;
}
}
else {
j = temp;
break;
}
}
}
}
}
CCF试题Java-字符串匹配
猜你喜欢
转载自blog.csdn.net/kidchildcsdn/article/details/81392445
今日推荐
周排行