-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge_Emphasized_String.cpp
133 lines (113 loc) · 3.05 KB
/
Merge_Emphasized_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Reena has to sent the mails regularly.
// She used emphasize any word W based on a given set of words[].
// Two emphasize the words she used enclose them with <i> and </i> tag.
// The emphasize procedure of the sub-words of W is as follows:
// - If two sub-words are intersected with each other,
// enclose them with one <i></i> tag.
// - If two sub-words are enclosed with <i></i> tag and neighbours also,
// then merge the sub-words as one and enclose with one <i> </i> tag.
// You will be given the word W, and set of words[] to emphasize.
// Your task is to help Reena to emphasize the word W.
// and print it.
// Input Format:
// -------------
// Line-1: A string W, the word.
// Line-2: space separated strings, set of words[].
// Output Format:
// --------------
// Print the string, the emphasized string.
// Sample Input-1:
// ---------------
// kmitngit123
// it 123
// Sample Output-1:
// ----------------
// km<i>it</i>ng<i>it123</i>
// Sample Input-2:
// ---------------
// qwertykeypad
// qwerty key pad
// Sample Output-2:
// ----------------
// <i>qwertykeypad</i>
// Sample Input-3:
// ---------------
//aaaabbbbbcccccccdddddddeeeeeeeefffffffffffff
// aaa ab bbc ccc cdd dee eff fff
// Sample Output-3:
// ----------------
// output =<i>aaaab</i>bb<i>bbcccccccdd</i>dddd<i>dee</i>eeeee<i>efffffffffffff</i>
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
vector<string> v;
string s1;
cin.ignore();
getline(cin,s1);
string t1;
stringstream x1(s1);
while(getline(x1,t1,' ')){
v.push_back(t1);
}
//finding intervals
vector<vector<int>> nums;
for(auto x:v){
for(int i=0;i<s.size()-x.size()+1;i++){
if(s.substr(i,x.size())==x){
vector<int> p;
p.push_back(i);
p.push_back(i+x.size()-1);
nums.push_back(p);
}
}
}
//sorting intervals
sort(nums.begin(),nums.end(),[&](vector<int> a,vector<int> b){
if(a[0]<b[0]){
return true;
}
else if(a[0]==b[0]){
if(a[1]<b[1]){
return true;
}
else{
return false;
}
}
else{
return false;
}
});
//merging intervals
vector<vector<int>> arr;
if(nums.size()!=0){
arr.push_back(nums[0]);
for(int i=1;i<nums.size();i++){
if(nums[i][0]-arr.back()[1]<=1){
arr.back()[0]=min(arr.back()[0],nums[i][0]);
arr.back()[1]=max(arr.back()[1],nums[i][1]);
}
else{
arr.push_back(nums[i]);
}
}
}
//finding result
string res="";
int prev=0;
for(auto x:arr){
string a="";
if(x[0]!=0){
a=s.substr(prev,x[0]-prev);
}
string b=s.substr(x[0],x[1]-x[0]+1);
res=res+a+"<i>"+b+"</i>";
prev=x[1]+1;
}
if(prev!=s.size()){
res=res+s.substr(prev,s.size()-prev);
}
cout<<res;
}