CCF topic: string Matching

Source: Internet
Author: User

Question number:

201409-3

Question Name:

String match

Time limit:

1.0s

Memory Limit:

256.0MB

Problem Description:

Problem description

Gives a string and multiline text, where the string appears in those lines. Your program also needs to support case sensitive options: When an option is on, the uppercase and lowercase letters of the same letter are treated as different characters; When the item is turned off, the uppercase and lowercase letters of the same letter are considered to be the same characters.

Input format

The first line of the input contains a string s, which is made up of uppercase and lowercase English letters.
The second line contains a number that represents the case-sensitive option, when the number is 0, the case is not sensitive, and when the number is 1, it is case sensitive.
The third line contains an integer n that represents the number of lines of the given text.
Next n rows, each line contains a string, and the string consists of uppercase and lowercase letters, without spaces and other characters.

Output format

Outputs multiple lines, each containing a string, in the order in which they appear, giving the lines that contain the string s.

Sample input

Hello
1
5
HelloWorld
Hihihellohihi
Grepisagreattool
HELLO
Helloisnothello

Sample output

HelloWorld
Hihihellohihi
Helloisnothello

Sample Description

In the example above, the fourth string, although also a hello, is not case-sensitive. If you change the second line of input to 0, the fourth string should be output.

Measuring use case size and conventions

1<=n<=100, the length of each string does not exceed 100.


==========================================================

Solution:

Package Csp3;import Java.util.scanner;public class Main3 {public static void main (string[] args) {new Main3 (). Run ();} public void Run () {Scanner sc=new Scanner (system.in); String str=sc.next (); int flag=sc.nextint (); int n=sc.nextint (); String[] s=new string[n];for (int i=0;i<n;i++) {s[i]=sc.next ();   Use Nextline () Here, you can enter only 4 rows}if (flag==1) {for (int i=0;i<n;i++) {if (S[i].indexof (str) >=0) {  //indexof indicates whether to include substrings , including return value >=0SYSTEM.OUT.PRINTLN (S[i]);}}} else{for (int i=0;i<n;i++) {if (S[i].touppercase (). IndexOf (Str.touppercase ()) >=0) System.out.println (S[i]);}}}

==========================================================

Knowledge Supplement:

The difference between next () and nextline () in scanner

When you use the Nextline () method, all the contents of the row remaining are read, including the newline character, and then the focus moves to the beginning of the next line. Therefore, you cannot receive a variable of type string that is entered in the next line.

The next () method receives the next string type variable as a line break or a space character.

Two examples (example selected from http://www.cnblogs.com/gold-worker/archive/2013/04/10/3013063.html)

Example 1:

Package Cn.dx;import Java.util.scanner;public class Scannertest {public    static void Main (string[] args) {        Scanner in =  new Scanner (system.in);        System.out.println ("Please enter an integer");        while (In.hasnextint ()) {            int num = In.nextint ();            System.out.println ("Please enter a string");            String str = in.nextline ();            System.out.println ("num=" +num+ ", str=" +str);            System.out.println ("Please enter an integer");}}}    

Results:

Please enter an integer
1231
Please enter a string
Num=1231,str=
Please enter an integer

The second parameter of type string is not read in.

=================================

Example 2:

Package Cn.dx;import Java.util.scanner;public class Scannertest {public    static void Main (string[] args) {        Scanner in =  new Scanner (system.in);        System.out.println ("Please enter an integer");        while (In.hasnextint ()) {            int num = In.nextint ();            System.out.println ("Please enter a string");            String str = In.next ();            System.out.println ("num=" +num+ ", str=" +str);            System.out.println ("Please enter an integer");}}}    

Results:

Please enter an integer
123
Please enter a string
Sdjakl
Num=123,str=sdjakl

Please enter an integer
213 Jdskals
Please enter a string
Num=213,str=jdskals
Please enter an integer



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CCF topic: string Matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.