Open Reisen1969 opened 1 year ago
╭╮╱╱╭━━┳━╮╱╭┳━╮╭━┳━━┳━╮╱╭┳━━┳━━━━╮ ┃┃╱╱╰┫┣┫┃╰╮┃┣╮╰╯╭┻┫┣┫┃╰╮┃┣┫┣┫╭╮╭╮┃ ┃┃╱╱╱┃┃┃╭╮╰╯┃╰╮╭╯╱┃┃┃╭╮╰╯┃┃┃╰╯┃┃╰╯ ┃┃╱╭╮┃┃┃┃╰╮┃┃╭╯╰╮╱┃┃┃┃╰╮┃┃┃┃╱╱┃┃ ┃╰━╯┣┫┣┫┃╱┃┃┣╯╭╮╰┳┫┣┫┃╱┃┃┣┫┣╮╱┃┃ ╰━━━┻━━┻╯╱╰━┻━╯╰━┻━━┻╯╱╰━┻━━╯╱╰╯
██╗░░░░░██╗███╗░░██╗██╗░░██╗██╗███╗░░██╗██╗████████╗ ██║░░░░░██║████╗░██║╚██╗██╔╝██║████╗░██║██║╚══██╔══╝ ██║░░░░░██║██╔██╗██║░╚███╔╝░██║██╔██╗██║██║░░░██║░░░ ██║░░░░░██║██║╚████║░██╔██╗░██║██║╚████║██║░░░██║░░░ ███████╗██║██║░╚███║██╔╝╚██╗██║██║░╚███║██║░░░██║░░░ ╚══════╝╚═╝╚═╝░░╚══╝╚═╝░░╚═╝╚═╝╚═╝░░╚══╝╚═╝░░░╚═╝░░░
░█─── ▀█▀ ░█▄─░█ ▀▄░▄▀ ▀█▀ ░█▄─░█ ▀█▀ ▀▀█▀▀ ░█─── ░█─ ░█░█░█ ─░█── ░█─ ░█░█░█ ░█─ ─░█── ░█▄▄█ ▄█▄ ░█──▀█ ▄▀░▀▄ ▄█▄ ░█──▀█ ▄█▄ ─░█──
| | | | \ | \ \ / / | \ | | | |
| | | | | | |\ V / | | | | | | | | |
| | | | | . | > < | | | .
| | | | |
| | | || |\ |/ . \ | || |\ || | | |
|__||| _// _\|| _|____| |_|
import java.util.Scanner;
/**
Description:
*/
public class Main0177 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String text1 = scanner.nextLine();
String text2 = scanner.nextLine();
String res = solution(text1, text2);
System.out.println(res);
}
}
private static String solution(String text1, String text2) { char[] s1 = text1.toCharArray(); char[] s2 = text2.toCharArray(); int[][] dp = new int[s1.length + 1][s2.length + 1]; int maxLen = 0; int lastPos = 0; for (int i = 0; i < s1.length; i++) { for (int j = 0; j < s2.length; j++) { if (s1[i] == s2[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; if (dp[i + 1][j + 1] > maxLen) { maxLen = dp[i + 1][j + 1]; lastPos = i; } } } } if (maxLen == 0) { return ""; } else { return text1.substring(lastPos - maxLen + 1, lastPos + 1); } } }