-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48_LongestString_Without_Duplication.cpp
More file actions
77 lines (56 loc) · 2.07 KB
/
48_LongestString_Without_Duplication.cpp
File metadata and controls
77 lines (56 loc) · 2.07 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by mark on 2019/7/22.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:48.找字符串中最长的不包含重复字符的子字符串
2. 思路:动态规划。从左到右扫描字符,计算第i个字符为结尾不包含重复字符的最长长度,如果前面没出现过,则f(i)=f(i-1)+1;如果出现过:
分成两种情况:1. d <= f(i-1),说明上次出现的字符在上个最长字符序列中,更新d为两个重复字符之间的距离
2. d > f(i-1),说明重复的字符没在上个最长子序列中,继续向后遍历,d加1即可;
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
using namespace std;
// 动态规划,分成两种情况:
// 1. 重复距离d比上个重复的子序列小
// 2. 重复距离d比上个重复的子序列大,继续累加
int longSubstingWithoutDuplicaton(const string &str)
{
int curLength = 0;
int maxLength = 0;
int* position = new int[26]; // 记录每个字符是否出现,未出现为-1,出现过则置为在字符串中出现的位置
for(int i = 0; i < 26; ++i)
position[i] = -1;
for(int i = 0; i < str.length(); ++i)
{
int prev = position[str[i] - 'a']; // 记录上个重复字符出现的字符
if(prev < 0 || i - prev > curLength) // 未重复字符,或者重复距离比前面子序列大(情况2)
++curLength;
else
{
if(curLength > maxLength)
maxLength = curLength;
curLength = i - prev; // 情况1,更新最大距离d
}
position[str[i] - 'a'] = i; // 记录上个重复字符的位置
}
delete[] position;
return maxLength;
}
int main()
{
string str = "arabcacfr";
int maxLen = longSubstingWithoutDuplicaton(str);
cout << maxLen << endl;
return 0;
}