1. 程式人生 > >CCF-20180903 元素選擇器

CCF-20180903 元素選擇器

CCF-20180903 元素選擇器

題意: 找出給定html文件的對應元素所在位置。
分析: 字串問題大模擬, CCF經典題。
程式碼: 這道題只得了80分, 還在找bug。

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 111;
struct Item //html文件
{
    int dot;
    string text;
    string id;
} item[MAXN];

int fa[MAXN]; //當前元素的父親元素所在位置

struct Option //操作
{
int len, id; string op[11]; } option[MAXN]; string arr[MAXN]; vector<int> vi[MAXN]; //儲存結果 int main () { int n, m; cin >> n >> m; getchar(); for (int i = 0; i < n; i++) { getline(cin, arr[i]); } //solve //預處理HTML文件 memset (fa, -1, sizeof
(fa)); for (int i = 0; i < n; i++) { int dot = 0; string tmp = ""; string tmpid = ""; bool flag = true; for (int j = 0; j < arr[i].length (); j++) { if (arr[i][j] == '.') { dot++; continue
; } if (arr[i][j] == ' ') { flag = false; continue; } if (flag) tmp += arr[i][j]; else tmpid += arr[i][j]; } item[i].text = tmp; item[i].id = tmpid; item[i].dot = dot; for (int k = 0; k < item[i].text.length(); k++) { if (isupper(item[i].text[k])) item[i].text[k] = tolower(item[i].text[k]); } for (int j = i; j >= 0; j--) { if (item[i].dot == item[j].dot + 2) { fa[i] = j; break; } } } /* for (int i = 0; i < n; i++) { cout <<fa[i] << " " << item[i].dot << " " << item[i].text << " " << item[i].id << endl; } */ //預處理操作 for (int i = 0; i < m; i++) { int sz = 0; string s; getline (cin, s); string tmp = ""; for (int j = 0; j < s.length (); j++) { if (s[j] == ' ') { option[i].op[sz] = tmp; sz++; tmp = ""; } else { tmp += s[j]; } } option[i].op[sz] = tmp; option[i].len = sz; option[i].id = i; } for (int i = 0; i < m; i++) { for (int j = 0; j <= option[i].len; j++) { if (option[i].op[j][0] != '#') { for (int k = 0; k < option[i].op[j].length(); k++) { if (isupper(option[i].op[j][k])) option[i].op[j][k] = tolower(option[i].op[j][k]); } } } } /* for (int i = 0; i < m; i++) { for (int j = 0; j <= option[i].len; j++) { cout << option[i].op[j] << " "; } cout << endl; } */ //查詢匹配 for (int k = 0; k < m; k++){ int sz = option[k].len; for (int i = 0; i < n; i++) { int j1, j2; for (j1 = i, j2 = sz; j1 != -1&&j2 >= 0; j1 = fa[j1], j2--) { // cout <<j1 << " " << j2 <<" "<< option[k].op[j2] << " " << item[j1].text << " " << item[j1].id << endl; if (option[k].op[j2].compare(item[j1].text) == 0 || option[k].op[j2].compare(item[j1].id) == 0) { } else { break; } } if (j2 == -1) { vi[k].push_back(i + 1); } } } for (int i = 0; i < m; i++) { cout << vi[i].size(); for (int j = 0; j < vi[i].size(); ++j) { cout << " " << vi[i][j]; } cout << endl; } return 0; }