【Java笔试强训】day20编程题

编程题

字符串反转

【Java笔试强训】day20编程题_第1张图片

import java.util.Scanner;

@SuppressWarnings({"all"})
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        StringBuffer stringBuffer = new StringBuffer(s);
        String reverseStr = String.valueOf(stringBuffer.reverse());
        System.out.println(reverseStr);
    }

}
import java.util.Scanner;

@SuppressWarnings({"all"})
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] ch = s.toCharArray();
        int l = 0;
        int r = ch.length - 1;
        while (l < r) {
            char tmp = ch[l];
            ch[l] = ch[r];
            ch[r] = tmp;
            l++;
            r--;
        }
        String s1 = new String(ch);
        System.out.println(s1);
    }
}

公共子串计算

【Java笔试强训】day20编程题_第2张图片

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            String max = s1.length() >= s2.length() ? s1 : s2;
            String min = s1.length() >= s2.length() ? s2 : s1;
            String s = "";
            // i j
            // a s d f a s
            // w e r a s d f a s w e r
            int maxLength = 0;
            for (int i = 0; i < min.length(); i++) {
                for (int j = i + 1; j <= min.length(); j++) {
                    if (max.contains(min.substring(i, j)) && j - i > maxLength) {
                        maxLength = j - i;
                        s = min.substring(i, j);
                    }
                }
            }
            System.out.println(s.length());
        }
        sc.close();
    }
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            int row = s1.length();
            int col = s2.length();
            int[][] dp = new int[row + 1][col + 1];
            int max = 0;
            //dp[i][j]表示s1的前i个字符串和s2的前j个字符串的最大公共字串的长度
            for (int i = 0; i <= row; i++) {
                dp[i][0] = 0;
            }
            for (int i = 0; i <= col; i++) {
                dp[0][i] = 0;
            }
            for (int i = 1; i <= row; i++) {
                for (int j = 1; j <= col; j++) {
                    if (s1.charAt(i - 1) != s2.charAt(j - 1)) {
                        dp[i][j] = 0;
                    } else {
                        dp[i][j] = dp[i-1][j-1]+1;
                    }
                    max = Math.max(max,dp[i][j]);
                }
            }
            System.out.println(max);
        }
    }
}

你可能感兴趣的:(【Java笔试强训】48day,java,开发语言)