记花了朕三个半小时写的模拟题

没想到,这年头模拟题都这么难以模拟了。

学校让我们准备cspccf比赛,于是乎,找到了ccf的题库。然后做到了这道题:201803-3URL映射。鉴于此题细节之多以及模拟之复杂,想到如果直接写的话,我可能会shi,不shi的话亦可能脱发enmm

于是乎,我打开了谷歌浏览器,输入了ccfcsp201903-3。然后....

首先,承认思路借鉴了网上大佬。其次,代码是我自己敲的,花了我一个下午qwq。

恩,大佬的思路:

那么,小弟不才,展示一下自己写的代码吧:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

const int MAXN = 110;
string rule[MAXN];///读入规则
int rule_[MAXN] = {0};///记录规则i最后是否为/
string ruleItem[MAXN][MAXN];///记录规则的每一项
string ruleParameter[MAXN];///记录规则的参数
int ruleLength[MAXN] = {0};///记录第i条规则的项数
string match;///待匹配串
int match_ = 0;///记录待匹配串的最后一个字符是否为/
string matchItem[MAXN];///待匹配串的每一项
int matchLength = 0;///待匹配串的项数
string ans = "";///记录结果
void Init(int i){
    int length_i = rule[i].length();
    if (rule[i][length_i - 1] == '/')
        rule_[i] = 1;
    for (int j = 0; j < length_i; j ++){
        if (rule[i][j] == '/')
            rule[i][j] = ' ';
    }
    string temp;
    stringstream input(rule[i]);///https://blog.csdn.net/onever_say_love/article/details/49123935
    int j = 0;
    while (input >> temp){
        ruleItem[i][j++] = temp;
        ruleLength[i] ++;
    }
}
void Init_(){
    match_ = 0;
    matchLength = 0;
    int length_match = match.length();
    if (match[length_match-1] == '/')
        match_ = 1;
    for (int i = 0; i < length_match; i ++){
        if (match[i] == '/')
            match[i] = ' ';
    }
    string temp;
    stringstream input(match);
    int i = 0;
    while(input >> temp){
        matchItem[i++] = temp;
        matchLength ++;
    }
}
bool isNumber(string MI, string &temp){
    bool flag = true;
    int length_MI = MI.length();
    for (int i = 0; i < length_MI; i ++){
        if (MI[i] > '9' || MI[i] < '0'){
            return false;
        }else {
            if (MI[i] == '0' && flag){

            }else{
                temp += MI[i];
                flag = false;
            }
        }
    }
    return true;
}
bool matching(int j){
    ans = "";
    int pj = 0;
    int pm = 0;
    while (pm < matchLength && pj < ruleLength[j]){
        if (matchItem[pm] == ruleItem[j][pj]){
            pm ++;
            pj ++;
            continue;
        }
        string temp = "";
        if (ruleItem[j][pj] == "<int>"){
            if (isNumber(matchItem[pm], temp)){
                ans += " ";
                ans += temp;
            }else{
                return false;
            }
        }else if (ruleItem[j][pj] == "<str>"){
            ans += " ";
            ans += matchItem[pm];
        }else if (ruleItem[j][pj] == "<path>"){
            ans += " ";
            ans += matchItem[pm];
            pm ++;
            while (pm < matchLength){
                ans += "/";
                ans += matchItem[pm++];
            }
            if (match_){
                ans += "/";
            }
            return true;
        }else{
            return false;
        }
        pm ++;
        pj ++;
    }
    if (match_ ^ rule_[j])
        return false;
    if (pj != ruleLength[j] || pm != matchLength)
        return false;
    return true;
}
int main(){
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++){
        cin >> rule[i] >> ruleParameter[i];
        Init(i);
    }
    for (int i = 0; i < m; i++){
        bool flag = false;
        cin >> match;
        Init_();
        for (int j = 0; j < n; j ++){
            if (matching(j)){
                cout << ruleParameter[j] << ans << endl;
                flag = true;
                break;
            }
        }
        if (!flag){
            cout << "404" << endl;
        }
    }
    return 0;
}

写出来以后还得调cnm。总之代码好多地方写的有明显错误,不过,在我的手动codeblocks调试内存(cout)下终于解决了。

好了,现在17::45了,我要去二餐吃饭了。话说六区的这个研讨型教室简直就是天堂,格力空调、超快wifi、舒适座椅,安静环境。实不相瞒,我都想住在这里面了hhhh。

对了,记得看一下sstream哦,其中用到了哦,后面有链接哦       是在下没错了

附借鉴大佬的博客:https://blog.csdn.net/xbb224007/article/details/79962425

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值