url mapping ccf (Java regular expression solution)

Problem Description

Question number: 201803-3
Questions Name: URL mapping
time limit: 1.0s
Memory Limit: 256.0MB
Problem Description:
Problem Description
  URL mapping is a key component such as Django, Ruby on Rails and other web frameworks (web frameworks) of. Sent from the browser to the HTTP request, URL mapping module resolves the URL request and dispatches them to the corresponding processing code. Now, you come to realize a simple URL mapping function.
  In this problem configured URL mapping by several pieces of URL mapping rules. When a request arrives, URL address URL mapping function will request one by one in the order of the rules match the configuration. When faced with the first rule exact match, the match is successful, get matching rules and parameters to match. If you can not match any of these rules, the match fails.
  The title URL address is entered slash / as the path separator to ensure that begins with a slash. Other characters also includes legal case letters, digits, the minus sign -, underscore _ and the decimal point .. For example, / person / 123 / is a legitimate URL addresses, and / person / 123? Is not legitimate (there is no legal character question mark?). Further, letters case sensitive, so / case / and / CAse / different URL address.
  For the URL mapping rules, the same is beginning slash. In addition to the normal URL address a, may further contain parameters, there are the following three:
  string <str>: to match a string, where the string can not contain a slash attention. For example, abcde0123.
  Integer <int>: matching for a unsigned integer, all the Arabic numerals. For example, 01234.
  Path <path>: to match a string, the string can contain a slash. For example, abcd / 0123 /.
  Three or more types of parameters must match non-empty string. Simplicity, the rules in the title <str> and <int> must be the front slash slash either behind, or the end of the rule (i.e. the parameter is the last part of the rule). The front <path> must be a slash followed by the rule must be ended. Whether or URL address rules consecutive slash will not appear.
Input Format
  The first input line is a two positive integers  n  and  m , respectively represent the number of URL and the number of rules to be processed URL address mapping, separated by a space character.
  Line second to  n- + 1'd line by matching the order described in the configuration profile URL mapping rules. Of  I + 1'd row comprises two strings  P I  and  R & lt I , in which  P I  represents the rule matching the URL, R & lt I  represents this URL matching names. Two non-empty strings, does not contain a space character, both separated by an intermediate space character.
  The first  n- +2 row to the  n- + m + 1'd line describes the URL address to be processed. Of  n- +1+ I  line comprising a string  Q I Indicating the URL address to be processed, the string does not contain a space character.
Output Format
  The input common-  m  row, the  i  line indicates  Q i  of matching results. If the match is successful, the matching rule set  P J  , the output of the corresponding  R & lt J . Meanwhile, if there is a rule parameters, the output parameters of the matching sequence in the same row. Note should remove leading zero integer parameter output. Adjacent separated by a space character between the two. If the match fails, then the output 404.
Sample input
5 4
/articles/2003/ special_case_2003
/articles/<int>/ year_archive
/articles/<int>/<int>/ month_archive
/articles/<int>/<int>/<str>/ article_detail
/static/<path> static_serve
/articles/2004/
/articles/1985/09/aloha/
/articles/hello/
/static/js/jquery.js
Sample Output
year_archive 2004
article_detail 1985 9 aloha
404
static_serve js/jquery.js
样例说明
  对于第 1 个地址 /articles/2004/,无法匹配第 1 条规则,可以匹配第 2 条规则,参数为 2004。
  对于第 2 个地址 /articles/1985/09/aloha/,只能匹配第 4 条规则,参数依次为 1985、9(已经去掉前导零)和 aloha。
  对于第 3 个地址 /articles/hello/,无法匹配任何一条规则。
  对于第 4 个地址 /static/js/jquery.js,可以匹配最后一条规则,参数为 js/jquery.js。
数据规模和约定
  1 ≤  n ≤ 100,1 ≤  m ≤ 100。
  所有输入行的长度不超过 100 个字符(不包含换行符)。
  保证输入的规则都是合法的。

 

把字符串的前导0也去掉了,80分。。。

 1 import java.util.LinkedHashMap;
 2 import java.util.Map;
 3 import java.util.Scanner;
 4 import java.util.regex.Matcher;
 5 import java.util.regex.Pattern;
 6 
 7 
 8 public class Main {
 9 
10     public static void main(String[] args) {
11         int n, m;
12         int flag = 0;
13         Scanner sc = new Scanner(System.in);
14         n = sc.nextInt();
15         m = sc.nextInt();
16         sc.nextLine();
17         LinkedHashMap<String, String> mp = new LinkedHashMap<>();
18         for (int i = 1; i <= n; ++i) {
19             String line = sc.nextLine();
20             String[] arr = line.split(" ");
21             mp.put(arr[0], arr[1]);
22         }
23 
24         for (int i = 1; i <= m; ++i) {
25             String line = sc.nextLine();
26             for (Map.Entry<String, String> en : mp.entrySet()) {
27                 String regex = en.getKey();
28                 regex = regex.replace("<int>", "(\\d+)");
29                 regex = regex.replace("<str>", "(.+)");
30                 regex = regex.replace("<path>", "(.+)");
31                 if(line.matches(regex)) {    // 完全匹配
32                     flag = 1;
33                     Pattern p = Pattern.compile(regex);
34                     Matcher mat = p.matcher(line);
35                     while (mat.find()) {    
36                         System.out.print(en.getValue());
37                         for (int j = 1; j <= mat.groupCount(); ++j) {
38                             String str = mat.group(j);
39                     //        if(str.matches("\\d+"))
40                             str = str.replaceAll("^0+", "");    // 去掉数字前面的0 (字符串前面的0也去掉了)
41                             System.out.print(" " + str);
42                         }
43                         System.out.println();
44                     }
45                     break;
46                 }
47             }
48             if(flag == 0)
49                 System.out.println(404);
50             else
51                 flag = 0;    // 重置flag
52         }
53 
54     }
55 
56 }

 

Guess you like

Origin www.cnblogs.com/FengZeng666/p/11484278.html