标签:
试题编号:201409-3
试题名称:字符串匹配
时间限制: 1.0s
内存限制: 256.0MB
1 package ccf_test2014_09; 2 3 import java.util.Scanner; 4 public class StringPiPei { 5 6 public static void main(String[] args) { 7 8 Scanner input = new Scanner(System.in); 9 10 String s = input.nextLine().trim(); 11 12 boolean flag = (input.nextInt()==1); 13 14 input.nextLine(); 15 16 int num = input.nextInt(); 17 18 String [] strings = new String[num]; 19 20 String [] outStrings = new String[num]; 21 22 int total = 0; 23 24 input.nextLine(); 25 26 for(int i = 0; i < num; i++){ 27 28 strings[i] = input.nextLine().trim(); 29 30 } 31 32 for(int i = 0; i < num; i++){ 33 34 int length = strings[i].length(); 35 36 for(int j = 0; length-j>=s.length();j++){ 37 38 String newString = strings[i].substring(j, j+s.length()); 39 40 if(flag && newString.equals(s)){ 41 42 outStrings[total++] = strings[i]; 43 44 break; 45 46 } 47 if(!flag && newString.compareToIgnoreCase(s)== 0){ 48 49 outStrings[total++] = strings[i]; 50 51 break; 52 } 53 } 54 55 } 56 for(int i = 0; i < total; i++){ 57 58 System.out.println(outStrings[i]); 59 } 60 61 } 62 63 }
运行结果:
实现代码(c++):
1 #include<bits/stdc++.h> 2 #include<algorithm> 3 #include<iostream> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <functional> 8 #include <cctype> 9 using namespace std; 10 string s1,s2; 11 int n; 12 void solve(string &s) 13 { 14 int l=s.size(); 15 for(int i=0;i<l;i++) 16 { 17 if(s[i]>=‘A‘&&s[i]<=‘Z‘) s[i]=s[i]-‘A‘+‘a‘; 18 } 19 } 20 int main() 21 { 22 // freopen("in.txt","r",stdin); 23 int flag; 24 while(cin>>s1){ 25 scanf("%d",&flag); 26 if(flag){ 27 scanf("%d",&n); 28 for(int i=0;i<n;i++) 29 { 30 cin>>s2; 31 if(s2.find(s1)!= s2.npos) cout<<s2<<endl; 32 } 33 } 34 else{ 35 solve(s1); 36 scanf("%d",&n); 37 for(int i=0;i<n;i++) 38 { 39 cin>>s2; 40 string s3=s2; 41 solve(s2); 42 43 if(s2.find(s1) != s2.npos) cout<<s3<<endl; 44 } 45 } 46 } 47 return 0; 48 }
运行结果:
标签:
原文地址:http://www.cnblogs.com/haimishasha/p/5356889.html