【猿辅导笔试】9.12客户端笔试第二题

题目:

使用s1,s2,s3三个字符,s1,s2只能从左边分开,然后将分开的数拼接陈s3,如果最后拼接的次数小于给定的次数,则输出1.

题解:

package test;
import java.util.*;
//示例:
//1
//ac bb abbc 3
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int T = in.nextInt();//输出次数
            for (int i = 0; i < T; i++) {
                String s1 = in.next();
                String s2 = in.next();
                String s3 = in.next();
                int t = in.nextInt();
                String[] s11 = s1.split("");
                String[] s22 = s2.split("");
                String[] s33 = s3.split("");

                int l1 = 0;
                int l2 = 0;
                int count = 0;
                for (int j = 0; j < s33.length;) {
                        if (s11[l1].equals(s33[j]) && l1 <= s11.length - 1) {
                            while (l1 <= s11.length - 1 && s11[l1].equals(s33[j])) {
                                l1++;//这道题让我知道的是以后如果
                                j++;
                            }
                            count++;
                        } else if (s22[l2].equals(s33[j]) && l2 <= s22.length - 1) {
                            while (l2 <= s22.length - 1 && s22[l2].equals(s33[j])) {
                                l2++;
                                j++;
                            }
                            count++;
                        }
                }
                if (count <= t) {
                    System.out.println(1);
                }
            }
        }
    }
}

 

你可能感兴趣的:(历年真题,双指针)