826某飞笔试

第一题

小红定义了一个正整数x为完美偶数满足以下两个条件:

  1. x是偶数
  2. x不小于l,不大于r
    小红拿到了一个长度为m的数组a,她想直到这恶数组里面有多少完美偶数

输入描述
第一行输入三个正整数n, l, r
第二行输入n个正整数,代表小红拿到的数组
1 ≤ n, a_i ≤ 100
1 ≤ l ≤ r ≤ 100
输出表示
该数组总完美偶数的数量

示例1:
输入
5 3 8
1 2 6 8 7

输出
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;
}

第二题

找两个字符串的相似度
相似度定义如下

  1. 两个字符串长度形同,相似度+1
  2. 使用了同样的字符集,相似度+1

输入描述
第一行输入一个正整数t代表询问次数
队医每次询问,输入两个字符串
1 ≤ t ≤ 10

输出描述:
给出t行每行一个整数,代表相似度

示例
输入
3
abcd
1234
abc.
S?AD
aha
a ha

输出
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;
}

小红拿到了一个字符串,其中有一些字符变成了“?”。小红希望你将所有的"?'替换成任意一个数字字符,使得最终字符串代表的十进制整数是p的倍数。请你告诉小红最终有多少种方案。
注:可以包含前导零。
由于答案过大,请对10^9-+7取模。
输入描述
第一行输入一个仅由数字和"?"组成的字符串。
第二行输入一个正整数p.
字符串长度不超过100;
1≤p≤10^4
输出描述
—个整数,代表方案数对10^9 +7取模的值,
示例1
输入
??
1
输出
100
说明修改为00-99均可
示例2
输入
???1
12
输出
0
说明
无论怎么修改都是奇数,不可能是2的倍数

主要的步骤如下:

  • 定义一个递归函数,用来计算给定字符串和模数 p 的方案数,该函数有四个参数:s 表示字符串,p 表示模数,index 表示当前遍历到的字符的下标,remainder 表示当前的余数。
  • 如果字符串遍历完毕,判断余数是否为 0,如果是,则说明这是一个符合条件的方案,返回 1,否则返回 0。
  • 如果当前字符不是 ‘?’,则直接计算余数并递归下一个字符,余数的计算方法是将当前余数乘以 10 再加上当前字符代表的数字,然后对 p 取模。
  • 如果当前字符是 ‘?’,则尝试所有可能的数字字符,并累加方案数,每次累加后对 MOD 取模,以防止溢出。同时,在每次尝试后要恢复余数的值,以便下一次尝试。
  • 在主函数中,输入字符串和模数 p,并调用递归函数,从第 0 个字符和余数为 0 开始计算方案数,并输出结果。
#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;
}

回溯
逆向遍历字符串,并维护一个数组 total_ways,其中 total_ways[i] 表示余数为 i 时的方案数。然后根据字符是否为 “?”,分别计算累积的余数,并更新方案数。最后返回 total_ways[0],即余数为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;
}

猜你喜欢

转载自blog.csdn.net/weixin_47895938/article/details/132511739
今日推荐