forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path616.Add-Bold-Tag-in-String.cpp
52 lines (47 loc) · 1.15 KB
/
616.Add-Bold-Tag-in-String.cpp
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
class Solution {
public:
string addBoldTag(string s, vector<string>& dict)
{
int N=s.size();
vector<int>p(N,0);
for (int i=0; i<dict.size(); i++)
{
int len=dict[i].size();
string str=dict[i];
for (int j=0; j<=N-len; j++)
{
if (s.substr(j,len)==str)
{
for (int k=j; k<j+len; k++)
p[k]=1;
}
}
}
/*
for (int i=0; i<p.size(); i++)
cout<<p[i];
*/
string result;
int i=0;
while (i<N)
{
while (i<N && p[i]==0)
{
result+=s[i];
i++;
}
if (i==N)
return result;
else
{
result+="<b>";
int i0=i;
while (i<N && p[i]==1)
i++;
result+=s.substr(i0,i-i0);
result+="</b>";
}
}
return result;
}
};