Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAX 500007
- using namespace std;
- int link[MAX], len[MAX], tab[MAX][26], sz, last;
- string s, t;
- void init(){
- link[0] = -1;
- len[0] = 0;
- sz = 1;
- last = 0;
- }
- void extend(int c){
- int cur = sz++;
- len[cur] = len[last] +1;
- for(; last != -1 && !tab[last][c]; last = link[last]) tab[last][c] = cur;
- if(last != -1){
- int p = tab[last][c];
- if(len[p] == len[last] + 1) link[cur] = p;
- else{
- int clone = sz++;
- len[clone] = len[last] + 1;
- memcpy(tab[p], tab[clone], sizeof tab[clone]);
- link[clone] = link[p];
- for(;last != -1 && tab[last][c] == p; last = link[last]) tab[last][c] = clone;
- link[p] = link[cur] = clone;
- }
- }else link[cur] = 0;
- last = cur;
- }
- void substring(){
- init();
- for(int i = 0; i < s.size(); i++) extend(s[i]-'a');
- int estado = 0, tamanho = 0, maior = 0, melhor = 0;
- for(int i = 0; i < t.size(); i++){
- while(estado && !tab[estado][t[i]-'a']){
- estado = link[estado];
- tamanho = len[estado];
- }
- if(tab[estado][t[i]-'a']){
- estado = tab[estado][t[i]-'a'];
- tamanho++;
- }
- if(tamanho > maior){
- maior = tamanho;
- melhor = i;
- }
- }
- if(maior > 0) cout << t.substr(melhor-maior+1, maior) << endl;
- cout << maior << endl;
- }
- int main(){
- cin >> s >> t;
- substring();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement