第三題
- 第三題,是一個根據url格式來判斷輸入的url屬於哪一類,然後來輸出是否匹配,以及解析後的結構
- Django框架就是類似的URL匹配
- 總的來說,沒有難點,但很麻煩,因爲要考慮的因素比較多
- 也是花了不少時間,還沒有得到滿分
- 把代碼貼出來供大家參考一下
import java.util.Scanner;
public class third
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
String string=scanner.nextLine();
String sd[]=string.split(" ");
int sum[]=transfer(sd);
int form_number=sum[0];
int line_number=sum[1];
String str_form[]=new String[form_number];
String form_gs[]=new String[form_number];
String form_name[]=new String[form_number];
String str_line[]=new String[line_number];
boolean is_file[]=new boolean[line_number];
for (int i = 0; i < str_form.length; i++) {
str_form[i]=scanner.nextLine();
form_gs[i]=str_form[i].split(" ")[0];
form_name[i]=str_form[i].split(" ")[1];
}
for (int i = 0; i < str_line.length; i++) {
str_line[i]=scanner.nextLine();
}
for (int i = 0; i < str_line.length; i++) {
is_file[i]=false;
boolean ismatch[]=new boolean[form_gs.length];
for (int j = 0; j < ismatch.length; j++) {
ismatch[j]=true;
}
String linesp[]=str_line[i].split("/");
for (int j = 0; j < form_gs.length; j++) {
String each[]=form_gs[j].split("/");
if (each.length>linesp.length) {
ismatch[j]=false;
continue;
}
for (int k = 0; k < each.length; k++) {
if (!each[k].startsWith("<")) {
if(!linesp[k].equalsIgnoreCase(each[k])){
ismatch[j]=false;
break;
}
}
else {
String type=findType(each[k]);
if (type.equalsIgnoreCase("path")) {
is_file[i]=true;
if (linesp.length>each.length) {
continue;
}
}
if (each.length!=linesp.length) {
ismatch[j]=false;
continue;
}
boolean is_equal=is_equal(linesp[k], type);
if (is_equal) {
}
else {
ismatch[j]=false;
break;
}
}
}
}
boolean is_404=true;
for (int j = 0; j < form_gs.length; j++) {
if (ismatch[j]==true) {
System.out.print(form_name[j]+" ");
if (is_file[i]) {
String each[]=form_gs[j].split("/");
for (int j2 =2; j2 < linesp.length; j2++) {
if (j2>=each.length) {
System.out.print("/"+linesp[j2]);
}
else if (j2>=each.length-1) {
System.out.print(linesp[j2]);
}
else {
System.out.print(linesp[j2]+" ");
}
}
}
else {
for (int j2 =2; j2 < linesp.length; j2++) {
System.out.print(linesp[j2]+" ");
}
}
System.out.println();
is_404=false;
break;
}
}
if (is_404) {
System.out.println("404");
}
}
}
static boolean is_equal(String str,String type){
if (type.equalsIgnoreCase("int")) {
boolean result= is_int(str);
return result;
}
else if (type.equalsIgnoreCase("str")) {
return true;
}
return false;
}
static boolean is_int(String str){
try {
int a=Integer.parseInt(str);
return true;
} catch (Exception e) {
return false;
}
}
static String findType( String str){
int length=str.length()-1;
String string=str.substring(1,length);
return string;
}
static void outstr (String a[]){
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
static void out (int a[]){
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
static int[] transfer(String str[]){
int a[]=new int [str.length];
for (int i = 0; i < str.length; i++) {
a[i]=Integer.parseInt(str[i]);
}
return a;
}
}
希望能幫助到大家
等題目出來後我會重新做一遍的