Skip to content

Commit 1f33f8b

Browse files
committed
84 Longest Common Substring.
1 parent 1fa6bd0 commit 1f33f8b

File tree

9 files changed

+332
-1
lines changed

9 files changed

+332
-1
lines changed

demo.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,31 @@
20912091
"notes": "",
20922092
"problem": "Reverse 3-digit Integer",
20932093
"tags": []
2094+
},
2095+
"84": {
2096+
"Address": "Central Campus Mall, Boulder, CO 80302, USA",
2097+
"Geolocation": {
2098+
"city": "Boulder",
2099+
"country_code": "US",
2100+
"country_name": "United States",
2101+
"ip": "198.11.31.184",
2102+
"latitude": "40.015",
2103+
"longitude": "-105.2706",
2104+
"metro_code": "751",
2105+
"region_code": "CO",
2106+
"region_name": "Colorado",
2107+
"time_zone": "America/Denver",
2108+
"zip_code": "80309"
2109+
},
2110+
"date": "Thu, Mar 01, 2018 10:37:44 PM",
2111+
"description": "",
2112+
"link": {
2113+
"jiuzhangsuanfa": "http://www.jiuzhang.com/solution/longest-common-substring/",
2114+
"lintcode": "http://www.lintcode.com/en/problem/longest-common-substring/"
2115+
},
2116+
"notes": "",
2117+
"problem": "Longest Common Substring",
2118+
"tags": []
20942119
}
20952120
}
20962121
}

message.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
83 Reverse 3-digit Integer.
1+
84 Longest Common Substring.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
https://stackoverflow.com/questions/30046411/define-multiple-tasks-in-vscode
2+
3+
{ "command": "myCommand"
4+
"args": ["myCommandArguments"],
5+
"tasks" : [
6+
{ "taskName": "myTask",
7+
"args": ["myTaskArguments"],
8+
"suppressTaskName": false,
9+
}
10+
]
11+
}
12+
13+
The above definition will result in the following command:
14+
15+
myCommand myCommandArguments myTaskArguments myTask
16+
17+
The task name myTask is always last. Since version 0.4 it can be omitted with "suppressTaskName": true.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"${workspaceRoot}",
7+
"${workspaceRoot}/../../include",
8+
"/usr/include",
9+
"/usr/local/include"
10+
],
11+
"defines": [],
12+
"intelliSenseMode": "clang-x64",
13+
"browse": {
14+
"path": [
15+
"/usr/include",
16+
"/usr/local/include",
17+
"${workspaceRoot}/../../include",
18+
"${workspaceRoot}"
19+
],
20+
"limitSymbolsToIncludedHeaders": true,
21+
"databaseFilename": ""
22+
},
23+
"macFrameworkPath": [
24+
"/System/Library/Frameworks",
25+
"/Library/Frameworks"
26+
]
27+
},
28+
{
29+
"name": "Linux",
30+
"includePath": [
31+
"${workspaceRoot}",
32+
"${workspaceRoot}/../../include",
33+
"/usr/include",
34+
"/usr/local/include",
35+
"/usr/include/x86_64-linux-gnu/c++/5",
36+
"/usr/lib/gcc/x86_64-linux-gnu/5/include",
37+
"/usr/include/c++/5"
38+
],
39+
"defines": [],
40+
"intelliSenseMode": "clang-x64",
41+
"browse": {
42+
"path": [
43+
"/usr/include",
44+
"/usr/local/include",
45+
"${workspaceRoot}/../../include",
46+
"${workspaceRoot}"
47+
],
48+
"limitSymbolsToIncludedHeaders": true,
49+
"databaseFilename": ""
50+
}
51+
},
52+
{
53+
"name": "Win32",
54+
"includePath": [
55+
"${workspaceRoot}",
56+
"${workspaceRoot}/../../include",
57+
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/include/*",
58+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
59+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
60+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
61+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/winrt",
62+
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.10.25017/include/*",
63+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um",
64+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt",
65+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared",
66+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/winrt"
67+
],
68+
"defines": [
69+
"_DEBUG",
70+
"UNICODE"
71+
],
72+
"intelliSenseMode": "msvc-x64",
73+
"browse": {
74+
"path": [
75+
"${workspaceRoot}",
76+
"${workspaceRoot}/../../include",
77+
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/include/*",
78+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
79+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
80+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
81+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/winrt",
82+
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.10.25017/include/*",
83+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um",
84+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt",
85+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared",
86+
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/winrt"
87+
],
88+
"limitSymbolsToIncludedHeaders": true,
89+
"databaseFilename": ""
90+
}
91+
}
92+
],
93+
"version": 3
94+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [{
4+
"name": "(gdb) Launch",
5+
"type": "cppdbg",
6+
"request": "launch",
7+
"program": "${workspaceRoot}/solution.exe",
8+
"args": [],
9+
"stopAtEntry": false,
10+
"cwd": "${workspaceRoot}",
11+
"environment": [],
12+
"externalConsole": true,
13+
"MIMode": "gdb",
14+
"setupCommands": [{
15+
"description": "Enable pretty-printing for gdb",
16+
"text": "-enable-pretty-printing",
17+
"ignoreFailures": true
18+
}]
19+
},
20+
{
21+
"name": "(Windows) Launch",
22+
"type": "cppvsdbg",
23+
"request": "launch",
24+
"program": "${workspaceRoot}/solution.exe",
25+
"args": [],
26+
"stopAtEntry": false,
27+
"cwd": "${workspaceRoot}",
28+
"environment": [],
29+
"externalConsole": false
30+
}
31+
]
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"climits": "cpp"
4+
}
5+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"version": "0.1.0",
3+
"tasks": [
4+
{
5+
"taskName": "build",
6+
"args": [],
7+
"isBuildCommand": true,
8+
"showOutput": "silent",
9+
"suppressTaskName": true
10+
}
11+
],
12+
"windows": {
13+
"command": "build.bat",
14+
"isShellCommand": true,
15+
"showOutput": "always"
16+
},
17+
"linux": {
18+
"command": "g++",
19+
"args": [
20+
"-g",
21+
"-std=c++11",
22+
"-o",
23+
"solution.exe",
24+
"solution.cpp"
25+
],
26+
"isShellCommand": true,
27+
"showOutput": "always",
28+
"problemMatcher": {
29+
"owner": "cpp",
30+
"fileLocation": [
31+
"relative",
32+
"${workspaceRoot}"
33+
],
34+
"pattern": {
35+
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
36+
"file": 1,
37+
"line": 2,
38+
"column": 3,
39+
"severity": 4,
40+
"message": 5
41+
}
42+
}
43+
},
44+
"osx": {
45+
"command": "clang++",
46+
"args": [
47+
"solution.cpp",
48+
"-v"
49+
],
50+
"isShellCommand": true,
51+
"showOutput": "always",
52+
"problemMatcher": {
53+
"owner": "cpp",
54+
"fileLocation": [
55+
"relative",
56+
"${workspaceRoot}"
57+
],
58+
"pattern": {
59+
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
60+
"file": 1,
61+
"line": 2,
62+
"column": 3,
63+
"severity": 4,
64+
"message": 5
65+
}
66+
}
67+
}
68+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@echo off
2+
pushd %cd%
3+
REM call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
4+
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
5+
popd
6+
echo %cd%
7+
set compilerflags=/Od /Zi /EHsc
8+
set linkerflags=/OUT:solution.exe
9+
set source=../../include/fileIO/fileIO.cpp ../../include/dataStructure/dataStructure.cpp ../../include/misc/misc.cpp
10+
11+
cl.exe %compilerflags% solution.cpp %source% /link %linkerflags%
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <utility>
3+
#include <vector>
4+
#include <list>
5+
#include <set>
6+
#include <unordered_set>
7+
#include <unordered_map>
8+
#include <map>
9+
#include <queue>
10+
#include <stack>
11+
#include <forward_list>
12+
#include <algorithm>
13+
#include <climits>
14+
#include <string>
15+
#include <cstring>
16+
#include <cstdlib>
17+
#include <numeric>
18+
#include <functional>
19+
#include <iterator>
20+
21+
#include "../../include/fileIO/fileIO.h"
22+
#include "../../include/dataStructure/dataStructure.h"
23+
#include "../../include/misc/misc.h"
24+
25+
using namespace std;
26+
27+
class Solution
28+
{
29+
public:
30+
/**
31+
* @param A: A string
32+
* @param B: A string
33+
* @return: the length of the longest common substring.
34+
*/
35+
int longestCommonSubstring(string &A, string &B)
36+
{
37+
// write your code here
38+
int m = A.size();
39+
int n = B.size();
40+
if (m == 0 || n == 0)
41+
return 0;
42+
std::vector<std::vector<int>> f(m + 1, std::vector<int>(n + 1, 0));
43+
44+
int max = 0;
45+
// longest substring till index i, covering (0, i)
46+
for (int i = 1; i <= m; ++i)
47+
{
48+
for (int j = 1; j <= n; ++j)
49+
{
50+
if (A[i - 1] == B[j - 1])
51+
{
52+
f[i][j] = f[i - 1][j - 1] + 1;
53+
max = std::max(max, f[i][j]);
54+
}
55+
else
56+
{
57+
f[i][j] = 0;
58+
}
59+
}
60+
}
61+
62+
return max;
63+
}
64+
};
65+
66+
int main(int argc, char **argv)
67+
{
68+
// initialization, data preparation
69+
std::string A = "ABCD";
70+
std::string B = "CBCE";
71+
72+
// my solution
73+
Solution sln;
74+
75+
// correct answer
76+
auto x = sln.longestCommonSubstring(A, B);
77+
78+
return EXIT_SUCCESS;
79+
}

0 commit comments

Comments
 (0)