-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstr.cpp
More file actions
41 lines (34 loc) · 722 Bytes
/
substr.cpp
File metadata and controls
41 lines (34 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s="ababcd",t="abcd";
int sl,tl;
sl=s.length();
tl=t.length();
int cache[sl][tl];
string out="";
if(sl==0||tl==0)
cout<<out;
for(int i=0;i<sl;i++)
{
for(int j=0;j<tl;j++)
{
if(s[i]==t[j])
{
if(i==0||j==0)
cache[i][j]=1;
else
{
cache[i][j]=cache[i-1][j-1]+1;
}
if(cache[i][j]>out.length())
{
out=s.substr(i-cache[i][j]+1,i+1);
}
}
}
}
cout<<out<<endl;
return 0;
}