-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMAY17WSITES012.cpp
More file actions
113 lines (106 loc) · 4.35 KB
/
MAY17WSITES012.cpp
File metadata and controls
113 lines (106 loc) · 4.35 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
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
#include<algorithm>
#include<string>
#include<iostream>
#define endl '\n' //Obvious: to avoid flushes.
using namespace std;
int main()
{ long unsigned int n;
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin>>n;
pair<string, char> sites [n];
char x;
string name;
for(int i=0; i<n; i++)
{ cin>>x>>name;
sites[i]={name, x};
}
sort(sites, sites+n);//This sorts all the website, making the most similar ones next to each other.
int filter=0;
string filters[n];
for(int i=0; i<n; i++)
{ if(sites[i].second == '+')//Is any legit website blocked by a previous filter? (test case # 13 & 14 will pass due to this.)
{ for(int k=0; k<filter; k++)
{ if(filters[k][0]!=(sites[i].first)[0]) //There's no point in comparing the filter and the website if the first char don't match.
continue;
size_t found = (sites[i].first).find(filters[k]);
if(found==0) //This means the filter is found at starting of ith string.
{ cout<<-1; return 0;
}
}
}
else
{
//Let's check if current website is already blocked or not.
bool blockedornot=0;
for(int k=0; k<filter; k++)
{ if(filters[k][0]!=(sites[i].first)[0]) //There's no point in comparing the filter and the website if the first char don't match.
continue;
size_t found = (sites[i].first).find(filters[k]);
if(found==0) //This means the filter is found at starting of ith string.
{ blockedornot=1;
break;
}
}
if(blockedornot==1)//Do nothing if current website is already blocked.
continue;
int temp=filter;
for(int j=i-1; j>-1; j--)
{ if(sites[j].second=='+') //To find the first allowed website just before the current one.
{ int k=0;
char newfilter[(sites[i].first).length()];
while((k<(sites[i].first).length()) && (k<(sites[j].first).length()) && ((sites[i].first)[k]==(sites[j].first)[k])) //To add all the same characters to the filter.
{ newfilter[k]=(sites[i].first)[k];
k++;
}
if(k==(sites[i].first).length())//This means ith website is subset of jth website. So designing a filter is impossible task...
{ cout<<-1;
return 0;
}
newfilter[k]=(sites[i].first)[k]; //To add the first differing character.
newfilter[k+1]='\0'; //To remove the garbage characters.
filters[filter++]=newfilter;
break;
}
}
//This finds the lexographically previous allowed (+) website, before the current one.
//Then, compares it char by char with the current website, makes a filter, and adds it into the list of filters.
for(int j=i; j<n; j++)
{ if(sites[j].second=='+')
{ int k=0;
char newfilter[(sites[i].first).length()];
while(k<(sites[i].first).length() && (k<(sites[j].first).length()) && (sites[i].first)[k]==(sites[j].first)[k])
{ newfilter[k]=(sites[i].first)[k];
k++;
}
if(k==(sites[i].first).length())
{ cout<<-1;
return 0;
}
newfilter[k]=(sites[i].first)[k];
newfilter[k+1]='\0';
if(filter>0 && k>=filters[filter-1].length() && 0 == (sites[i].first).find(filters[filter-1]))//If this overlaps with the last filter.
{ filters[filter-1]=newfilter;
}//e.g. last filter -> "codec" & current one is "codechef".
else if(filter==0 || 0 != (sites[i].first).find(filters[filter-1]))
{ filters[filter++]=newfilter;
}//If this is the first ever filter to be stored or this is the first filter of ith (THIS) website.
break;
}
}// This just finds the lexographically next allowed (+) website after the current blocked website.
//Then, compares it char by char with the current website, makes a filter, which replaces the previous filter if be long enough.
if(temp==filter && filter>0 && filters[filter-1][0]!=(sites[i].first)[0])
{ char newfilter=(sites[i].first) [0];
filters[filter++]=newfilter;
}
else if(temp==filter && filter==0)
{ char newfilter=(sites[i].first) [0];
filters[filter++]=newfilter;
}
}
}
cout<<filter<<endl; //Print the number of filters.
for(int i=0; i<filter; i++)
cout<<filters[i]<<endl; //And, the filters.
return 0;
}