programming questions
Find the longest common substring of two strings a and b
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String s1 = scanner.nextLine();
String s2 = scanner.nextLine();
System.out.println(stringchuan(s1,s2));
}
}
private static String stringchuan(String s1, String s2) {
String str = "";
if(s1.length() > s2.length()) {
str = s1;
s1 = s2;
s2 = str;
}
int[][] dp = new int[s1.length() + 1][s2.length() + 1];
int max = 0;
int index = 0;
for (int i = 1; i < s1.length() + 1; i++) {
for (int j = 1; j < s2.length() + 1; j++) {
if(s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
if(dp[i][j] > max) {
max = dp[i][j];
index = i;
}
}
}
return s1.substring(index - max,index);
}
}
soda bottle
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int n = scanner.nextInt();
if(n == 0) {
break;
}else {
System.out.println(n/2);
}
}
}
}