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
*/
在考試的時候沒有寫出來...
後來一直很怕這道題,可是今天才發現我原來和解出這道題只差一點點.
#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了.