Title Description:
In the computer, the wildcard character is a kind of special syntax, which is widely used in the fields of file searching, database, regular expression and so on. We now ask you to implement the string wildcard algorithm.
Requirements:
Implement the following 2 wildcard characters:
*: matches 0 or more characters (characters are made up of letters and numbers 0-9 , not case sensitive.) below) ( not including., etc special characters )
? : Matches 1 characters
Input
a wildcard expression;
A set of strings.
Output
Returns the matching result, correct output true, error output false
Ideas:
In three different situations:
(1) No wildcard: the corresponding characters are exactly equal, otherwise the match fails, the output is false
(2) What is a wildcard character? Case: The corresponding character is a letter (case-insensitive) or a number, otherwise the match fails, the output is false
(3) When the wildcard character is *: The next non-alphabetic and numeric characters in the matching string must be the same as the characters after the *, otherwise the match fails with the output false
If there is no false output after the last loop, it indicates that the match was successful and the output true
1 ImportJava.util.Scanner;2 3 /**4 * In the computer, a wildcard character is a special syntax, widely used in file search, database, regular expression and other fields. 5 * You are now required to implement the string wildcard algorithm. 6 * Requirements:7 * Implementation of the following 2 wildcard characters:8 * *: matches 0 or more characters (characters are made up of letters and numbers 0-9 and are not case sensitive.) hereinafter)9 *? : Matches 1 charactersTen * One * Input A * wildcard expression; - * A set of strings. - * Output the * Return matching result, correct output true, error output false - * - */ - Public classStringmatch { + - Public Static voidMain (string[] args) { + //input, read in two lines of characters AScanner cin =NewScanner (system.in); atString str =cin.nextline (); -String Strmatch =cin.nextline (); - cin.close (); - - intLen =str.length (); - intLenmatch =strmatch.length (); in inti = 0 ; - intj = 0 ; to + Chartemp; - //loop comparison, which determines whether the loop ends when either one reaches the end of the string the while(I < Len && J <Lenmatch) { * /* $ * Three types of cases:Panax Notoginseng * (1) No wildcard: the corresponding characters are exactly equal, otherwise the match fails, the output is false - * (2) What is the wildcard character? Case: The corresponding character is a letter (case-insensitive) or a number, the * Otherwise the match fails, output false + * (3) The case of a wildcard is *: The next non-alphabetic and numeric character of the matching string must be the same as the character after the *. A * Otherwise the match fails, output false the * If there is no false output after the last loop, the match is successful and the output is true + */ - if(Str.charat (i)! = ' * ' && str.charat (i)! = '? ' ){ $ //(1) non-wildcard characters $ if(Str.charat (i)! =Strmatch.charat (j)) { - //match failed with output false -System.out.println ("false"); the return ; -}Else{Wuyi //match successfully, enter the next character match thei++ ; -J + + ; Wu Continue ; - } About}Else if(Str.charat (i) = = '? '){ $ //(2) What is a wildcard character? The situation -temp =Strmatch.charat (j); - if(Temp >= ' a ' && temp <= ' z ') -|| (Temp >= ' A ' && temp <= ' Z ') A|| (temp >= ' 0 ' && temp <= ' 9 ')){ + //match successfully, enter the next character match thei++ ; -J + + ; $ Continue ; the}Else{ the //match failed with output false theSystem.out.println ("false"); the return ; - } in}Else{ the //(3) The case of a wildcard character * thetemp =Strmatch.charat (j); About if(Temp >= ' a ' && temp <= ' z ') the|| (Temp >= ' A ' && temp <= ' Z ') the|| (temp >= ' 0 ' && temp <= ' 9 ')){ the //Strmatch The current character of the match succeeds, the next character that enters Strmatch matches +J + + ; - Continue ; the}Else{ Bayi //the next non-alphabetic and numeric character in the matching string must be the same as the character after the *. the //indicates that the current character of Strmatch is a non-numeric, alphabetic character and must match the next character in Str the if(i = = (len-1)){ - //if the * is the last in Str, the match fails with the output false -System.out.println ("false"); the return ; the}Else{ the //If STR is not the last, continue to the next in STR and continue matching thei++ ; - Continue ; the } the } the }94 } the theSystem.out.println ("true"); the return ;98 } About -}
View Code
Huawei OJ Platform--string wildcard characters