Wrong Answer
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define endl '\n'
#define debug cout<<"HERE"<<endl;
#define ff first
#define ss second
void edm()
{
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void solve()
{
string s;cin>>s;
map<int,int>mp;
for(auto i:s)
{
mp[i-'a']++;
}
string ss = "";
int prev=-1;
for(int i=0;i<s.size();i++)
{
for(int j=0;j<26;j++)
{
int is=1;
if(mp[j] && j!=prev)
{
int now = ss.size() + 1;
int baki = s.size() - now;
int mx = (baki+1)/2;
for(int k=0;k<26;k++)
{
if(j==k)continue;
if(mp[k]==0)continue;
// cout<<k<<" "<<mp[k]<<" "<<mx<<endl;
if(mp[k]>mx)
{
// debug;
is=0;
break;
}
}
if(is)
{
char c = 'a' + j;
prev = j;
ss+=c;
mp[j]--;
}
}
}
// cout<<prev<<endl;
}
// cout<<ss<<endl;
if(ss.size()!=s.size())
{
cout<<-1<<endl;
return;
}
for(int i=0;i<ss.size()-1;i++)
{
if(ss[i]==ss[i+1])
{
cout<<-1<<endl;
return;
}
}
cout<<ss<<endl;
}
signed main()
{
//edm();
int t = 1;
cin>>t;
for(int i=1;i<=t;i++)
{
///cout<<"Case "<<i<<": ";
solve();
}
}
Information
- Submit By
- Type
- Submission
- Problem
- P1209 B. Rearrange the String
- Contest
- Educational Round 1
- Language
- C++17 (G++ 13.2.0)
- Submit At
- 2025-07-14 16:58:46
- Judged At
- 2025-07-14 16:58:46
- Judged By
- Score
- 0
- Total Time
- 2ms
- Peak Memory
- 532.0 KiB