1+ #include < string>
2+ #include < vector>
3+ #include < iostream>
4+ #include < sstream>
5+ #include < regex>
6+ #include < algorithm>
7+ #include < unordered_map>
8+ using namespace std ;
9+ 10+ struct user {
11+ string code;
12+ int score;
13+ };
14+ 15+ user users[50001 ];
16+ vector<string> candidates;
17+ unordered_map<string, pair<int , int >> m;
18+ 19+ const string ops[4 ][3 ] = { {" java" , " python" , " cpp" }, {" frontend" ," backend" },{" junior" ," senior" },{" pizza" ," chicken" }};
20+ 21+ bool cmp (user &u1, user &u2) {
22+ if (u1.code < u2.code )
23+ return true ;
24+ else if (u1.code == u2.code ) {
25+ if (u1.score < u2.score )
26+ return true ;
27+ else
28+ return false ;
29+ }
30+ else
31+ return false ;
32+ }
33+ 34+ void getCandidate (string s[], string code, int idx){
35+ if (idx > 3 ) {
36+ candidates.push_back (code);
37+ return ;
38+ }
39+ 40+ if (s[idx] != " -" ) {
41+ getCandidate (s, code + s[idx], idx + 1 );
42+ }
43+ else {
44+ int end = 2 ;
45+ if (idx == 0 )
46+ end = 3 ;
47+ for (int i = 0 ; i < end; ++i)
48+ getCandidate (s, code + ops[idx][i], idx + 1 );
49+ }
50+ }
51+ 52+ vector<int > solution (vector<string> info, vector<string> query) {
53+ vector<int > answer;
54+ int row = info.size ();
55+ 56+ for (int i = 0 ; i < info.size (); ++i) {
57+ string s[5 ];
58+ istringstream sst (info[i]);
59+ sst >> s[0 ] >> s[1 ] >> s[2 ] >> s[3 ] >> s[4 ];
60+ 61+ string code;
62+ for (int i = 0 ; i < 4 ; ++i)
63+ code += s[i];
64+ 65+ users[i] = {code, stoi (s[4 ])};
66+ }
67+ 68+ sort (users, users + row, cmp);
69+ int s=0 , e=0 ;
70+ string prev = users[0 ].code ;
71+ for (int i = 0 ; i < row; ++i) {
72+ if (prev != users[i].code ) {
73+ e = i - 1 ;
74+ if (e!=-1 )
75+ m.insert ({ prev, { s,e } });
76+ 77+ prev = users[i].code ;
78+ s = i;
79+ e = i;
80+ }
81+ }
82+ m.insert ({ prev, { s,row-1 } });
83+ 84+ for (auto a : query) {
85+ string b = regex_replace (a,regex (" and " )," " );
86+ string s[5 ];
87+ istringstream sst (b);
88+ sst >> s[0 ] >> s[1 ] >> s[2 ] >> s[3 ] >> s[4 ];
89+ 90+ candidates.clear ();
91+ getCandidate (s, " " , 0 );
92+ 93+ int count = 0 ;
94+ int want = stoi (s[4 ]);
95+ 96+ for (auto b : candidates) {
97+ if (m.count (b) > 0 ) {
98+ pair<int , int > pos = m[b];
99+ int left = pos.first ;
100+ int right = pos.second ;
101+ int mid, lower_bnd=-1 ;
102+ 103+ while (left <= right){
104+ mid = (left + right) >> 1 ;
105+ if (users[mid].score >= want){
106+ lower_bnd = mid;
107+ right = mid - 1 ;
108+ }
109+ else left = mid + 1 ;
110+ }
111+ if (lower_bnd!=-1 )
112+ count += pos.second -lower_bnd+1 ;
113+ 114+ }
115+ }
116+ 117+ answer.push_back (count);
118+ }
119+ 120+ return answer;
121+ }
122+ 123+ int main () {
124+ vector<string> info = { " java backend junior pizza 150" , " python frontend senior chicken 210" , " python frontend senior chicken 150" , " cpp backend senior pizza 260" , " java backend junior chicken 80" , " python backend senior chicken 50" };
125+ vector<string> query = { " java and backend and junior and pizza 100" , " python and frontend and senior and chicken 200" , " cpp and - and senior and pizza 250" , " - and backend and senior and - 150" , " - and - and - and chicken 100" , " - and - and - and - 150" };
126+ vector<int > answer = solution (info, query);
127+ 128+ for (auto a : answer)
129+ cout << a << " " ;
130+ return 0 ;
131+ }
0 commit comments