Блог пользователя -200

Автор -200, история, 6 часов назад, По-английски

Hii! everyone,

I wrote a solution for this problem: https://codeforces.net/gym/105418/problem/B. The issue is that while my output is correct, I'm getting a verdict of "WA on testcase 1." However, there's no explanation (no jury answer provided) for why I'm getting the wrong answer.

Can you please help me identify what's going wrong?

Here's my code:

Your code here...

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ul unsigned long long 
#define int long long 

#define init ios_base::sync_with_stdio(false); cin.tie(NULL);
#define no cout << "NO\n"
#define yes cout << "YES\n"
#define mod 1000000007
#define endd cout << "\n"
#define endl "\n"
#define came cout<<"came\n";

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; __print(x); cerr << endl;
#else 
#define debug(x)
#endif

template<class T>
void in(T a[],int n){for(int i=0;i<n;i++) cin>>a[i];}
template<class T> 
void out(T a[], int x){for(int i = 0; i < x; i++){cout << a[i] << " "; } cout << endl;}
template<class T> 
void in(vector<T>& a, int n){for(int i = 0; i < n; i++){ int x; cin >> x; a.push_back(x);}}
template<class T>
void out(vector<T> a){for(auto i : a)cout << i << " ";cout << endl;}
inline int fib(int n) {const double phi = (1 + sqrt(5)) / 2;const double psi = (1 - sqrt(5)) / 2;return round((pow(phi, n) - pow(psi, n)) / sqrt(5));}
inline int gcd(int A, int B) {if (B == 0) return A;return gcd(B, A % B);}
inline int lcm(int A, int B) {return ((A * B) / (gcd(A, B)));}

inline bool isprime(int X) {if(X==1) return 0;if (X == 2) return true;for (int i = 2; i <= sqrt(X); i++) {if ((X % i) == 0)return false;}return true;}
int Mod(string &a,int k){int rem=0;for(int i=0;i<a.size();i++){rem = (rem*10+(a[i]-'0'))%k;}return rem;}
signed main() {
    init;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);  
    #endif

    ll _ = 1;
    cin >> _;
    while(_--){
       string s;
       cin >> s;
       int n = s.size();
       vector<int> mp(26,0);
       
       for(int i=0; i<n; i++) {
           mp[s[i]-'a']++;
       }

       vector<string> o_char;

       for(int i=0; i<26; i++){
           if(mp[i] & 1){
               string tp;
               tp.push_back((char)(i+'a'));
               mp[i]--;
               o_char.push_back(tp);
           }
       }

       if(o_char.size() == 0) {
           sort(s.begin(), s.end());
           cout << "2 \n" << s << endl;
       } else {
           if(o_char.size() > 1 && (n - o_char.size()) % o_char.size() == 0) {
               int i = 0;
               while(i < 26 && mp[i] <= 0) i++;
               if(i < 26 && mp[i] > 0) {
                   for(int j = 0; i < 26; j++) {
                       while(i < 26 && mp[i] <= 0) i++;
                       if(i < 26 && mp[i] > 0) {
                           o_char[j % o_char.size()] = ((char)(i+'a') + o_char[j % o_char.size()] + (char)(i+'a'));
                           mp[i] -= 2;
                       }
                   }
               }
               cout << o_char.back().size() << endl;
               for(auto h : o_char) cout << h;
               cout << "\n";
           } else {
               cout << -1 << endl;
           }
       }
    }
}

Let me know if you spot any issues or need more information.

  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

»
4 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Sir, learn to debug

  • »
    »
    4 часа назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Yeah I debugged the code and I'm getting expected output but when I submitted my solution getting WA

»
4 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

The checker for this problem is stricter than usual Codeforces problems and appears to care about whitespace. Remove the extra space from this line:

cout << "2 \n" << s << endl;

(And then you'll get WA on test 2, but at least you're not failing on the sample anymore.)

  • »
    »
    4 часа назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    I got a wrong answer on test case 2, as you said. Sir, can you give a hint for that problem?

    • »
      »
      »
      67 минут назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I don't have time to really delve into the problem and your solution.

      But here is one thing; on this test case:

      1
      aa
      

      your code prints

      2
      aa
      

      But the answer should be -1 because aa doesn't contain a palindrome of length 2 twice.

»
92 минуты назад, # |
  Проголосовать: нравится -6 Проголосовать: не нравится

Try to send on another version of C++