Skip to content

Pro3_correct

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

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>

using namespace std;

vector<string> input;
map<string,int> id;
vector<int> depth;

struct iequal
{
    bool operator()(int c1, int c2) const
    {
        return std::toupper(c1) == std::toupper(c2);
    }
};

bool iequals(const std::string& str1, const std::string& str2)
{
    return std::equal(str1.begin(), str1.end(), str2.begin(), iequal());
}

int main()
{
    /* ifstream srcFile("time14.txt",ios::in); */
    int n,m;
    cin >> n >> m;
    cin.get();

    string str;
    for(int i=0;i<n;++i)
    {
        getline(cin,str);

        int len = str.size();
        int pos = str.find("#");
        int index = 0;
        if(-1 != pos)
        {
            id.insert(make_pair(str.substr(pos,len),i));
            len = pos;
        }
        while(str[index]=='.')  index++;
        str = str.substr(index,len-index);
        input.push_back(str);
        depth.push_back(index/2);
    }

    vector<int> candidate;
    vector<bool> flag;
    for(int i=0;i<m;++i)
    {
        candidate.clear();
        flag.clear();
        for(int i=0;i<n;i++) flag.push_back(true);

        getline(cin,str);

        int pos = str.find(' ');
        if(str[0]=='#' && -1==pos)
        {
            if(id.count(str) > 0)
                cout << 1 << " " << id[str]+1 << endl;
            else
                cout << 0 << endl;
        }else if(-1 == pos)
        {
            int number = 0;
            for(int j=0;j<n;++j)
            {
                /* cout << str << "----" << input[j] << endl; */
                if(iequals(str,input[j]))
                {
                    number++;
                    candidate.push_back(j);
                }
            }

            cout << number;
            for(int j=0;j<number;++j)
            {
                flag[candidate[j]] = false;
                cout << " " << candidate[j]+1;
            }
            cout << endl;
        }else
        {
            vector<string> str_sub;
            while(-1 != pos)
            {
                str_sub.push_back(str.substr(0,pos));
                str = str.substr(pos+1,80);
                pos = str.find(' ');
            }
            str_sub.push_back(str);


            str = str_sub[0];
            if(str[0] == '#')
            {
                if(id.count(str) > 0)
                    candidate.push_back(id[str]);
            }else
            {
                for(int j=0;j<n;++j)
                {
                    if(iequals(str,input[j]))
                    {
                        candidate.push_back(j);
                    }
                }
            }

            int size = str_sub.size();
            vector<int> output;
            for(int index=1;index<size;++index)
            {
                int number = candidate.size();
                string str1 = str_sub[index];

                for(int j=0;j<number;++j)
                {
                    int line = candidate[j];
                    if(str1[0]=='#')
                    {
                        if(id.count(str1) > 0 && depth[id[str1]] > depth[line])
                        {
                            output.push_back(id[str1]);
                            break;
                        }
                    }else
                    {
                        for(int k=0;k<n;++k)
                        {
                            if(depth[line] < depth[k] && iequals(str1,input[k]))
                            {
                                if(!flag[k]) continue;
                                output.push_back(k);
                                flag[k] = false;
                            }
                        }
                    }
                }

                candidate.clear();
                for(auto x:output) 
                {
                    candidate.push_back(x);
                    flag[x] = true;
                }
                output.clear();
            }

            int number = candidate.size();

            cout << number;
            for(int j=0;j<number;++j)
                cout << " " << candidate[j]+1;
            cout << endl;
        }
    }

    return 0;
}