826 A Fly Written Exam

first question

Xiaohong defines a positive integer x as a perfect even number satisfying the following two conditions:

  1. x is an even number
  2. x is not less than l, not greater than r
    Xiaohong got an array a of length m, she wants to know how many perfect even numbers there are in this evil array

Input description
The first line inputs three positive integers n, l, r The second line inputs n positive integers, representing the array 1 ≤ n, a_i ≤ 100 1 ≤ l ≤ r ≤ 100 that
Xiaohong got. The output indicates the total number of the array number of perfect even numbers



Example 1:
Enter
5 3 8
1 2 6 8 7

output
2

#include <iostream>
#include <vector>

int main() {
    
    
    int n, l, r;
    std::cin >> n >> l >> r;
	
	int num;
	int res = 0;
	for(auto i : n){
    
    
		std::cin >> num;
		if ((num % 2 == 0) && (num >= l && num <= r)) {
    
    
            res++;
        }
	}
	
	std::cout << res << std::endl;
  
    return 0;
}

second question

Find the similarity between two strings.
Similarity is defined as follows

  1. Two strings have the same length, similarity +1
  2. Using the same character set, similarity +1

Input description
In the first line, enter a positive integer t to represent the number of inquiries.
Each time the team doctor asks, enter two strings
1 ≤ t ≤ 10

Output description:
Give an integer in each row of t rows, representing the similarity

Example
Enter
3
abcd
1234
abc.
S?AD
aha
a ha

output
1
2
0

#include <iostream>
#include <unordered_map>
using namespace std;

int calculateSimilarity(string str1, string str2) {
    
    
    int similarity = 0;

    if (str1.length() == str2.length()) {
    
    
        similarity++;
    }

    unordered_map<char,int> charSet1,charSet2;
    charSet1.insert(make_pair('0',0));
    charSet1.insert(make_pair('a',0));
    charSet1.insert(make_pair('o',0));
    charSet2.insert(make_pair('0',0));
    charSet2.insert(make_pair('a',0));
    charSet2.insert(make_pair('o',0));
    for (char c : str1) {
    
    
        if (isdigit(c)) {
    
    
            charSet1['0']++;
        } else if (isalpha(c)) {
    
    
            charSet1['a']++;
        } else {
    
    
            charSet1['o']++;
        }
    }

    for (char c : str2) {
    
    
        if (isdigit(c)) {
    
    
            charSet2['0']++;
        } else if (isalpha(c)) {
    
    
            charSet2['a']++;
        } else {
    
    
            charSet2['o']++;
        }
    }

    if (charSet1['0'] == charSet2['0'] && charSet1['a'] == charSet2['a'] && charSet1['o'] == charSet2['o']) {
    
    
        similarity++;
    }

    return similarity;
}

int main() {
    
    
    int t;
    cin >> t;

    while (t--) {
    
    
        string str1, str2;
        cin >> str1 >> str2;
        
        int similarity = calculateSimilarity(str1, str2);
        cout << similarity << endl;
    }

    return 0;
}

Xiaohong got a string, some of which became "?". Xiaohong wants you to replace all "?'s with any number character, so that the decimal integer represented by the final string is a multiple of p. Please tell Xiaohong how many solutions there are in the end. Note: leading zeros can be included. Thanks to
the
answer If it is too large, please take the modulus of 10^9-+7.
Input description
Enter a string consisting of numbers and "?" in the first line.
Enter a positive integer p in the second line.
The length of the string does not exceed 100;
1 ≤p≤10^4 output
description — an integer , representing the value of the number of
schemes modulo 10^9 +7 ,
example 1
input
?? Output 0 means no matter how it is modified, it is an odd number and cannot be a multiple of 2











The main steps are as follows:

  • Define a recursive function to calculate the number of solutions for a given string and modulus p. This function has four parameters: s represents the string, p represents the modulus, index represents the subscript of the currently traversed character, and remainder represents The current remainder.
  • After traversing the string, judge whether the remainder is 0, if yes, it means that this is a qualified solution, return 1, otherwise return 0.
  • If the current character is not '?', directly calculate the remainder and recurse to the next character. The method of calculating the remainder is to multiply the current remainder by 10, add the number represented by the current character, and then take the modulus of p.
  • If the current character is '?', try all possible numeric characters and accumulate the number of schemes, and take the modulo of MOD after each accumulation to prevent overflow. At the same time, the value of the remainder is restored after each attempt for the next attempt.
  • In the main function, input the string and the modulus p, and call the recursive function, calculate the number of schemes from the 0th character and the remainder is 0, and output the result.
#include <iostream>
#include <string>
using namespace std;

// 定义一个常量 MOD 为 10^9 + 7
const int MOD = 1000000007;

// 定义一个递归函数,用来计算给定字符串和模数 p 的方案数
int countWays(string s, int p, int index, int remainder) {
    
    
    // 如果字符串遍历完毕,判断余数是否为 0
    if (index == s.size()) {
    
    
        return remainder == 0 ? 1 : 0;
    }
    // 如果当前字符不是 '?',则直接计算余数并递归下一个字符
    if (s[index] != '?') {
    
    
        remainder = (remainder * 10 + (s[index] - '0')) % p;
        return countWays(s, p, index + 1, remainder);
    }
    // 如果当前字符是 '?',则尝试所有可能的数字字符,并累加方案数
    int ans = 0;
    for (char c = '0'; c <= '9'; c++) {
    
    
        remainder = (remainder * 10 + (c - '0')) % p;
        ans += countWays(s, p, index + 1, remainder);
        ans %= MOD; // 每次累加后取模
        remainder = (remainder - (c - '0') + p) % p; // 恢复余数
    }
    return ans;
}

int main() {
    
    
    // 输入字符串和模数 p
    string s;
    int p;
    cin >> s >> p;
    // 调用递归函数,从第 0 个字符和余数为 0 开始计算方案数
    int ans = countWays(s, p, 0, 0);
    // 输出结果
    cout << ans << endl;
    return 0;
}

Backtracking
Traverse the string in reverse, and maintain an array total_ways, where total_ways[i] represents the number of solutions when the remainder is i. Then according to whether the character is "?", the accumulated remainder is calculated respectively, and the number of schemes is updated. Finally, total_ways[0] is returned, that is, the number of schemes with a remainder of 0

#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int MOD = 1000000007;

int countModularMultiples(const string& s, int p, int idx, int cur_mod, vector<vector<int>>& dp) {
    
    
    if (idx == s.size()) {
    
    
        return (cur_mod == 0) ? 1 : 0;
    }
    
    if (dp[idx][cur_mod] != -1) {
    
    
        return dp[idx][cur_mod];
    }
    
    int ways = 0;
    
    if (s[idx] != '?') {
    
    
        int digit = s[idx] - '0';
        ways = countModularMultiples(s, p, idx + 1, (cur_mod * 10 + digit) % p, dp);
    } else {
    
    
        for (int digit = 0; digit <= 9; ++digit) {
    
    
            ways = (ways + countModularMultiples(s, p, idx + 1, (cur_mod * 10 + digit) % p, dp)) % MOD;
        }
    }
    
    dp[idx][cur_mod] = ways;
    return ways;
}

int main() {
    
    
    string s;
    int p;
    cin >> s >> p;
    
    vector<vector<int>> dp(s.size(), vector<int>(p, -1));
    
    int result = countModularMultiples(s, p, 0, 0, dp);
    cout << result << endl;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_47895938/article/details/132511739