zoukankan      html  css  js  c++  java
  • CCF系列之字符串匹配(201409-3)

    试题编号:201409-3
    试题名称:字符串匹配
    时间限制: 1.0s
    内存限制: 256.0MB

    问题描述
      给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行。你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符;当选项关闭时,表示同一个字母的大写和小写看作相同的字符。
    输入格式
      输入的第一行包含一个字符串S,由大小写英文字母组成。
      第二行包含一个数字,表示大小写敏感的选项,当数字为0时表示大小写不敏感,当数字为1时表示大小写敏感。
      第三行包含一个整数n,表示给出的文字的行数。
      接下来n行,每行包含一个字符串,字符串由大小写英文字母组成,不含空格和其他字符。
    输出格式
      输出多行,每行包含一个字符串,按出现的顺序依次给出那些包含了字符串S的行。
    样例输入
    Hello
    1
    5
    HelloWorld
    HiHiHelloHiHi
    GrepIsAGreatTool
    HELLO
    HELLOisNOTHello
    样例输出
    HelloWorld
    HiHiHelloHiHi
    HELLOisNOTHello
    样例说明
      在上面的样例中,第四个字符串虽然也是Hello,但是大小写不正确。如果将输入的第二行改为0,则第四个字符串应该输出。
    评测用例规模与约定
      1<=n<=100,每个字符串的长度不超过100。
     
     
    解题思路:
     
    实现代码(java):
      
     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 }
    View Code

    运行结果:

      

     实现代码2(java)

      

     1 import java.util.Scanner;
     2 import java.util.regex.Matcher;
     3 import java.util.regex.Pattern;
     4 
     5 public class Main {
     6     public static void main(String[] args) {
     7         Scanner sc=new Scanner(System.in);
     8         String str=sc.next();
     9         int flag=sc.nextInt();
    10         int n=sc.nextInt();
    11         String[] array=new String[n];
    12         for (int i = 0; i < n; i++) {
    13             array[i]=sc.next();
    14         }
    15         for (int i = 0; i < n; i++) {
    16             Pattern p;
    17             if (flag==0) {
    18                 p=Pattern.compile(str,Pattern.CASE_INSENSITIVE);
    19             }else{
    20                 p=Pattern.compile(str);
    21             }
    22             
    23             Matcher m=p.matcher(array[i]);
    24             if (m.find()) {
    25                 System.out.println(array[i]);
    26             }
    27         }
    28         
    29     }
    30 
    31 }
    View Code

    运行结果:

      

    实现代码(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 }
    View Code

    运行结果:

      

  • 相关阅读:
    json.net的常用语句JsonConvert.SerializeObject(对象)
    struts2国际化
    多浏览器兼容性问题及解决方案之Javascript篇
    对XMLHttpRequest异步请求的面向对象封装
    java的学习资料
    多浏览器兼容性问题及解决方案之CSS篇
    jQuery库与其他JS库冲突的解决办法
    abstract virtual interface区别
    项目优化经验——垃圾回收导致的性能问题(转)
    C#实现小写金额转大写金额
  • 原文地址:https://www.cnblogs.com/haimishasha/p/5356889.html
Copyright © 2011-2022 走看看