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
*/
在考试的时候没有写出来...
后来一直很怕这道题,可是今天才发现我原来和解出这道题只差一点点.
#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了.