import java.util.*;
import java.io.*;

public class D1141 {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			int n = Integer.parseInt(br.readLine());

			ArrayList<Pair> ans = new ArrayList<>();
			String s1 = br.readLine();
			String s2 = br.readLine();
			ArrayList<ArrayList<Integer>> left = new ArrayList<>(26);
			ArrayList<ArrayList<Integer>> right = new ArrayList<>(26);
			for(int i = 0;i < 26; i++) {
				left.add(i, new ArrayList<Integer>());
				right.add(i, new ArrayList<Integer>());

			}
			ArrayList<Integer> leftI = new ArrayList<>();//Left indefinite color
			ArrayList<Integer> rightI = new ArrayList<>();

			for(int i = 0;i < n; i++) {
				char ch = s1.charAt(i);
				if(ch != '?') {

						ArrayList<Integer> l = left.get(ch - 97);
						l.add( i + 1 );
						left.set(ch - 97,l);

				}
				else {
					leftI.add(i + 1);
				}
				 ch = s2.charAt(i);
				if(ch != '?') {

						ArrayList<Integer> l = right.get(ch - 97);
						l.add( i + 1 );
						right.set(ch - 97,l);

				}
				else {
					rightI.add(i + 1);
				}

			}
			int [] leftAfterDirectPairing = new int[26];
			//Form direct pairs eg(a,a) etc
			for(int i = 0;i < 26; i++) {
				for(int j = 0;j < Math.min(left.get(i).size(), right.get(i).size()); j++) {
					Pair p = new Pair();
					p.x = left.get(i).get(j);
					p.y = right.get(i).get(j);
					ans.add(p);

				}
				leftAfterDirectPairing[i] = Math.min(left.get(i).size(), right.get(i).size());
			}
			//Left matching with right's '?'
			for(int i = 0;i < 26; i++) {
				for(int j = leftAfterDirectPairing[i];j < left.get(i).size(); j++) {
					if(rightI.isEmpty()) {
						break;
					}
					Pair p = new Pair();
					p.x = left.get(i).get(j);
					p.y = rightI.get(rightI.size() - 1);
					rightI.remove(rightI.size() - 1);
					ans.add(p);

				}
			}
			//Right matching with left's '?'
			for(int i = 0;i < 26; i++) {
				for(int j = leftAfterDirectPairing[i];j < right.get(i).size(); j++) {
					if(leftI.isEmpty()) {
						break;
					}
					Pair p = new Pair();
					p.y = right.get(i).get(j);
					p.x = leftI.get(leftI.size() - 1);
					leftI.remove(leftI.size() - 1);
					ans.add(p);

				}
			}
				for(int j = 0;j < Math.min(leftI.size(), rightI.size()); j++) {
					Pair p = new Pair();
					p.x = leftI.get(j);
					p.y = rightI.get(j);
					ans.add(p);

				}
			System.out.println(ans.size());
			if(ans.size() > 0) {
				for(Pair p : ans) {
					System.out.println(p.x + " " + p.y);
				}
			}

	}

}
class Pair {
	int x;
	int y;
}


// Created by Whiplash99
import java.io.*;
import java.util.*;
public class E
{
    static class Boot
    {
        char c;
        int pos;

        Boot(char c, int pos)
        {
            this.c=c;
            this.pos=pos;
        }
    }

    public static void main(String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i,N,j,q1,q2;

        q1=q2=0;

        N=Integer.parseInt(br.readLine().trim());
        char ch1[]=br.readLine().trim().toCharArray();
        char ch2[]=br.readLine().trim().toCharArray();

        ArrayList<Integer> s1=new ArrayList<>();
        ArrayList<Integer> s2=new ArrayList<>();

        ArrayDeque<Integer> qp1=new ArrayDeque<>();
        ArrayDeque<Integer> qp2=new ArrayDeque<>();

        Boot[] b1,b2;

        b1=new Boot[N];
        b2=new Boot[N];

        for(i=0;i<N;i++)
        {
            b1[i]=new Boot(ch1[i],i);
            b2[i]=new Boot(ch2[i],i);

            if(ch1[i]=='?')
            {
                q1++;
                qp1.add(i);
            }
            if(ch2[i]=='?')
            {
                q2++;
                qp2.add(i);
            }
        }

        Arrays.sort(b1,(Boot x, Boot y)->(x.c-y.c));
        Arrays.sort(b2,(Boot x, Boot y)->(x.c-y.c));

        i=j=0;
        while(i<N&&j<N)
        {
            if(b1[i].c=='?')
            {
                i++;
                continue;
            }
            if(b2[j].c=='?')
            {
                j++;
                continue;
            }

            if(b1[i].c==b2[j].c)
            {
                s1.add(b1[i].pos+1);
                s2.add(b2[j].pos+1);

                b1[i].pos=-1;
                b2[j].pos=-1;

                i++;
                j++;

            }
            else if(b1[i].c<b2[j].c)
                i++;
            else
                j++;
        }

        for(i=0;i<N;i++)
        {
            if(qp2.isEmpty())
                break;

            if(b1[i].pos!=-1&&b1[i].c!='?')
            {
                int tmp=qp2.poll();
                s1.add(b1[i].pos+1);
                s2.add(tmp+1);
                b2[tmp].pos=-1;
            }
        }

        for(i=0;i<N;i++)
        {
            if(qp1.isEmpty())
                break;

            if(b2[i].pos!=-1&&b2[i].c!='?')
            {
                int tmp=qp1.poll();
                s2.add(b2[i].pos+1);
                s1.add(tmp+1);
                b1[tmp].pos=-1;
            }
        }

        while(!qp2.isEmpty())
        {
            if(qp1.isEmpty())
                break;

            int tmp=qp1.pollFirst();
            int tmp2=qp2.pollFirst();

            s1.add(tmp+1);
            s2.add(tmp2+1);
        }

        StringBuilder sb=new StringBuilder();

        sb.append(s1.size()).append("\n");

        for(i=0;i<s1.size();i++)
        {
            sb.append(s1.get(i)).append(" ").append(s2.get(i)).append("\n");
        }
        System.out.println(sb);
    }
}


// Created by Whiplash99
import java.io.*;
import java.util.*;
public class E
{
    static class Boot
    {
        char c;
        int pos;

        Boot(char c, int pos)
        {
            this.c=c;
            this.pos=pos;
        }
    }

    public static void main(String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i,N,j,q1,q2;

        q1=q2=0;

        N=Integer.parseInt(br.readLine().trim());
        char ch1[]=br.readLine().trim().toCharArray();
        char ch2[]=br.readLine().trim().toCharArray();

        ArrayList<Integer> s1=new ArrayList<>();
        ArrayList<Integer> s2=new ArrayList<>();

        ArrayDeque<Integer> qp1=new ArrayDeque<>();
        ArrayDeque<Integer> qp2=new ArrayDeque<>();

        Boot[] b1,b2;

        b1=new Boot[N];
        b2=new Boot[N];

        for(i=0;i<N;i++)
        {
            b1[i]=new Boot(ch1[i],i);
            b2[i]=new Boot(ch2[i],i);

            if(ch1[i]=='?')
            {
                q1++;
                qp1.add(i);
            }
            if(ch2[i]=='?')
            {
                q2++;
                qp2.add(i);
            }
        }

        Arrays.sort(b1,(Boot x, Boot y)->(x.c-y.c));
        Arrays.sort(b2,(Boot x, Boot y)->(x.c-y.c));

        i=j=0;
        while(i<N&&j<N)
        {
            if(b1[i].c=='?')
            {
                i++;
                continue;
            }
            if(b2[j].c=='?')
            {
                j++;
                continue;
            }

            if(b1[i].c==b2[j].c)
            {
                s1.add(b1[i].pos+1);
                s2.add(b2[j].pos+1);

                b1[i].pos=-1;
                b2[j].pos=-1;

                i++;
                j++;

            }
            else if(b1[i].c<b2[j].c)
                i++;
            else
                j++;
        }

        for(i=0;i<N;i++)
        {
            if(qp2.isEmpty())
                break;

            if(b1[i].pos!=-1&&b1[i].c!='?')
            {
                int tmp=qp2.poll();
                s1.add(b1[i].pos+1);
                s2.add(tmp+1);
                b2[tmp].pos=-1;
            }
        }

        for(i=0;i<N;i++)
        {
            if(qp1.isEmpty())
                break;

            if(b2[i].pos!=-1&&b2[i].c!='?')
            {
                int tmp=qp1.poll();
                s2.add(b2[i].pos+1);
                s1.add(tmp+1);
                b1[tmp].pos=-1;
            }
        }

        while(!qp2.isEmpty())
        {
            if(qp1.isEmpty())
                break;

            int tmp=qp1.pollFirst();
            int tmp2=qp2.pollFirst();

            s1.add(tmp);
            s2.add(tmp2);
        }

        StringBuilder sb=new StringBuilder();

        sb.append(s1.size()).append("\n");

        for(i=0;i<s1.size();i++)
        {
            sb.append(s1.get(i)).append(" ").append(s2.get(i)).append("\n");
        }
        System.out.println(sb);
    }
}


import java.io.*;
import java.util.*;

public class Solution {

    public static class FastScanner {
        private BufferedReader in;
        private BufferedWriter out;
        private StringTokenizer st;

        public FastScanner () {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new BufferedWriter(new OutputStreamWriter(System.out));
        }

        public FastScanner (String in, String out) throws IOException {
            this.in = new BufferedReader(new FileReader(in));
            this.out = new BufferedWriter(new FileWriter(out));
        }

        public String nextLine () throws IOException {
            return in.readLine();
        }

        public String nextString () throws IOException {
            while ((st == null) || (!st.hasMoreTokens())) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        }

        public int nextInt () throws IOException {
            return Integer.parseInt(nextString());
        }

        public long nextLong () throws IOException {
            return Long.parseLong(nextString());
        }

        public double nextDouble () throws IOException {
            return Double.parseDouble(nextString());
        }

        public void println (String string) throws IOException {
            out.write(string);
            out.newLine();
        }

        public void print (String string) throws IOException {
            out.write(string);
        }

        public void  close () throws IOException {
            in.close();
            out.close();
        }
    }

    public static class Boot implements Comparable<Boot> {
        private char foot;
        private char color;
        private int index;

        public Boot(char foot, char color, int index) {
            this.foot = foot;
            this.color = color;
            this.index = index;
        }

        public char getFoot() {
            return foot;
        }

        public void setFoot(char foot) {
            this.foot = foot;
        }

        public char getColor() {
            return color;
        }

        public void setColor(char color) {
            this.color = color;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public boolean compatible (Boot boot) {
            return foot != boot.getFoot() && (color == boot.getColor() || color == '~' || boot.getColor() == '~');
        }

        @Override
        public int compareTo(Boot o) {
            return color - o.getColor();
        }

        @Override
        public String toString() {
            return foot+" "+color+" "+index;
        }
    }

    public static List<Boot[]> solve (int n, String l, String r) {
        List<Boot[]> ans = new ArrayList<>();
        List<Boot> bootsL = new ArrayList<>();
        List<Boot> bootsR = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            bootsL.add(new Boot('l', l.charAt(i) == '?' ? '~' : l.charAt(i), i));
            bootsR.add(new Boot('r', r.charAt(i) == '?' ? '~' : r.charAt(i), i));
        }
        Collections.sort(bootsL);
        Collections.sort(bootsR);
        int indexL = 0;
        int indexIndefiniteL = n - 1;
        int indexR = 0;
        int indexIndefiniteR = n - 1;
        while (indexL < bootsL.size() && indexR < bootsR.size()) {
            if (bootsL.get(indexL).compatible(bootsR.get(indexR))) {
                ans.add(new Boot[]{bootsL.get(indexL), bootsR.get(indexR)});
                indexL++;
                indexR++;
            }
            else if (bootsL.get(indexL).compareTo(bootsR.get(indexR)) < 0){
                if (bootsL.get(indexL).compatible(bootsR.get(indexIndefiniteR))) {
                    ans.add(new Boot[]{bootsL.get(indexL), bootsR.get(indexIndefiniteR)});
                    bootsR.get(indexIndefiniteR).setColor('*');
                    indexIndefiniteR--;
                }
                indexL++;
            }
            else {
                if (bootsR.get(indexR).compatible(bootsL.get(indexIndefiniteL))) {
                    ans.add(new Boot[]{bootsL.get(indexIndefiniteL), bootsR.get(indexR)});
                    bootsL.get(indexIndefiniteL).setColor('*');
                    indexIndefiniteL--;
                }
                indexR++;
            }
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        FastScanner fastScanner = new FastScanner();
        int n = fastScanner.nextInt();
        String l = fastScanner.nextString();
        String r = fastScanner.nextString();
        List<Boot[]> ans = solve(n, l, r);
        fastScanner.println(ans.size()+"");
        for (int i = 0; i < ans.size(); i++) {
            fastScanner.println((ans.get(i)[0].getIndex()+1)+" "+(ans.get(i)[1].getIndex()+1));
        }
        fastScanner.close();
    }
}


import java.io.*;
import java.util.*;

public class Solution {

    public static class FastScanner {
        private BufferedReader in;
        private BufferedWriter out;
        private StringTokenizer st;

        public FastScanner () {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new BufferedWriter(new OutputStreamWriter(System.out));
        }

        public FastScanner (String in, String out) throws IOException {
            this.in = new BufferedReader(new FileReader(in));
            this.out = new BufferedWriter(new FileWriter(out));
        }

        public String nextLine () throws IOException {
            return in.readLine();
        }

        public String nextString () throws IOException {
            while ((st == null) || (!st.hasMoreTokens())) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        }

        public int nextInt () throws IOException {
            return Integer.parseInt(nextString());
        }

        public long nextLong () throws IOException {
            return Long.parseLong(nextString());
        }

        public double nextDouble () throws IOException {
            return Double.parseDouble(nextString());
        }

        public void println (String string) throws IOException {
            out.write(string);
            out.newLine();
        }

        public void print (String string) throws IOException {
            out.write(string);
        }

        public void  close () throws IOException {
            in.close();
            out.close();
        }
    }

    public static List<Integer[]> solve (int n, String l, String r) {
        List<Integer[]> ans = new ArrayList<>();
        char[] array1 = l.toCharArray();
        char[] array2 = r.toCharArray();
        for (int i = 0; i < n; i++) {
            if (array1[i] == '?') {
                continue;
            }
            boolean found = false;
            for (int j = 0; j < n; j++) {
                if (array1[i] == array2[j]) {
                    ans.add(new Integer[] {i+1, j+1});
                    array1[i] = '*';
                    array2[j] = '*';
                    found = true;
                    break;
                }
            }
            if (!found) {
                for (int j = 0; j < n; j++) {
                    if (array2[j] == '?') {
                        ans.add(new Integer[] {i+1, j+1});
                        array1[i] = '*';
                        array2[j] = '*';
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (array1[i] == '?') {
                boolean found = false;
                for (int j = 0; j < n; j++) {
                    if (array2[j] != '?' && array2[j] != '*') {
                        ans.add(new Integer[] {i+1, j+1});
                        array1[i] = '*';
                        array2[j] = '*';
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    for (int j = 0; j < n; j++) {
                        if (array2[j] == '?') {
                            ans.add(new Integer[] {i+1, j+1});
                            array1[i] = '*';
                            array2[j] = '*';
                            break;
                        }
                    }
                }
            }
            for (int j = 0; j < n; j++) {

                if (array1[i] == array2[j] && array1[i] == '?') {
                    ans.add(new Integer[] {i+1, j+1});
                    array1[i] = '*';
                    array2[j] = '*';
                    break;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        FastScanner fastScanner = new FastScanner();
        int n = fastScanner.nextInt();
        String l = fastScanner.nextString();
        String r = fastScanner.nextString();
        List<Integer[]> ans = solve(n, l, r);
        fastScanner.println(ans.size()+"");
        for (int i = 0; i < ans.size(); i++) {
            fastScanner.println(ans.get(i)[0]+" "+ans.get(i)[1]);
        }
        fastScanner.close();
    }
}


import java.io.*;
import java.util.*;

public class Solution {

    public static class FastScanner {
        private BufferedReader in;
        private BufferedWriter out;
        private StringTokenizer st;

        public FastScanner () {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new BufferedWriter(new OutputStreamWriter(System.out));
        }

        public FastScanner (String in, String out) throws IOException {
            this.in = new BufferedReader(new FileReader(in));
            this.out = new BufferedWriter(new FileWriter(out));
        }

        public String nextLine () throws IOException {
            return in.readLine();
        }

        public String nextString () throws IOException {
            while ((st == null) || (!st.hasMoreTokens())) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        }

        public int nextInt () throws IOException {
            return Integer.parseInt(nextString());
        }

        public long nextLong () throws IOException {
            return Long.parseLong(nextString());
        }

        public double nextDouble () throws IOException {
            return Double.parseDouble(nextString());
        }

        public void println (String string) throws IOException {
            out.write(string);
            out.newLine();
        }

        public void print (String string) throws IOException {
            out.write(string);
        }

        public void  close () throws IOException {
            in.close();
            out.close();
        }
    }

    public static class Boot implements Comparable<Boot> {
        private char foot;
        private char color;
        private int index;

        public Boot(char foot, char color, int index) {
            this.foot = foot;
            this.color = color;
            this.index = index;
        }

        public char getFoot() {
            return foot;
        }

        public void setFoot(char foot) {
            this.foot = foot;
        }

        public char getColor() {
            return color;
        }

        public void setColor(char color) {
            this.color = color;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public boolean compatible (Boot boot) {
            return foot != boot.getFoot() && (color == boot.getColor() || color == '~' || boot.getColor() == '~');
        }

        @Override
        public int compareTo(Boot o) {
            return color - o.getColor();
        }

        @Override
        public String toString() {
            return foot+" "+color+" "+index;
        }
    }

    public static List<Boot[]> solve (int n, String l, String r) {
        List<Boot[]> ans = new ArrayList<>();
        List<Boot> bootsL = new ArrayList<>();
        List<Boot> bootsR = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            bootsL.add(new Boot('l', l.charAt(i) == '?' ? '~' : l.charAt(i), i));
            bootsR.add(new Boot('r', r.charAt(i) == '?' ? '~' : r.charAt(i), i));
        }
        Collections.sort(bootsL);
        Collections.sort(bootsR);
        int indexL = 0;
        int indexIndefiniteL = n - 1;
        int indexR = 0;
        int indexIndefiniteR = n - 1;
        while (indexL < bootsL.size() && indexR < bootsR.size()) {
            if (bootsL.get(indexL).compatible(bootsR.get(indexR))) {
                ans.add(new Boot[]{bootsL.get(indexL), bootsR.get(indexR)});
                indexL++;
                indexR++;
            }
            else if (bootsL.get(indexL).compareTo(bootsR.get(indexR)) < 0){
                if (bootsL.get(indexL).compatible(bootsR.get(indexIndefiniteR))) {
                    ans.add(new Boot[]{bootsL.get(indexL), bootsR.get(indexIndefiniteR)});
                    bootsR.get(indexIndefiniteR).setColor('*');
                    indexIndefiniteR--;
                }
                indexL++;
            }
            else {
                if (bootsR.get(indexR).compatible(bootsL.get(indexIndefiniteL))) {
                    ans.add(new Boot[]{bootsR.get(indexR), bootsL.get(indexIndefiniteL)});
                    bootsL.get(indexIndefiniteL).setColor('*');
                    indexIndefiniteL--;
                }
                indexR++;
            }
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        FastScanner fastScanner = new FastScanner();
        int n = fastScanner.nextInt();
        String l = fastScanner.nextString();
        String r = fastScanner.nextString();
        List<Boot[]> ans = solve(n, l, r);
        fastScanner.println(ans.size()+"");
        for (int i = 0; i < ans.size(); i++) {
            fastScanner.println((ans.get(i)[0].getIndex()+1)+" "+(ans.get(i)[1].getIndex()+1));
        }
        fastScanner.close();
    }
}


import java.io.*;
import java.util.*;

public class Solution {

    public static class FastScanner {
        private BufferedReader in;
        private BufferedWriter out;
        private StringTokenizer st;

        public FastScanner () {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new BufferedWriter(new OutputStreamWriter(System.out));
        }

        public FastScanner (String in, String out) throws IOException {
            this.in = new BufferedReader(new FileReader(in));
            this.out = new BufferedWriter(new FileWriter(out));
        }

        public String nextLine () throws IOException {
            return in.readLine();
        }

        public String nextString () throws IOException {
            while ((st == null) || (!st.hasMoreTokens())) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        }

        public int nextInt () throws IOException {
            return Integer.parseInt(nextString());
        }

        public long nextLong () throws IOException {
            return Long.parseLong(nextString());
        }

        public double nextDouble () throws IOException {
            return Double.parseDouble(nextString());
        }

        public void println (String string) throws IOException {
            out.write(string);
            out.newLine();
        }

        public void print (String string) throws IOException {
            out.write(string);
        }

        public void  close () throws IOException {
            in.close();
            out.close();
        }
    }

    public static class Boot implements Comparable<Boot> {
        private char foot;
        private char color;
        private int index;

        public Boot(char foot, char color, int index) {
            this.foot = foot;
            this.color = color;
            this.index = index;
        }

        public char getFoot() {
            return foot;
        }

        public void setFoot(char foot) {
            this.foot = foot;
        }

        public char getColor() {
            return color;
        }

        public void setColor(char color) {
            this.color = color;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public boolean compatible (Boot boot) {
            return foot != boot.getFoot() && (color == boot.getColor() || color == '~' || boot.getColor() == '~');
        }

        @Override
        public int compareTo(Boot o) {
            return color - o.getColor();
        }

        @Override
        public String toString() {
            return foot+" "+color+" "+index;
        }
    }

    public static List<Boot[]> solve (int n, String l, String r) {
        List<Boot[]> ans = new ArrayList<>();
        List<Boot> bootsL = new ArrayList<>();
        List<Boot> bootsR = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            bootsL.add(new Boot('l', l.charAt(i) == '?' ? '~' : l.charAt(i), i));
            bootsR.add(new Boot('r', r.charAt(i) == '?' ? '~' : r.charAt(i), i));
        }
        Collections.sort(bootsL);
        Collections.sort(bootsR);
        int indexL = 0;
        int indexIndefiniteL = n - 1;
        int indexR = 0;
        int indexIndefiniteR = n - 1;
        while (indexL <= indexIndefiniteL && indexR <= indexIndefiniteR) {
            if (bootsL.get(indexL).compatible(bootsR.get(indexR))) {
                ans.add(new Boot[]{bootsL.get(indexL), bootsR.get(indexR)});
                indexL++;
                indexR++;
            }
            else if (bootsL.get(indexL).compareTo(bootsR.get(indexR)) < 0){
                if (bootsL.get(indexL).compatible(bootsR.get(indexIndefiniteR))) {
                    ans.add(new Boot[]{bootsL.get(indexL), bootsR.get(indexIndefiniteR)});
                    bootsR.get(indexIndefiniteR).setColor('*');
                    indexIndefiniteR--;
                }
                indexL++;
            }
            else {
                if (bootsR.get(indexR).compatible(bootsL.get(indexIndefiniteL))) {
                    ans.add(new Boot[]{bootsL.get(indexIndefiniteL), bootsR.get(indexR)});
                    bootsL.get(indexIndefiniteL).setColor('*');
                    indexIndefiniteL--;
                }
                indexR++;
            }
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        FastScanner fastScanner = new FastScanner();
        int n = fastScanner.nextInt();
        String l = fastScanner.nextString();
        String r = fastScanner.nextString();
        List<Boot[]> ans = solve(n, l, r);
        fastScanner.println(ans.size()+"");
        for (int i = 0; i < ans.size(); i++) {
            fastScanner.println((ans.get(i)[0].getIndex()+1)+" "+(ans.get(i)[1].getIndex()+1));
        }
        fastScanner.close();
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
/**
 *
 * @author arvin
 */
public class Colored_boots {

    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();
        String l=sc.nextLine();
        String r=sc.nextLine();
        ArrayList<Integer> ar[]=new ArrayList[60];
        ArrayList<Integer> bd=new ArrayList<>();
        int l1[]=new int[60];
        int r1[]=new int[60];
        int ind[]=new int[60];
        int ch[]=new int[n+1];
        for(int i=0;i<60;i++)
            ar[i]=new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            l1[l.charAt(i)-63]++;
            r1[r.charAt(i)-63]++;
            ar[r.charAt(i)-63].add(i);
            if(l.charAt(i)=='?')
                bd.add(i);
        }
        int ans=0;
        for(int i=30;i<60;i++)
            ans+=Math.min(l1[i],r1[i]);

        int fin=Math.min(ans+l1[0]+r1[0],n);
        System.out.println(fin);
        for(int i=0;i<n;i++)
        {
            if(l.charAt(i)!='?')
            {
                if(ar[l.charAt(i)-63].size()>ind[l.charAt(i)-63])
                {
                    System.out.println((i+1)+" "+(ar[l.charAt(i)-63].get(ind[l.charAt(i)-63])+1));
                    ch[ar[l.charAt(i)-63].get(ind[l.charAt(i)-63])]=1;
                    ind[l.charAt(i)-63]++;
                }
                else if(ar[0].size()>ind[0])
                {
                    System.out.println((i+1)+" "+(ar[0].get(ind[0])+1));
                    ch[ar[0].get(0)]=1;
                    ind[0]++;
                }
            }
        }
        for(int i=0,j=0;i<n && j<bd.size();i++)
        {
            if(ch[i]!=1)
            {
                System.out.println((bd.get(j)+1)+" "+(i+1));
                j++;
            }
        }
        sc.close();
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
/**
 *
 * @author arvin
 */
public class Colored_boots {

    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();
        String l=sc.nextLine();
        String r=sc.nextLine();
        ArrayList<Integer> ar[]=new ArrayList[60];
        ArrayList<Integer> bd=new ArrayList<>();
        int l1[]=new int[60];
        int r1[]=new int[60];
        int ind[]=new int[60];
        int ch[]=new int[n+1];
        for(int i=0;i<60;i++)
            ar[i]=new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            l1[l.charAt(i)-63]++;
            r1[r.charAt(i)-63]++;
            ar[r.charAt(i)-63].add(i);
            if(l.charAt(i)=='?')
                bd.add(i);
        }
        int ans=0;
        for(int i=30;i<60;i++)
            ans+=Math.min(l1[i],r1[i]);

        int fin=Math.min(ans+l1[0]+r1[0],n);
        System.out.println(fin);
        for(int i=0;i<n;i++)
        {
            if(l.charAt(i)!='?')
            {
                if(ar[l.charAt(i)-63].size()>ind[l.charAt(i)-63])
                {
                    System.out.println((i+1)+" "+(ar[l.charAt(i)-63].get(ind[l.charAt(i)-63])+1));
                    ch[ar[l.charAt(i)-63].get(ind[l.charAt(i)-63])]=1;
                    ind[l.charAt(i)-63]++;
                }
                else if(ar[0].size()>ind[0])
                {
                    System.out.println((i+1)+" "+(ar[0].get(ind[0])+1));
                    ch[ar[0].get(ind[0])]=1;
                    ind[0]++;
                }
            }
        }
        for(int i=0,j=0;i<n && j<bd.size();i++)
        {
            if(ch[i]!=1)
            {
                System.out.println((bd.get(j)+1)+" "+(i+1));
                j++;
            }
        }
        sc.close();
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Main
{
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int ni() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nl() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nd() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
    static class Pair implements Comparable<Pair> {
        int x,y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public int compareTo(Pair o) {
            if(this.x!=o.x)
                return this.x-o.x;
            return this.y-o.y;
        }
        public String toString(){
            return x+" "+y;
        }
    }

    static long MOD=(long)1e9+7;
    static PrintWriter pw = new PrintWriter(System.out,true);
    static StringBuilder sb = new StringBuilder();
    public static void main(String[] args) throws IOException {
        //Reader sc=new Reader();
        Scanner sc = new Scanner(System.in);
        //Code start from here
        int n = sc.nextInt();
        String str1 = sc.next(), str2 = sc.next();
        LinkedList<Integer> left[] = new LinkedList[27];
        LinkedList<Integer> right[] = new LinkedList[27];
        for (int i = 0; i < 27; i++) {
            left[i] = new LinkedList<>();
            right[i] = new LinkedList<>();
        }
        for (int i = 0; i < n; i++) {
            if (str1.charAt(i) == '?')
                left[26].add(i + 1);
            else
                left[str1.charAt(i) - 'a'].add(i + 1);

            if (str2.charAt(i) == '?')
                right[26].add(i + 1);
            else
                right[str2.charAt(i) - 'a'].add(i + 1);
        }
        ArrayList<Integer> l = new ArrayList<>();
        ArrayList<Integer> r = new ArrayList<>();
        for (int i = 0; i < 27; i++) {
            while (!left[i].isEmpty()) {
                int x = left[i].poll();
                if (!right[i].isEmpty()) {
                    l.add(x);
                    int y = right[i].poll();
                    r.add(y);
                }
            }
            while (!right[i].isEmpty()) {
                int y = right[i].poll();
                if (!left[26].isEmpty()) {
                    int x = left[26].poll();
                    l.add(x);
                    r.add(y);
                }
            }
        }
        int size = l.size();
        sb.append(size+"\n");
        for(int i=0;i<size;i++)
            sb.append(l.get(i)+" "+r.get(i)+"\n");
        pw.print(sb);
        pw.flush();
        pw.close();
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Main
{
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int ni() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nl() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nd() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
    static class Pair implements Comparable<Pair> {
        int x,y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public int compareTo(Pair o) {
            if(this.x!=o.x)
                return this.x-o.x;
            return this.y-o.y;
        }
        public String toString(){
            return x+" "+y;
        }
    }

    static long MOD=(long)1e9+7;
    static PrintWriter pw = new PrintWriter(System.out,true);
    static StringBuilder sb = new StringBuilder();
    public static void main(String[] args) throws IOException {
        //Reader sc=new Reader();
        Scanner sc = new Scanner(System.in);
        //Code start from here
        int n = sc.nextInt();
        String str1 = sc.next(), str2 = sc.next();
        LinkedList<Integer> left[] = new LinkedList[27];
        LinkedList<Integer> right[] = new LinkedList[27];
        for (int i = 0; i < 27; i++) {
            left[i] = new LinkedList<>();
            right[i] = new LinkedList<>();
        }
        for (int i = 0; i < n; i++) {
            if (str1.charAt(i) == '?')
                left[26].add(i + 1);
            else
                left[str1.charAt(i) - 'a'].add(i + 1);

            if (str2.charAt(i) == '?')
                right[26].add(i + 1);
            else
                right[str2.charAt(i) - 'a'].add(i + 1);
        }
        ArrayList<Integer> l = new ArrayList<>();
        ArrayList<Integer> r = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            while (!left[i].isEmpty()) {
                int x = left[i].pollFirst();
                if (!right[i].isEmpty()) {
                    int y = right[i].pollFirst();
                    l.add(x);
                    r.add(y);
                }
                else if(!right[26].isEmpty()) {
                    int y = right[26].pollFirst();
                    l.add(x);
                    r.add(y);
                }
                else break;
            }
        }
        LinkedList<Integer> esidual_right = new LinkedList<>();
        for(int i =0;i<27;i++) {
            while(!right[i].isEmpty())
                esidual_right.add(right[i].pollFirst());
        }
        while(!left[26].isEmpty()) {
            l.add(left[26].pollFirst());
            r.add(esidual_right.pollFirst());
        }
        int size = l.size();
        sb.append(size+"\n");
        for(int i=0;i<size;i++)
            sb.append(l.get(i)+" "+r.get(i)+"\n");
        pw.print(sb);
        pw.flush();
        pw.close();
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        Stack<Integer>[]a=new Stack[30];
        Stack<Integer>[]b=new Stack[30];
        for(int i=0;i<30;i++){
            a[i]=new Stack<>();
            b[i]=new Stack<>();
        }
        Map<Integer,Integer>map=new TreeMap<>();
        int n = cin.nextInt();
        String l=cin.next();
        String r=cin.next();
        char[]stl=l.toCharArray();
        char[]str=r.toCharArray();
        for(int i=0;i<n;i++) {
            if(stl[i]=='?')a[27].add(i+1);
            if(str[i]=='?')b[27].add(i+1);
            if(stl[i]>='a'&&stl[i]<='z') a[stl[i]-'a'].push(i+1);
            if(str[i]>='a'&&str[i]<='z') b[str[i]-'a'].push(i+1);
        }
        for(int i=0;i<26;i++){
            while(!a[i].isEmpty()&&!b[i].isEmpty()){
                map.put(a[i].peek(),b[i].peek());
                a[i].pop();
                b[i].pop();
            }
            while(!a[i].isEmpty()&&!b[27].isEmpty()){
                map.put(a[i].peek(),b[27].peek());
                a[i].pop();
                b[27].pop();
            }
            while(!a[27].isEmpty()&&!b[i].isEmpty()){
                map.put(a[27].peek(),b[i].peek());
                a[27].pop();
                b[i].pop();
            }
        }
        while(!a[27].isEmpty()&&!b[27].isEmpty()){
            map.put(a[27].peek(),b[27].peek());
            a[27].pop();
            b[27].pop();
        }
        System.out.println(map.size());
        for(int seq:map.keySet()){
            System.out.println(seq+" "+map.get(seq));
        }

    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        Stack<Integer>[]a=new Stack[30];
        Stack<Integer>[]b=new Stack[30];
        for(int i=0;i<30;i++){
            a[i]=new Stack<>();
            b[i]=new Stack<>();
        }
        Map<Integer,Integer>map=new TreeMap<>();
        int n = cin.nextInt();
        String l=cin.next();
        String r=cin.next();
        char[]stl=l.toCharArray();
        char[]str=r.toCharArray();
        for(int i=0;i<n;i++) {
            if(stl[i]=='?')a[27].add(i+1);
            if(str[i]=='?')a[27].add(i+1);
            if(stl[i]>='a'&&stl[i]<='z') a[stl[i]-'a'].push(i+1);
            if(str[i]>='a'&&str[i]<='z') b[str[i]-'a'].push(i+1);
        }
        for(int i=0;i<26;i++){
            while(!a[i].isEmpty()&&!b[i].isEmpty()){
                map.put(a[i].peek(),b[i].peek());
                a[i].pop();
                b[i].pop();
            }
            while(!a[i].isEmpty()&&!b[27].isEmpty()){
                System.out.println(a[i].peek()+" "+b[27].peek());
                map.put(a[i].peek(),b[27].peek());
                a[i].pop();
                b[27].pop();
            }
            while(!a[27].isEmpty()&&!b[i].isEmpty()){
                map.put(a[27].peek(),b[i].peek());
                a[27].pop();
                b[i].pop();
            }
        }
        while(!a[27].isEmpty()&&!b[27].isEmpty()){
            map.put(a[27].peek(),b[27].peek());
            a[27].pop();
            b[27].pop();
        }
        System.out.println(map.size());
        for(int seq:map.keySet()){
            System.out.println(seq+" "+map.get(seq));
        }

    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 *
 * @author pttrung
 */
public class B_Edu_Round_26 {

    public static long MOD = 998244353;
    static long[][][] dp;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        String l = in.next();
        String r = in.next();
        LinkedList<Integer>[][]a = new LinkedList[2][27];
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 27; j++){
                a[i][j] = new LinkedList();
            }
        }
        for(int i = 0; i < n; i++){
            if(l.charAt(i) == '?'){
                a[0][26].add(i);
            }else{
                int index = l.charAt(i) - 'a';
                a[0][index].add(i);
            }
            if(r.charAt(i) == '?'){
                a[1][26].add(i);
            }else{
                int index = r.charAt(i) - 'a';
                a[1][index].add(i);
            }
        }
        ArrayList<Point> q = new ArrayList<>();
        for(int i = 0; i < 27; i++){
            while(!a[0][i].isEmpty() && (!a[1][i].isEmpty() || !a[1][26].isEmpty())){
                int x = a[0][i].poll();
                int y = a[1][i].isEmpty() ? a[1][26].poll() : a[1][i].poll();
                q.add(new Point(x, y));
            }
        }
        for(int i = 0; i < 27; i++){
            while(!a[1][i].isEmpty() && (!a[0][i].isEmpty() || !a[0][26].isEmpty())){
                int x = a[0][i].isEmpty() ? a[0][26].poll() : a[0][i].poll();
                int y = a[1][i].poll();
                q.add(new Point(x, y));
            }
        }
        out.println(q.size());
        for(Point p : q){
            out.println((p.x + 1) + " " + (p.y + 1));
        }
        out.close();
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 *
 * @author pttrung
 */
public class B_Edu_Round_26 {

    public static long MOD = 998244353;
    static long[][][] dp;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        String l = in.next();
        String r = in.next();
        LinkedList<Integer>[][]a = new LinkedList[2][27];
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 27; j++){
                a[i][j] = new LinkedList();
            }
        }
        for(int i = 0; i < n; i++){
            if(l.charAt(i) == '?'){
                a[0][26].add(i);
            }else{
                int index = l.charAt(i) - 'a';
                a[0][index].add(i);
            }
            if(r.charAt(i) == '?'){
                a[1][26].add(i);
            }else{
                int index = r.charAt(i) - 'a';
                a[1][index].add(i);
            }
        }
        ArrayList<Point> q = new ArrayList<>();
        for(int i = 0; i < 27; i++){
            while(!a[0][i].isEmpty() && (!a[1][i].isEmpty() || !a[1][26].isEmpty())){
                int x = a[0][i].poll();
                int y = a[1].[i].isEmpty() ? a[1][26].poll() : a[1][i].poll();
                q.add(new Point(x, y));
            }
        }
        for(int i = 0; i < 27; i++){
            while(!a[1][i].isEmpty() && (!a[0][i].isEmpty() || !a[0][26].isEmpty())){
                int x = a[0].[i].isEmpty() ? a[0][26].poll() : a[0][i].poll();
                int y = a[1][i].poll();
                q.add(new Point(x, y));
            }
        }
        out.println(q.size());
        for(Point p : q){
            out.println((p.x + 1) + " " + (p.y + 1));
        }
        out.close();
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        char[] l = scanner.next().toCharArray();
        char[] r = scanner.next().toCharArray();

        ArrayList<Integer> indef_l = new ArrayList<>();
        ArrayList<Integer> indef_r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for(int i = 0; i<l.length; i++){
            if(l[i] == '?') indef_l.add(i+1);
            if(r[i] == '?') indef_r.add(i+1);
        }

        for(int i = 0; i<n; i++){
            char c = l[i];
            if(c == '?') continue;

            for(int k = 0; k<n; k++){
                if(c == r[k]) {
                    res.put(i+1, k+1);
                    r[k] = '0';
                    break;
                }
            }
        }

        for(int i = 0; i<l.length; i++){
            char cl = l[i];
            char cr = r[i];

            if(cl != '?' && !indef_r.isEmpty()) res.put(i+1, indef_r.remove(0));
            if(cr != '?' && !indef_l.isEmpty()) res.put(indef_l.remove(0), i+1);
        }

        System.out.println(res.size());
        for(int i : res.keySet()){
            System.out.println(i + " "+res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        LinkedList<Integer> l_q = new LinkedList<>();
        LinkedList<Integer> r_q = new LinkedList<>();
        LinkedList<Pair> l = new LinkedList<>();
        LinkedList<Pair> r = new LinkedList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 && r.size() > 0) {

            if(l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                l.remove(0);
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r.remove(0);
            }
        }

        while (l_q.size() > 0 && r.size() > 0) {
            res.put(l_q.remove(0), r.remove(0).value);
        }

        while(l.size() > 0 && r_q.size() > 0){
            res.put(l.remove(0).value, r_q.remove(0));
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i);
            System.out.println(res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 && r.size() > 0) {

            if(l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        int il = 0;
        int ilq = 0;
        int ir = 0;
        int irq = 0;

        while(il < l.size() && ir < r.size()){

            if(l.get(il).key < r.get(ir).key){
                if(irq < r_q.size()) res.put(l.get(il).value, r_q.get(irq++));
                il++;
            } else if (l.get(il).key == r.get(ir).key){
                res.put(l.get(il++).value, r.get(ir++).value);
            } else {
                if(ilq < l_q.size()) res.put(l_q.get(ilq++), r.get(ir).value);
                ir++;
            }

        }

        while (il < l_q.size() && ir < r.size()) {
            res.put(l_q.get(ilq++), r.get(ir++).value);
        }

        while(il < l.size() && irq < r_q.size()){
            res.put(l.get(il++).value, r_q.get(irq++));
        }

        while (ilq < l_q.size() && irq < r_q.size()){
            res.put(l_q.get(ilq++), r_q.get(irq++));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.print(i);
            System.out.println(res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        int il = 0;
        int ilq = 0;
        int ir = 0;
        int irq = 0;

        while(il < l.size() && ir < r.size()){

            if(l.get(il).key < r.get(ir).key){
                if(r_q.size() > 0) res.put(l.get(il).value, r_q.get(irq++));
                il++;
            } else if (l.get(il).key == r.get(ir).key){
                res.put(l.get(il++).value, r.get(ir++).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.get(ilq++), r.get(ir).value);
                ir++;
            }

        }

        while (il < l_q.size() && ir < r.size()) {
            res.put(l_q.get(ilq++), r.get(ir++).value);
        }

        while(il < l.size() && irq < r_q.size()){
            res.put(l.get(il++).value, r_q.get(irq++));
        }

        while (ilq < l_q.size() && irq < r_q.size()){
            res.put(l_q.get(ilq++), r_q.get(irq++));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.print(i+ " ");
            System.out.println(res.get(i));
        }
    }
}


   import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 || r.size() > 0) {

            if(r.size() == 0 || l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));

        }

    }


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        int il = 0;
        int ilq = 0;
        int ir = 0;
        int irq = 0;

        while(il < l.size() && ir < r.size()){

            if(l.get(ir).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.get(il).value, r_q.get(irq++));
                il++;
            } else if (l.get(0).key == r.get(0).key){
                res.put(l.remove(il++).value, r.remove(ir++).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(ilq), r.remove(ir).value);
                ir++;
            }

        }

        while (il < l_q.size() && ir < r.size()) {
            res.put(l_q.remove(ilq++), r.remove(ir++).value);
        }

        while(il < l.size() && irq < r_q.size()){
            res.put(l.remove(il++).value, r_q.remove(irq++));
        }

        while (ilq < l_q.size() && irq < r_q.size()){
            res.put(l_q.remove(ilq++), r_q.remove(irq++));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i);
            System.out.println(res.get(i));
        }
    }
}


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Colored_Boots {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        char[] l = scanner.next().toCharArray();
        char[] r = scanner.next().toCharArray();

        ArrayList<Integer> indef = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < l.length; i++) {
            char c = l[i];
            if (c == '?') indef.add(i);

            else {
                for (int k = 0; k < n; k++) {
                    if (r[k] == c) {
                        r[k] = '0';
                        res.put(i, k);
                        break;
                    }
                }
            }
        }

        for (int i = 0; i < r.length; i++) {
            if (indef.size() == 0) break;
            if (r[i] != '0') res.put(indef.remove(0), i);
        }

        System.out.println(res.size());
        for(int i : res.keySet()) System.out.println(i + " "+res.get(i));
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        int il = 0;
        int ilq = 0;
        int ir = 0;
        int irq = 0;

        while(il < l.size() && ir < r.size()){

            if(l.get(il).key < r.get(ir).key){
                if(r_q.size() > 0) res.put(l.get(il).value, r_q.get(irq++));
                il++;
            } else if (l.get(il).key == r.get(ir).key){
                res.put(l.remove(il++).value, r.remove(ir++).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(ilq++), r.remove(ir).value);
                ir++;
            }

        }

        while (il < l_q.size() && ir < r.size()) {
            res.put(l_q.remove(ilq++), r.remove(ir++).value);
        }

        while(il < l.size() && irq < r_q.size()){
            res.put(l.remove(il++).value, r_q.remove(irq++));
        }

        while (ilq < l_q.size() && irq < r_q.size()){
            res.put(l_q.remove(ilq++), r_q.remove(irq++));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i);
            System.out.println(res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 || r.size() > 0) {

            if(r.size() == 0 || l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 && r.size() > 0) {

            if(r.size() == 0 || l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        int il = 0;
        int ilq = 0;
        int ir = 0;
        int irq = 0;

        while(il < l.size() && ir < r.size()){

            if(l.get(il).key < r.get(ir).key){
                if(irq < r_q.size()) res.put(l.get(il).value, r_q.get(irq++));
                il++;
            } else if (l.get(il).key == r.get(ir).key){
                res.put(l.get(il++).value, r.get(ir++).value);
            } else {
                if(ilq < l_q.size()) res.put(l_q.get(ilq++), r.get(ir).value);
                ir++;
            }

        }

        while (il < l_q.size() && ir < r.size()) {
            res.put(l_q.get(ilq++), r.get(ir++).value);
        }

        while(il < l.size() && irq < r_q.size()){
            res.put(l.get(il++).value, r_q.get(irq++));
        }

        while (ilq < l_q.size() && irq < r_q.size()){
            res.put(l_q.get(ilq++), r_q.get(irq++));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i);
            System.out.println(res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0) {

            if(l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_indef = new ArrayList<>();
        ArrayList<Integer> r_indef = new ArrayList<>();
        HashMap<Character, ArrayList<Integer>> l = new HashMap<>();
        HashMap<Character, ArrayList<Integer>> r = new HashMap<>();

        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            char cl = tmp_l[i];
            char cr = tmp_r[i];

            if (cl == '?') l_indef.add(i + 1);
            if (cr == '?') r_indef.add(i + 1);
            else {
                if (!r.containsKey(tmp_r[i])) r.put(tmp_r[i], new ArrayList<>());
                r.get(tmp_r[i]).add(i + 1);
            }
        }

        for (int i = 0; i < tmp_l.length; i++) {
            char c = tmp_l[i];
            if (c == '?') continue;

            if (r.containsKey(c) && r.get(c).size() > 0) {
                res.put(i+1, r.get(c).remove(0));
                if (r.get(c).size() == 0) res.remove(c);
            } else if (r_indef.size() > 0) {
                res.put(i+1, r_indef.remove(0));
            } else {
                if (!l.containsKey(c)) l.put(c, new ArrayList<>());
                l.get(c).add(i + 1);
            }
        }

        for (char c : r.keySet()) {
            if (l_indef.size() == 0) break;
            for (int i : r.get(c)) res.put(l_indef.remove(0), i);
        }

        while (r_indef.size() > 0 && l_indef.size() > 0) {
            res.put(l_indef.remove(0), r_indef.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        char[] l = scanner.next().toCharArray();
        char[] r = scanner.next().toCharArray();

        ArrayList<Integer> indef_l = new ArrayList<>();
        ArrayList<Integer> indef_r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for(int i = 0; i<l.length; i++){
            if(l[i] == '?') indef_l.add(i+1);
            if(r[i] == '?') indef_r.add(i+1);
        }

        for(int i = 0; i<n; i++){
            char c = l[i];
            if(c == '?') continue;

            for(int k = 0; k<n; k++){
                if(c == r[k]) {
                    res.put(i+1, k+1);
                    r[k] = '0';
                    break;
                }
            }
        }

        for(int i = 0; i<l.length; i++){
            char cl = l[i];
            char cr = r[i];

            if(cl != '?' && !indef_r.isEmpty()) res.put(i+1, indef_r.remove(0));
            if(cr != '?' && !indef_l.isEmpty()) res.put(indef_l.remove(0), i+1);
        }

        while(!indef_l.isEmpty() && !indef_r.isEmpty()){
            res.put(indef_l.remove(0), indef_r.remove(0));
        }

        System.out.println(res.size());
        for(int i : res.keySet()){
            System.out.println(i + " "+res.get(i));
        }
    }
}


import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 && r.size() > 0) {

            if(r.size() == 0 || l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r.size() > 0) {
            res.put(l_q.remove(0), r.remove(0).value);
        }

        while(l.size() > 0 && r_q.size() > 0){
            res.put(l.remove(0).value, r_q.remove(0));
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }
}


|  In English  По-русски
deobrah | Logout

HOME
TOP
CONTESTS
GYM
PROBLEMSET
GROUPS
RATING
API
HELP
CALENDAR

PROBLEMSSUBMIT CODEMY SUBMISSIONSSTATUSHACKSSTANDINGSCUSTOM INVOCATION

My Submissions

#	When	Who	Problem	Lang	Verdict	Time	Memory
56327268	Jun/30/2019 16:32UTC+1	deobrah	D - Colored Boots	Java 8	Wrong answer on test 4	170 ms	0 KB
56325469	Jun/30/2019 16:25UTC+1	deobrah	D - Colored Boots	Java 8	Runtime error on test 3	171 ms	0 KB
56310763	Jun/30/2019 15:39UTC+1	deobrah	D - Colored Boots	Java 8	Time limit exceeded on test 16	2000 ms	10000 KB
56310559	Jun/30/2019 15:34UTC+1	deobrah	D - Colored Boots	Java 8	Time limit exceeded on test 16	2000 ms	11300 KB
56309772	Jun/30/2019 15:08UTC+1	deobrah	D - Colored Boots	Java 8	Time limit exceeded on test 12	2000 ms	0 KB
56309637	Jun/30/2019 15:03UTC+1	deobrah	D - Colored Boots	Java 8	Wrong answer on test 4	124 ms	0 KB
56308794	Jun/30/2019 14:34UTC+1	deobrah	D - Colored Boots	Java 8	Wrong answer on test 4	124 ms	0 KB
56308759	Jun/30/2019 14:33UTC+1	deobrah	D - Colored Boots	Java 8	Wrong answer on test 1	124 ms	0 KB
56306505	Jun/30/2019 13:09UTC+1	deobrah	C - Polycarp Restores Permutation	Java 8	Accepted	1716 ms	10400 KB
56306063	Jun/30/2019 12:52UTC+1	deobrah	C - Polycarp Restores Permutation	Java 8	Time limit exceeded on test 38	2000 ms	0 KB
56305900	Jun/30/2019 12:46UTC+1	deobrah	C - Polycarp Restores Permutation	Java 8	Time limit exceeded on test 38	2000 ms	0 KB
56305644	Jun/30/2019 12:38UTC+1	deobrah	C - Polycarp Restores Permutation	Java 8	Wrong answer on test 3	139 ms	0 KB
56305550	Jun/30/2019 12:35UTC+1	deobrah	C - Polycarp Restores Permutation	Java 8	Runtime error on test 2	124 ms	0 KB
56304736	Jun/30/2019 12:06UTC+1	deobrah	B - Maximal Continuous Rest	Java 8	Accepted	280 ms	0 KB
56304373	Jun/30/2019 11:52UTC+1	deobrah	A - Game 23	Java 8	Wrong answer on test 7	124 ms	0 KB
56304263	Jun/30/2019 11:48UTC+1	deobrah	A - Game 23	Java 8	Accepted	140 ms	0 KB

Codeforces Round #547 (Div. 3)
Finished
Practice
 Add to favourites

→ Virtual participation
Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

→ Practice
You are registered for practice. You can solve problems unofficially. Results can be found in the contest status and in the bottom of standings.

→ Clone Contest to Mashup
You can clone this contest to a mashup.

→ Contest materials
Announcement #1 (en)
Announcement #2 (ru)
Tutorial (en)

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: Jun/30/2019 16:38:08UTC+1 (g2).
Mobile version, switch to desktop version.
Privacy Policy
Supported by
Telegram  ИТМО
By deobrah, contest: Codeforces Round #547 (Div. 3), problem: (D) Colored Boots, Wrong answer on test 4, #
 import java.util.*;

public class Colored_Boots {

    public static class Pair {
        Character key;
        Integer value;

        public Pair(Character key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public boolean equals(Pair p) {
            return p.key == key;
        }

        public int hashCode() {
            return key.hashCode();
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextInt();
        char[] tmp_l = scanner.next().toCharArray();
        char[] tmp_r = scanner.next().toCharArray();

        ArrayList<Integer> l_q = new ArrayList<>();
        ArrayList<Integer> r_q = new ArrayList<>();
        ArrayList<Pair> l_u = new ArrayList<>();
        ArrayList<Pair> r_u = new ArrayList<>();
        ArrayList<Pair> l = new ArrayList<>();
        ArrayList<Pair> r = new ArrayList<>();
        HashMap<Integer, Integer> res = new HashMap<>();

        for (int i = 0; i < tmp_l.length; i++) {
            if (tmp_l[i] == '?') l_q.add(i + 1);
            else l.add(new Pair(tmp_l[i], i + 1));
            if (tmp_r[i] == '?') r_q.add(i + 1);
            else r.add(new Pair(tmp_r[i], i + 1));
        }

        l.sort(Comparator.comparingInt(x -> x.key));
        r.sort(Comparator.comparingInt(x -> x.key));

        while (l.size() > 0 || r.size() > 0) {

            if(r.size() == 0 || l.get(0).key < r.get(0).key){
                if(r_q.size() > 0) res.put(l.remove(0).value, r_q.remove(0));
                else l_u.add(l.remove(0));
            } else if(l.get(0).key == r.get(0).key){
                res.put(l.remove(0).value, r.remove(0).value);
            } else {
                if(l_q.size() > 0) res.put(l_q.remove(0), r.remove(0).value);
                else r_u.add(r.remove(0));
            }
        }

        while (l_q.size() > 0 && r_q.size() > 0){
            res.put(l_q.remove(0), r_q.remove(0));
        }

        System.out.println(res.size());
        for (int i : res.keySet()) {
            System.out.println(i + " " + res.get(i));
        }
    }
}