Good Luck!

本文探讨了一个字符串处理问题,即寻找一个既是给定字符串的前缀也是后缀的子串,并且该子串还必须出现在字符串的中间部分。文章提供了几种尝试解决此问题的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

//完全使用String类的方法
//结果超时

//import java.text.ParseException;
//import java.text.SimpleDateFormat;
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!");
            else
                System.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!");
                else
                    System.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!");
                else
                    System.out.println(laststr);
            }
            n--;
        }
        ss.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值