华为机试:数据分类处理

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>

using namespace std;

bool match (string str1, string str2) {
    if (str2.length() < str1.length()) return false;
    for (int i = 0; i <= str2.length() - str1.length(); i++) {
        if (str1 == str2.substr(i, str1.length())) {
            return true;
        }
    }
    return false;
}

int main() {
    // int a, b;
    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }
    vector<string> I;
    set<int> R;
    int N;
    cin >> N;
    while (N--) {
        string str;
        cin >> str;
        I.push_back(str);
    }
    cin >> N;
    while (N--) {
        int M;
        cin >> M;
        R.insert(M);
    }
    vector<string> ans;
    for (auto it = R.begin(); it != R.end(); it++) {
        map<int, string> m;
        for (int j = 0; j < I.size(); j++) {
            if (match(to_string(*it), I[j])) {
                m.insert(make_pair(j, I[j]));
            }
        }
        if (!m.empty()) {
            ans.push_back(to_string(*it));
            ans.push_back(to_string(m.size()));
            for (auto p = m.begin(); p != m.end(); p++) {
                ans.push_back(to_string(p->first));
                ans.push_back(p->second);
            }
        }
    }
    cout << ans.size() << " ";
    for (int i = 0; i < ans.size() - 1; i++) {
        cout << ans[i] << " ";
    }
    cout << ans[ans.size() - 1] << endl;
    return 0;
}

全部评论

相关推荐

牛油果甜奶昔:别的先不说,牛客还能内推护士?
点赞 评论 收藏
分享
评论
点赞
7
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务