生活随笔
收集整理的這篇文章主要介紹了
Good Luck!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
我們都知道,前綴就是一個單詞的前幾個字母(長度小于單詞長度);后綴就是一個單詞的后幾個字母(長度小于單詞長度)。例如:Hello,{H,He,Hel,Hell}都是Hello的前綴,{ello,llo,lo,o}都是Hello的后綴。現在,給你一個字符串String,你的任務是找出一個字串s,s既是String的前綴,又是String的后綴,并且s也出現在String的中間部分(既不是前綴,也不是后綴),s的長度越長越好。
Input
輸入一個N,接下來N行,每行一個字符串String,String長度len( 1 <= len <= 1000000)。
Output
輸出只有一行,如果有符合條件的s,輸出長度最長的s,如果沒有,輸出“Bad Luck!”(不含引號)。
Sample Input
3
abcabcabcabcabc
papapapap
aaabaaaabab
Sample Output
abcabcabc
papap
Bad Luck!
Hint
Source
GLSilence
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {Scanner ss =
new Scanner(System.
in);
int n;n = ss.nextInt();ss.nextLine();
while (n >
0) {String str = ss.nextLine();
char a[] = str.toCharArray();
int len = str.length();
int maxlen =
0;String laststr =
null;
for (
int i =
1; i < len; i++) {String tp =
new String(a, len - i, i);
if (str.startsWith(tp) && str.indexOf(tp,
1) != str.lastIndexOf(tp)) {
if (tp.length() > maxlen) {maxlen = tp.length();laststr = tp;}}}
if (laststr ==
null)System.
out.println(
"Bad Luck!");
elseSystem.
out.println(laststr);n--;}ss.close();}
}
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {Scanner ss =
new Scanner(System.
in);
int n;n = ss.nextInt();ss.nextLine();
while (n >
0) {String str = ss.nextLine();
int len = str.length();
if (len <=
2) {System.
out.println(
"Bad Luck!");}
else {
char a[] = str.toCharArray();
int maxlen =
0;String laststr =
null;String temp =
new String(a,
1, len -
2);
for (
int i =
1; i < len; i++) {String tp =
new String(a, len - i, i);
if (str.startsWith(tp) && temp.contains(tp)) {
if (tp.length() > maxlen) {maxlen = tp.length();laststr = tp;}} }
if (laststr ==
null)System.
out.println(
"Bad Luck!");
elseSystem.
out.println(laststr);}n--;}ss.close();}
}
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {Scanner ss =
new Scanner(System.
in);
int n;n = ss.nextInt();ss.nextLine();
while (n >
0) {String str = ss.nextLine();
int len = str.length();
if (len <=
2) {System.
out.println(
"Bad Luck!");}
else {
int maxlen =
0;String laststr =
null;String temp = str.substring(
1, len -
1);
for (
int i = len -
1; i >
0; i--) {String tp = str.substring(i);
if (str.startsWith(tp) && temp.contains(tp)) {
if (tp.length() > maxlen) {maxlen = tp.length();laststr = tp;}}}
if (laststr ==
null)System.
out.println(
"Bad Luck!");
elseSystem.
out.println(laststr);}n--;}ss.close();}
}
總結
以上是生活随笔為你收集整理的Good Luck!的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。