1 条题解

  • 0
    @ 2025-7-14 18:12:56

    方法思路

    根据题目分析,我们需要找出字符串A中所有长度为n的子串,判断它们与字符串B异或后是否满足条件(所有位异或结果为0)。

    关键点:

    1. 如果异或结果中1的个数为偶数,则所有位异或的结果为0(因为偶数个1异或结果为0)
    2. 只有当A的子串与B对应位不同时,异或结果才为1
    3. 需要统计不同的合法子串数量,因此要用哈希表去重

    具体步骤:

    1. 遍历字符串A中所有长度为n的子串
    2. 对于每个子串,计算它与B异或后结果中1的个数
    3. 如果1的个数为偶数,则该子串是合法的
    4. 使用哈希表记录所有合法的子串,以避免重复计数
    5. 最后返回哈希表的大小,即不同的合法子串数量

    代码实现

    Java
    import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int m = scanner.nextInt();
            int n = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            
            String A = scanner.nextLine();
            String B = scanner.nextLine();
            
            System.out.println(solve(A, B, m, n));
            scanner.close();
        }
        
        public static int solve(String A, String B, int m, int n) {
            // 存储合法的子串
            Set<String> validSubstrings = new HashSet<>();
            
            // 遍历A中所有长度为n的子串
            for (int i = 0; i <= m - n; i++) {
                String substring = A.substring(i, i + n);
                
                // 计算子串与B异或后1的个数
                int countOnes = 0;
                for (int j = 0; j < n; j++) {
                    // 当A子串与B对应位不同时,异或结果为1
                    if (substring.charAt(j) != B.charAt(j)) {
                        countOnes++;
                    }
                }
                
                // 如果1的个数为偶数,则异或和为0
                if (countOnes % 2 == 0) {
                    validSubstrings.add(substring);
                }
            }
            
            return validSubstrings.size();
        }
    }
    
    
    Python
    def solve():
        m, n = map(int, input().split())
        A = input().strip()
        B = input().strip()
        
        # 存储合法的子串
        valid_substrings = set()
        
        # 遍历A中所有长度为n的子串
        for i in range(m - n + 1):
            substring = A[i:i+n]
            
            # 计算子串与B异或后1的个数
            count_ones = 0
            for j in range(n):
                # 当A子串与B对应位不同时,异或结果为1
                if substring[j] != B[j]:
                    count_ones += 1
            
            # 如果1的个数为偶数,则异或和为0
            if count_ones % 2 == 0:
                valid_substrings.add(substring)
        
        return len(valid_substrings)
    
    if __name__ == "__main__":
        print(solve())
    
    
    C++
    #include <iostream>
    #include <string>
    #include <unordered_set>
    using namespace std;
    
    int solve(const string& A, const string& B, int m, int n) {
        // 存储合法的子串
        unordered_set<string> validSubstrings;
        
        // 遍历A中所有长度为n的子串
        for (int i = 0; i <= m - n; i++) {
            string substring = A.substr(i, n);
            
            // 计算子串与B异或后1的个数
            int countOnes = 0;
            for (int j = 0; j < n; j++) {
                // 当A子串与B对应位不同时,异或结果为1
                if (substring[j] != B[j]) {
                    countOnes++;
                }
            }
            
            // 如果1的个数为偶数,则异或和为0
            if (countOnes % 2 == 0) {
                validSubstrings.insert(substring);
            }
        }
        
        return validSubstrings.size();
    }
    
    int main() {
        int m, n;
        cin >> m >> n;
        
        string A, B;
        cin >> A >> B;
        
        cout << solve(A, B, m, n) << endl;
        
        return 0;
    }
    
    
    • 1

    信息

    ID
    71
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    1
    已通过
    1
    上传者