Givena set of keywords words and a string S, make all appearances of allkeywords in S bold.Any letters between <b> and </b> tags become bold.
Thereturned string should use the least number of tags possible, and of course thetags should form a valid combination.
Forexample, given that words = ["ab", "bc"] and S ="aabcd", we shouldreturn "a<b>abc</b>d". Note that returning "a<b>a<b>b</b>c</b>d" would use more tags, so it isincorrect.
Note:
1. words has length in range [0, 50].
2. words[i] has length in range [1, 10].
3. S has length in range [0, 500].
4. All characters in words[i] and S are lowercase letters.
给你一个字符数组和一个字符串S,让你把S中所有字符数组中出现的字符都做重点标记,重点标记使用 <b> and </b> 来实现,这里要求使用的标记数量最少,
这题我花了一个小时都没想出来,因为我题目看不懂,make all appearances of all keywords in S bold这一句没看懂,后来再看一看,其实这一句就是把所有的S中的keywords都做重点标记,Any letters between <b> and </b> tags become bold是指怎样重点标记,看懂了这一句之后题目就变得非常的简单了,注意bold是重点标记的意思,也就是加粗的意思,
以后在做题的时候记得当题目看不懂的时候记得使用谷歌翻译,然后借鉴一下,这样说不定就看懂了
这一题还要注意一点,就是对于string.find_first_of(stringa)而言,返回的是a中的任何一个字符在string 中第一次出现的位置,而不是a在string中出现的第一次,要找a在string中出现的第一次请使用string.find()以后一定要注意!!!
class Solution {
public:
string boldWords(vector<string>& words, string S) {
int start=0;
vector<bool> mark(S.size(), false);
for (int i = 0; i < words.size(); i++)
{
start= S.find(words[i], 0);
while (start != -1)
{
for (int j = start; j < start + words[i].size(); j++)
{
mark[j] = true;
}
start = S.find(words[i], start+1);
}
}
int now = 0;
string tmp = "";
for (int i = 0; i < S.size(); i++)
{
if (mark[i] == true)
{
if (now == 0) {
tmp.insert(tmp.size(), "<b>"); now = 1;
}
}
else
{
if (now == 1) {
tmp.insert(tmp.size(), "</b>"); now = 0;
}
}
tmp.push_back(S[i]);
}
if (now == 1)
{
tmp.insert(tmp.size(), "</b>");
}
return tmp;
}
};