CCF 201803-3 URL映射

原文链接

我的个人博客

原题链接

URL映射

思路

用p数组存放所有的url匹配规则,r数组记录匹配的名称。对于每一个需要进行判读的url路径,将其与所有的p数组中的字符串进行比较。一旦有符合的就输出。

代码
#include <bits/stdc++.h>
using namespace std;

const int N = 100;
string p[N], r[N], s;

//判读s是否符合t的匹配规则 
bool match(string& s, string& t, bool flag){
	int lent = t.size();
	int lens = s.size();
	int ps=0,pt=0;
	while(ps < lens && pt < lent){
		if(t[pt] == s[ps]){
			pt++;
			ps++;
		}else{
			//去匹配<xxx>
			if(t[pt++] != '<'){
				return false; 
			} 
			if(flag) cout<<" ";
			
			bool ok = false;
			if(t[pt] == 'i'){
				//匹配的是<int>
				while(s[ps] && isdigit(s[ps])){
					if(s[ps] != '0')
						ok = true;
					if(flag && ok){
						cout<<s[ps];
					}
					ps++;
				}
				if(!ok){
					return false;//不能匹配<int> 
				} 
				pt += 4; 
			}else if(t[pt] == 's'){
				//匹配的是<str>
				bool ok = false;
				while(s[ps] && s[ps]!='/'){
					ok = true;
					if(flag){
						cout<<s[ps];
					}
					ps++;
				} 
				if(!ok){
					return false;
				}
				pt += 4;
			}else if(t[pt] == 'p'){
				//匹配的是<path>
				//不管是什么,path一定是符合的 
				if(flag){
					while(s[ps]){
						cout<<s[ps++];
					}
				} 
				return true;
			} 
		}
	} 
	return pt==lent && ps == lens;
}
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>p[i]>>r[i];//输入匹配的规则和 名字 
	}
	for(int i=0;i<m;i++){
		cin>>s;//输入要处理的字符串
		bool flag = true;//表示是否找到
		for(int j=0;flag&&j<n;j++){
			if(match(s,p[j],false)){
				//与某个规则匹配
				flag = false;
				cout<<r[j];
				match(s,p[j],true);//输出结果 
			}
		} 
		if(flag){
			cout<<"404";//表示未找到 
		} 
		cout<<endl; 
	} 
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值