ccf(元素选择器)AC

201809-3
试题名称:元素选择器
时间限制:1.0s
内存限制:256.0MB
问题描述:




 

在考试的时候没有写出来...

后来一直很怕这道题,可是今天才发现我原来和解出这道题只差一点点.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string.h>
#include <stack>
#include <ctype.h>
#include <map>

using namespace std;

struct Node{
	int no;
	string label;
	string id;
};

//变成小写
string tolow(string s)
{
	for(int i = 0;i<s.length();i++)
	{
		s[i] = tolower(s[i]);
	}
	return s;
} 

//分割字符串,并且放在容器内
void split(char * s,vector<string>&v) 
{
	v.clear();
	char * p = strtok(s," ");
	while(p)
	{
		v.push_back(p);
		p = strtok(NULL," ");
	}
}


vector<string>demands;//命令 
map<string,int>an;//祖先的label和id,   以及命令中要求的段数 

int main() {
	//行数
	int n,m; 
	//变量 
	int i;
	char c;
	//放置数组
	vector<Node>nodes; 
	//读入行数 
	cin>>n>>m;
	getchar();
	for(i = 0;i<n;i++)
	{
		int flag = 0;
		int tno = 0;
		string tlabel = "";
		string tid = "";
		Node temp;
		while((c=getchar())!='\n')
		{
			if(c=='.')//no
			{
				tno++;
			}
			else if(c=='#')//id
			{
				cin>>tid;
				getchar();
				temp.id = c+tid;
				break;
			}
			else//label
			{
				tlabel+=c;
				while((c=getchar())!=' ')
				{
					if(c=='\n')
					{
						flag = 1;
						break;
					}
					tlabel+=c;
				}
				temp.label = tolow(tlabel);
			}
			if(flag) break;
		}
		temp.no = tno/2;
		nodes.push_back(temp);
		//cout<<temp.no<<" "<<temp.label<<" "<<temp.id<<endl;
	}
	//开始操作
	while(m--)
	{
		char d[100]; 
		vector<int>ans;//保存行数 
		gets(d);
		split(d,demands);
		//全部id变小写
		for(int i = 0;i<demands.size();i++)
		{
			if(demands[i][0]!='#') demands[i] = tolow(demands[i]);
		} 
		if(demands.size()==1)//如果只有一个的话直接可以匹配 
		{
			//匹配有没有相等的情况 
			for(int i =0;i<n;i++)
			{
				if(nodes[i].label == demands[0] ||nodes[i].id== demands[0])
				{
					ans.push_back(i);
				}
			}
		}
		//是后继选择器
		else
		{			
			for(int i = 0;i<n;i++)
			{
				int len = demands.size()-1;
				//找到第一个了 
				if(nodes[i].label == demands[len]||nodes[i].id== demands[len])
				{
					len--;
					for(int j = i-1;j>=0&&nodes[j].no<=nodes[i].no;j--)
					{
						if(nodes[j].no<nodes[i].no)
						{
							if(nodes[j].label == demands[len]||nodes[j].id == demands[len])
							{
								len--;
								if(len==-1) break;
							}
						}
					}
				}
				if(len==-1)
					ans.push_back(i);		
			}
		} 		
		cout<<ans.size()<<" ";
			for(int i = 0;i<ans.size();i++)
				cout<<ans[i]+1<<" ";
			cout<<endl;
	} 
	return 0;
}

/*
11 5
html
..head
....title
..body
....h1
....p #subtitle
....div #main
......h2
......p #one
......div
........p #two
div div p
p
h1 #subtitle
html
div p
*/

世界上最可怕的事情应该就是妄自菲薄了吧.

高估自己能力什么的,起码还是有干劲的.

AC了.


我有毒。

我今天又写了一遍。

我今天神志有点不清醒,写完发现我的代码离上次的差好远,一点也不优雅。

果然人家的代码结构超好的。

要向人家学习~

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Node
{
    int num;
    string label;
    string id;
};
int n,m;
vector<Node>nodes;
vector<string>re;


string toLower(string label)//为了把字符串变成小写,因为label对大小写不敏感
{
    for(int i = 0;i<label.length();i++)
    {
        if(label[i]>='A'&&label[i]<='Z')
        {
            int num = label[i]-'A';
            label[i] = num + 'a';
        }
    }
    return label;
}


void split(string t)   //分割字符串
{
    int i;
    int pos1 = 0,pos2 = 0;
    while((pos2 = t.find(' ',pos1))!=string::npos)
    {
        string s = t.substr(pos1,pos2-pos1);
         s = toLower(s);
        re.push_back(s);
        pos1 = pos2+1;
    }
    string s = t.substr(pos1,t.length()-pos1);
    if(s[0] != '#')    //label是大小写不敏感的
        s = toLower(s);
    re.push_back(s);
}


void reAns()
{
    int i,j,k;
    queue<int>st;
    for(i = 0;i<n;i++)
    {
        int flag = 0;//为1表示不匹配
        int in = 0;//为0表示最一个选择器就找不到匹配(如h3)
        string t1 = re[re.size()-1];//最后一个开始,然后找祖宗,倒序找
        if(t1 == nodes[i].label || t1 == nodes[i].id)
        {
            in = 1;
            int t_num = re.size()-2;
            for(j = i-1;j>=0;j--)
            {
                if(t_num<0)//找完了退出
                {
                    break;
                }
                t1 = re[t_num];
                if(nodes[j].num<=nodes[i].num)
                {
                    if(nodes[j].num<nodes[i].num && (t1==nodes[j].label || t1 == nodes[j].id))
                    {
                        t_num--;
                        continue;
                    }
                }
                else
                {
                    flag = 1;
                    break;
                }
            }
            if((j == -1&&t_num >=0) || !in)//全部规则都找遍了都没有找到,或者还有祖宗没有匹配.
            {
                flag = 1;
            }
            if(flag == 0)
                st.push(i+1);
        }
    }
    cout<<st.size()<<" ";
    while(!st.empty())
    {
        cout<<st.front()<<" ";
        st.pop();
    }
    cout<<endl;
}

int main()
{
    int i,j,k;
    string t;
    Node node;
    cin>>n>>m;
    getchar();
    for(i = 0; i<n; i++)
    {
        node.id = "";node.num = 0;node.label = "";
        while(getline(cin,t))
        {
            int t_i = 0;
            if(t[t_i]=='.')
            {
                while(t[++t_i]=='.');
                node.num = t_i/2;
            }
            //找id
            int pos;
            if( (pos = t.find(' ',t_i))!=string::npos)//说明有标签
            {
                node.label = toLower(t.substr(t_i,pos-t_i));
                pos = t.find('#',pos);
                node.id = t.substr(pos,t.length()-pos);
                nodes.push_back(node);
                break;
            }
            else
            {
                node.label = toLower(t.substr(t_i,t.length()-t_i));
                nodes.push_back(node);
                break;
            }
        }
    }
    for(i = 0;i<m;i++)
    {
        re.clear();
        getline(cin,t);
        split(t);
        reAns();
    }
    return 0;
}
/**
11 5
html
..head
....title
..Body
....h1
....p #subtitle
....div #main
......h2
......p #one
......div
........p #two
p
#Subtitle
h3
div p
div div p



3 1
..body
....p #subtitle
....h1

**/

解释一下:

因为我们是从下到上找的,先找最后一个选择器,如

div    div  p,那么先找到了p,我们希望是p的直系亲属匹配div,所以结构时这样的:

..

....

.......

...........

............

..............

而我们的p在最底层,如果是

.

.....

..

.........

那么就是有两层结构了,是两个家族.我们只在三角形里找,有平辈没有关系,但是祖宗不能有比自己长的.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值