【Codeforces 126B】Password

【链接】 我是链接,点我呀:)
【题意】


给你一个字符串s
让你从中选出来一个字符串t
这个字符串t是s的前缀和后缀
且在除了前缀和后缀之外的中间部位出现过。
且要求t的长度最长。
让你输出这个字符串t

【题解】


KMP的应用
f[i]就是以i为结尾的后缀能匹配的最长前缀的长度
因此只要知道f[n]的值。
然后在做kmp的时候,看看中间有没有哪个时刻能匹配到长度为f[n]的前缀就好(开个数组标记一下就好);
如果没有就让j = f[f[n]]
直到匹配不到为止.

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    static int N = (int)1e6;
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static class Task{
        public void solve(InputReader in,PrintWriter out) {
            int [] f = new int[N+10];
            int [] ok = new int[N+10];
            int n;
            String s = in.next();
            n = s.length();
            f[0] = 0;f[1] = 0;
            for (int i = 1;i < n;i++) {
                int j = f[i];
                while (j>0 && s.charAt(i)!=s.charAt(j)) j = f[j];
                if (s.charAt(i)==s.charAt(j)) {
                    f[i+1] = j+1;
                    if (i<n-1) {
                        ok[j+1] = 1;
                    }
                }
                else
                    f[i+1] = 0;
            }
            
            int j = f[n];
            int temp = 0;
            while (j>0) {
                if (ok[j]==1) {
                    temp = Math.max(temp, j);
                }
                j = f[j];
            }
            
            if (s.charAt(n-1)==s.charAt(0) && ok[1]==1) {
                temp = Math.max(1, temp);
            }
            if (temp==0) {
                out.print("Just a legend");
            }else {
                for (int i = 0;i < temp;i++) {
                    out.print(s.charAt(i));
                }
            }
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/10359552.html