H
H
Homemade2021-12-23 20:20:00
C++ / C#
Homemade, 2021-12-23 20:20:00

What is my mistake in the task?

Task

My decision
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
 
int main()
{
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        char c;
        string s;
        cin >> n >> c >> s;
        string b = s;
        sort(b.begin(), b.end());
        if (b[0] == b[b.size() - 1]) {
            cout << "0" << '\n';
            continue;
        }
        vector<bool>prime(n + 1, true);
        prime[0] = prime[1] = false;
        for (int i = 2; i * i <= n; i++) {
            if (prime[i]) {
                for (int j = i * i; j <= n; j += i)
                    prime[j] = false;
            }
        }
        int ans = 0;
        for (int i = n; i >= 0; i--) {
            if (prime[i]) {
                ans = i;
                break;
            }
        }
        if (s[ans - 1] == c) {
            cout << "1" << '\n' << ans << '\n';
        }
        else {
            cout << "2" << '\n';
            if (ans < n)
                cout << ans << " " << ans + 1 << '\n';
            else
                cout << ans - 1 << " " << ans << '\n';
        }
        
    }
 
    return 0;
}


The readability of the code is bad, so I'll write my algorithm here.
First, we check the case when all letters in the string are equal to the character c. Then, with the help of Eratosthenes, we find the largest prime number ans in the interval [1,n], since no number from this range is divisible by it except itself. Then we check s[ans-1]==c, if so then the answer is only ans, otherwise ans, ans+-1.

Where is my error (in the algorithm, in the code, or both)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-12-23
@Homemade

You have the right idea that two numbers in the answer is always enough. You can always give, for example, n, n-1 and these two operations will cover all positions from 1 to n.
But here is the case when one number is enough, you misunderstood. Why are you looking for the largest prime number? What's the point of looking at what's in position ans-1 is c?
Also, your code outputs 0 incorrectly sometimes.
Maybe you misunderstood the problem? You need to make sure that all positions in which characters other than c are not divisible by at least one number in your answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question