1215C - Swap Letters - CodeForces Solution


constructive algorithms greedy *1500

Please click on ads to support us..

Python Code:

n=int(input())s=input()
t=input()
l,r=[],[]
for i in range(n):
    if s[i]+t[i]=='ba':
        r.append(i+1)
    if s[i]+t[i]=='ab':
        l.append(i+1)
p=len(l)+len(r)
if p%2==0:
    if len(l)%2==0:
        print(p//2)
        r=r+l
        for i in range(0,p,2):
            print(r[i],r[i+1])
    else:
        print(p//2+1)
        print(r[0],r[0])
        print(l[0],r[0])
        l=l[1:]+r[1:]
        for i in range(0,p-2,2):
            print(l[i],l[i+1])
else:
    print(-1)

C++ Code:

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll; 
typedef vector<ll> vl;
typedef set<ll> sl;
const int mod = (int)1e9 + 7;
#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } 
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
/*
Code to set Precision
std::cout << std::fixed;
std::cout << std::setprecision(6);// value after decimal
std::cout << (double)vk[i] << std::endl;
*/
/* struct Graph
{
	vector<vector<int>> adj;
	Graph(int n)
	{
		adj.resize(n+1);
	}	
	void add_edge(int a, int b, bool directed = false)
	{
		adj[a].pb(b);
		if(!directed) adj[b].pb(a);
	}
}; */
void solve(){
    ll n;cin>>n;
    string a, b; cin>>a>>b;

    ll abyb=0, bbya=0;

    ll counta=0; ll countb=0;


    //dbg(a); dbg(b);
    set<ll> posabyb;
    set<ll> posbbya;

    for(ll i=0; i<n; i++){


        if(a[i]=='a'){counta++;}
        if(b[i]=='a'){counta++;}


        if(a[i]=='b'){countb++;}
        if(b[i]=='b'){countb++;}


        if(a[i]=='a'&&b[i]=='b'){
            abyb++;
            posabyb.insert(i+1);
        }

        if(a[i]=='b'&&b[i]=='a'){
            bbya++;
            posbbya.insert(i+1);
        }

    }
    //dbg(counta);dbg(countb);

    if(counta%2==1||countb%2==1){
        cout<<-1<<endl;return;
    }

    //dbg(posabyb);
    ll ans=(abyb/2)+bbya/2;
    if(abyb%2==1){ans+=2;}

    cout<<ans<<endl;
    

    
    while(posabyb.size()>1){
        auto it=posabyb.begin();
        auto last=--posabyb.end();
        cout<<*it<<" "<<*(last)<<endl;
        
        posabyb.erase(posabyb.begin());
        posabyb.erase(last);
    }
    while(posbbya.size()>1){
        auto it=posbbya.begin();
        auto last=--posbbya.end();
        cout<<*it<<" "<<*(last)<<endl;
        
        posbbya.erase(posbbya.begin());
        posbbya.erase(last);
    }
    

    auto final=posabyb.begin();
    auto notfinal= posbbya.begin();


    if(posabyb.size()!=0){
        cout<<*final<<" "<<*final<<endl;
        cout<<*final<<" "<<*notfinal<<endl;
    }
}

int main(){
    fast;
    ll t;t=1;while(t--){solve();}
    
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction