解题思路

  1. 首先需要处理两个输入序列:原始序列I和规则序列R
  2. 对规则序列R进行排序和去重
  3. 对于R中的每个数字r:
    • 在I序列中查找包含r的所有数字
    • 记录这些数字在原序列中的位置
  4. 按照要求的格式输出结果:
    • 先输出总的匹配数量
    • 然后按R中数字的顺序输出匹配结果

代码

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;

bool contains(int num, int target) {
    string numStr = to_string(num);
    string targetStr = to_string(target);
    return numStr.find(targetStr) != string::npos;
}

int main() {
    int n, m;
    while (cin >> n) {
        vector<int> I(n);
        for (int i = 0; i < n; i++) {
            cin >> I[i];
        }
        
        cin >> m;
        vector<int> R(m);
        for (int i = 0; i < m; i++) {
            cin >> R[i];
        }
        
        // 对R进行排序和去重
        set<int> uniqueR(R.begin(), R.end());
        vector<int> sortedR(uniqueR.begin(), uniqueR.end());
        
        vector<int> result;
        // 处理每个规则数字
        for (int r : sortedR) {
            vector<pair<int, int>> matches;  // (位置, 值)
            for (int i = 0; i < n; i++) {
                if (contains(I[i], r)) {
                    matches.push_back({i, I[i]});
                }
            }
            
            if (!matches.empty()) {
                result.push_back(r);
                result.push_back(matches.size());
                for (auto& match : matches) {
                    result.push_back(match.first);
                    result.push_back(match.second);
                }
            }
        }
        
        // 输出结果
        cout << result.size() << " ";
        for (int i = 0; i < result.size(); i++) {
            cout << result[i];
            if (i < result.size() - 1) cout << " ";
        }
        cout << endl;
    }
    return 0;
}
import java.util.*;

public class Main {
    private static boolean contains(int num, int target) {
        return String.valueOf(num).contains(String.valueOf(target));
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int[] I = new int[n];
            for (int i = 0; i < n; i++) {
                I[i] = sc.nextInt();
            }
            
            int m = sc.nextInt();
            int[] R = new int[m];
            for (int i = 0; i < m; i++) {
                R[i] = sc.nextInt();
            }
            
            // 对R进行排序和去重
            TreeSet<Integer> uniqueR = new TreeSet<>();
            for (int r : R) uniqueR.add(r);
            
            List<Integer> result = new ArrayList<>();
            // 处理每个规则数字
            for (int r : uniqueR) {
                List<int[]> matches = new ArrayList<>();  // [位置, 值]
                for (int i = 0; i < n; i++) {
                    if (contains(I[i], r)) {
                        matches.add(new int[]{i, I[i]});
                    }
                }
                
                if (!matches.isEmpty()) {
                    result.add(r);
                    result.add(matches.size());
                    for (int[] match : matches) {
                        result.add(match[0]);
                        result.add(match[1]);
                    }
                }
            }
            
            // 输出结果
            System.out.print(result.size());
            for (int val : result) {
                System.out.print(" " + val);
            }
            System.out.println();
        }
    }
}
def contains(num, target):
    """检查数字num是否包含target作为子串"""
    num_str = str(num)
    target_str = str(target)
    return target_str in num_str

while True:
    try:
        # 读取序列I
        I = list(map(int, input().split()))
        n = I[0]
        I = I[1:]
        
        # 读取序列R
        R = list(map(int, input().split()))
        m = R[0]
        R = R[1:]
        
        # 对R进行排序和去重
        unique_R = sorted(set(R))
        
        result = []
        # 处理每个规则数字
        for r in unique_R:
            matches = []  # [(位置, 值)]
            for i in range(len(I)):
                if contains(I[i], r):
                    matches.append((i, I[i]))
            
            if matches:
                result.append(r)
                result.append(len(matches))
                for pos, val in matches:
                    result.append(pos)
                    result.append(val)
        
        # 输出结果
        print(len(result), end='')
        for i in range(len(result)):
            print(f" {result[i]}", end='')
        print()
            
    except EOFError:
        break

算法及复杂度

  • 算法:字符串匹配 + 排序
  • 时间复杂度:,其中n是序列I的长度,m是序列R的长度,k是数字转字符串后的平均长度
  • 空间复杂度:,用于存储结果和临时数据