C. Swap Letters 01 string least equal to several times the exchange

 

C. Swap Letters

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Monocarp has got two strings ss and tt having equal length. Both strings consist of lowercase Latin letters "a" and "b".

Monocarp wants to make these two strings ss and tt equal to each other. He can do the following operation any number of times: choose an index pos1pos1 in the string ss, choose an index pos2pos2 in the string tt, and swap spos1spos1 with tpos2tpos2.

You have to determine the minimum number of operations Monocarp has to perform to make ss and tt equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal.

Input

The first line contains one integer n(1n2105)(1≤n≤2⋅105) — the length of ss and tt.

The second line contains one string ss consisting of nn characters "a" and "b".

The third line contains one string tt consisting of nn characters "a" and "b".

Output

If it is impossible to make these strings equal, print 1−1.

Otherwise, in the first line print kk — the minimum number of operations required to make the strings equal. In each of the next kk lines print two integers — the index in the string ss and the index in the string tt that should be used in the corresponding swap operation.

Examples
input
4 
abab 
aabb
output
2
3 3
3 2
input
1
a
b
output
-1
input
8 
Babb aabb 
abababaa
output
3
2 6
1 3
7 8
Note

In the first example two operations are enough. For example, you can swap the third letter in ss with the third letter in tt. Then s=s= "abbb", t=t= "aaab". Then swap the third letter in ss and the second letter in tt. Then both ss and tt are equal to "abab".

In the second example it's impossible to make two strings equal.

 

Meaning of the questions: two strings to you, ask how many times can be exchanged at least these two strings are equal, and the output switching scheme

 

Solution: because the character is only a, b two kinds; it is not equal when only two cases

1、

   

  b

 

2、

   b

  a

Respectively, the number of occurrences statistics in both cases, with k1, k2 represents

If k1, k2 has an odd number, an even number, i.e., (k1 + k2)% 2 == 1, then it is impossible to exchange two strings are equal, the output of -1

Otherwise, let the case of the same unequal pairs of the exchange, the number of exchanges of k1 / 2 + k2 / 2

Finally determining k1, k2 whether both odd, and if so, the last only as a set is not equal to

a  b

b  a

It should be switched twice to make two strings are equal

------------1

b  b

a  a

------------2

b  a

b  a

------------

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<stack>
#include<string.h>
#include<string>
#include<vector>
#define ll long long
using namespace std;
int pos1[200005],pos2[200005];
int main()
{
    string s1,s2;
    int t;
    cin>>t;
    cin>>s1;
    China>> S2; 
        
    int K1 = 0 , K2 = 0 ;
     for ( int I = 0 ; I <T; I ++ ) 
    { 
        IF (! S1 [I] = S2 [I] && S1 [I] == ' A ' ) 
            POS1 [K1 ++] = I + 1 ; // output index starts from an 

        IF (S1 [I] = S2 [I] && S1 [I] ==! ' B ' ) 
            POS2 [K2 ++] = I + 1 ; 
    } 
    IF ((K1 + K2)% 2 == . 1 ) // K1, K2 is an odd number, an even number 
        cout << -. 1 << endl;
     the else 
    { 
        int cnt = K1 / 2 + K2 / 2 ;
         IF (K1% 2 == . 1 && K2% 2 == . 1 ) // If k1, k2 for the last odd-numbered switching time to be exchanged twice 
            cnt CNT + = 2 ; 
        COUT << CNT << endl;
         int I, J;
         for (I = 0 ; I + . 1 <K1; I = I + 2 ) 
            COUT << POS1 [I] << '  ' << POS1 [I + . 1 ] << endl;
                
        for(j=0;j+1<k2;j=j+2)
            cout<<pos2[j]<<' '<<pos2[j+1]<<endl;

        if(i!=k1&&j!=k2)//处理最后一次交换下标
        {
            cout<<pos1[k1-1]<<' '<<pos1[k1-1]<<endl;
            cout<<pos1[k1-1]<<' '<<pos2[k2-1]<<endl;
        }
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/-citywall123/p/11919680.html