2020/9/7 贝壳找房笔试题

1. 石头剪刀布模拟题    easy

2. 题目大意:单次操作可以插入一个字符至末尾 / 或将当前已有字符copy一遍到末尾(仅可使用一次)
问最少需要多少次操作得到目标串s
len<=1e6len<=1e6len<=1e6

注意:只能复制一次是重点,什么是复制一次,就是把abc+abc,理解清楚这个是重点,所以并不需要KMP,只需要判断前n/2的前缀就行。

public class T02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        String s = sc.nextLine();
        if(n == 1) System.out.println(1);
        else {
            String ans = resCount(s,n);
            int count = n - ans.length();
            System.out.println(count);
        }
    }

    public static String resCount(String s,int n){
        String res = "";
        for (int i = 0; i <= n/2; i++) {
            String temp = s.substring(0,i);
            int j = i+1;
            if(j+i<=n){
                String temp2 = s.substring(j,j+i);
                if(temp.equals(temp2)){
                    res = temp;
                }else continue;
            }else break;
        }
        return res;
    }
}

3,4都是动态规划,过于难。

两道题目及格水平,后面两道竞赛题目要求。

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/108453076