主要思路:vector接收I数据,set接收R数据(自动去重排序)
中间步骤中使用了stringstream实现string与int转换(不熟悉使用可查)

#include<bits/stdc++.h>
using namespace std;
int n1, n2;
int main()
{
    while(scanf("%d", &n1) != EOF) {
        vector<string> I;
        set<int> R;
        vector<vector<string> > ans;
        string s;
        for(int i = 0; i < n1; i++) {
            cin >> s;
            I.push_back(s);
        }
        cin >> n2;
        for(int i = 0; i < n2; i++) {
            int a;
            cin >> a;
            R.insert(a);
        }
        int num = 0;
        for(auto it : R) {
            vector<string> temp;
            int tmp = it;
            stringstream sss;
            sss << it;
            string tmp1;
            sss >> tmp1;
            temp.push_back(tmp1);
            for(int j = 0; j < I.size(); j++) {
                if(I[j].find(tmp1) != string::npos) {
                    //cout << "index :" << j << " ";
                    stringstream ss;
                    ss << j;
                    string stemp;
                    ss >> stemp;
                    //cout << "string:"<<stemp;
                    temp.push_back(stemp);
                    temp.push_back(I[j]);
                    num += 2;
                }
            }
            if(temp.size() > 1) ans.push_back(temp);
        }
        num += ans.size() * 2;
        printf("%d ", num);
        for(int i = 0; i < ans.size(); i++) {
            cout << ans[i][0] << " " << (ans[i].size() - 1) / 2 << " ";
            for(int j = 1; j < ans[i].size(); j++) {
                cout << ans[i][j] << " ";
            }
        }
        cout << endl;
    }
    return 0;
}