当前位置: 代码迷 >> 综合 >> CF1562B Scenes From a Memory(枚举,思维)
  详细解决方案

CF1562B Scenes From a Memory(枚举,思维)

热度:64   发布时间:2023-10-13 23:55:36.0

原题链接

题意

给一个字符串,删去任意多位,使剩下来的那个数尽可能小,且是个合数或 1。
输出剩下那个数的位数和那个数(2行)。

思路

通过举例子可以发现,剩下的合数最大只有 2 位

  1. 只要有 1, 4, 6, 8, 9,这中间的数,肯定是输出这一位
  2. 剩下的数 2, 3, 5, 7,可以组成 22,25,27,32,33,35,52,55,57,72,75,77
  3. 可以发现,如果满足上面两个条件的任意其一,就肯定凑不出一个 3 位数是质数,而且题目保证一定有解,那就再字符串中找有没有上面的数出现过即可,注意要找最小的。

代码

#include<bits/stdc++.h> 
using namespace std;int main()
{
    int t;cin >> t;while (t -- ){
    int n; cin >> n;string s; cin >> s;map<char, int> ma;for (int i = 0; i < s.length(); i ++ ){
    ma[s[i]] ++;}if (ma['1'] != 0) {
    cout << 1 << endl << "1" << endl;continue;}else if (ma['4'] != 0) {
    cout << 1 << endl << "4" << endl;continue;}else if (ma['6'] != 0) {
    cout << 1 << endl << "6" << endl;continue;}else if (ma['8'] != 0) {
    cout << 1 << endl << "8" << endl;continue;}else if (ma['9'] != 0) {
    cout << 1 << endl << "9" << endl;continue;}bool f = 0;ma.clear();string minn = "zz";cout << "2" << endl;for (int i = 0; i < s.length(); i ++ ){
    if (ma[s[i]] != 0){
    string aa = "";aa += s[i];aa += s[i];minn = min(aa, minn);}if (s[i] == '5' && ma['2'] != 0){
    string aa = "25";minn = min(minn, aa);}if (s[i] == '7' && ma['2'] != 0){
    string aa = "27";minn = min(minn, aa);}if (s[i] == '2' && ma['3'] != 0){
    string aa = "32";minn = min(minn, aa);}if (s[i] == '5' && ma['3'] != 0){
    string aa = "35";minn = min(minn, aa);}if (s[i] == '2' && ma['5'] != 0){
    string aa = "52";minn = min(minn, aa);}if (s[i] == '7' && ma['5'] != 0){
    string aa = "57";minn = min(minn, aa);}if (s[i] == '2' && ma['7'] != 0){
    string aa = "72";minn = min(minn, aa);}if (s[i] == '5' && ma['7'] != 0){
    string aa = "75";minn = min(minn, aa);}ma[s[i]] ++;}cout << minn << endl;}return 0;
}

总结

是一道简单的水体,可是因为我的懒惰,居然因为它可能有那么一点点麻烦,一直没有写,导致整个构造题单的进度停止,是我的罪。

  相关解决方案