Skip to content

Pro3

原始文件为 CPP 代码,本文是转换后的 Markdown 文件。

#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> rule[105];
vector<string> value;
bool lastarr[105];
int main()
{
    freopen("in.txt","r",stdin);

    int n,m;
    string line,index,content;
    scanf("%d%d",&n,&m);
    cin.get();

    for (int i = 0; i < n; ++i)
    {
        getline(cin,line);
        stringstream ss(line);
        ss >> index >> content;

        lastarr[i] = false;
        if (index[index.size()-1] == '/') lastarr[i] = true;

        stringstream split_index(index);
        string item;
        getline(split_index,item,'/');
        while(getline(split_index,item,'/'))
        {
            rule[i].push_back(item);
        }       
        value.push_back(content);
    }

    for (int i = 0; i < m; ++i)
    {
        vector<string> input;
        vector<string> output;

        getline(cin,line);
        stringstream ss(line);
        string item;

        bool last = false;
        if (line[line.size()-1] == '/') last = true;
        getline(ss,item,'/');
        while(getline(ss,item,'/'))
            input.push_back(item);

        int size = input.size();
        bool flag = true;
        for (int j = 0; j < n; ++j)
        {
            int count = 0;
            for (auto x = rule[j].begin(); x != rule[j].end(); ++x)
            {
                //cout << count << "  " << size << endl;
                if (count == size) goto notthis;

                if (*x == "<int>")
                {
                    string str = input[count];
                    for(char& item : str)
                    {
                        if (item >= '0' && item <= '9') 
                            continue;
                        else
                            goto notthis;
                    }
                    count++;
                    output.push_back(str.erase(0, min(str.find_first_not_of('0'), str.size()-1)));
                }else if (*x == "<str>")
                {
                    string str = input[count];
                    count++;
                    output.push_back(str);
                }else if (*x == "<path>")
                {
                    string str = "";
                    for( ; count < size; ++count)
                    {
                        str += input[count];
                        str += "/";
                    }
                    x = rule[j].end()-1;
                    if (!last)  str.erase(str.size()-1);
                    last = lastarr[j];
                    //cout << str << endl;
                    output.push_back(str);
                }else
                {
                    if (input[count] == *x) 
                        count++;
                    else 
                        goto notthis;
                }

            }

            // rule[j][rule[j].size()-1] != "<path>"            
            if (count < size || lastarr[j] != last) goto notthis;

            cout << value[j];
            for (auto x = output.begin(); x != output.end(); ++x)
                cout << " " << *x;
            cout << endl;
            flag = false;
            goto program_end;

            notthis:
            output.clear();
        }
        if (flag) cout << "404" << endl;
        program_end:
        output.clear(); 
    }

}