Skip to content

Commit 0cbde81

Browse files
committed
add new questions
1 parent 07a47cb commit 0cbde81

File tree

131 files changed

+2998
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+2998
-8
lines changed

CodeChef/FLOW016.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
5+
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
6+
7+
typedef long long ll;
8+
typedef long double ld;
9+
typedef pair<int,int> pii;
10+
11+
#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
12+
#define endl "\n"
13+
14+
#define fi first
15+
#define se second
16+
#define pb push_back
17+
18+
#define sz(x) (ll)(x.size())
19+
#define all(x) x.begin(),x.end()
20+
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
21+
#define ms(x,a) memset(x,a,sizeof(x))
22+
23+
#define INF 0x3f3f3f3f
24+
#define INFLL 0x3f3f3f3f3f3f3f3f
25+
26+
#define mod 1000000007LL
27+
#define MAXN 200010
28+
29+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
30+
ll T,N,M,K;
31+
32+
ll MDC(ll a, ll b) { return b ? MDC(b,a%b) : a; }
33+
ll MMC(ll a,ll b) { return a*(b/MDC(a,b)); }
34+
35+
void solve(){
36+
cout << MDC(N,M) << " " << MMC(N,M) << endl;
37+
}
38+
39+
int main(){
40+
41+
optimize;
42+
43+
cin >> T;
44+
45+
while(T--){
46+
cin >> N >> M;
47+
48+
solve();
49+
}
50+
51+
return 0;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp",
4+
"chrono": "cpp",
5+
"tuple": "cpp"
6+
}
7+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
5+
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
6+
7+
typedef long long ll;
8+
typedef long double ld;
9+
typedef pair<int,int> pii;
10+
11+
#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
12+
#define endl "\n"
13+
14+
#define fi first
15+
#define se second
16+
#define pb push_back
17+
18+
#define sz(x) (ll)(x.size())
19+
#define all(x) x.begin(),x.end()
20+
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
21+
#define ms(x,a) memset(x,a,sizeof(x))
22+
23+
#define INF 0x3f3f3f3f
24+
#define INFLL 0x3f3f3f3f3f3f3f3f
25+
26+
#define mod 1000000007LL
27+
#define MAXN 200010
28+
29+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
30+
ll T,N,M,K;
31+
string v[MAXN];
32+
33+
void solve(){
34+
35+
multiset<pii> sP, sN, sN2, sN3;
36+
37+
for(int i=0;i<N;i++){
38+
int aux = 0, menor = INF, abre = 0;
39+
40+
for(int j=0;j<v[i].size();j++){
41+
if(v[i][j] == '(') {
42+
aux++;
43+
abre++;
44+
}
45+
else{
46+
aux--;
47+
if(abre) abre--;
48+
}
49+
50+
menor = min(menor, aux);
51+
}
52+
53+
if(menor >= 0){
54+
sP.insert(pii(-menor, i));
55+
}
56+
else{
57+
if(abre >= 0 && aux >= 0) sN.insert(pii(menor, i));
58+
else if(abre >= 0) sN2.insert(pii(-abre , i));
59+
else sN3.insert(pii(-menor , i));
60+
}
61+
}
62+
63+
string aux;
64+
vector<int> ans(N,0);
65+
66+
int cnt = 0, pos = 0;
67+
68+
for(auto i : sP){
69+
aux += v[ i.se ];
70+
ans[pos++] = i.se +1;
71+
}
72+
73+
//Olhando o balancemanto ate agora
74+
cnt = 0;
75+
76+
for(int i=0;i<aux.size();i++){
77+
if(aux[i] == '(') cnt++;
78+
else cnt--;
79+
80+
if(cnt < 0){
81+
cout << "NO\n";
82+
return;
83+
}
84+
}
85+
86+
//Busca binaria para decidir quem coloco
87+
while(!sN.empty()){
88+
int temp = cnt;
89+
90+
//Vejo a partir de qual eu posso pegar
91+
auto it = sN.lower_bound(pii(-cnt,-INF));
92+
//Pego
93+
if(it == sN.end()){
94+
cout << "NO\n";
95+
return;
96+
}
97+
98+
aux += v[ (*it).se ];
99+
ans[pos++] = (*it).se +1;
100+
101+
//Atualizo o cnt
102+
string str = v[(*it).se];
103+
for(int l=0;l<str.size();l++){
104+
if(str[l] == '(') cnt++;
105+
else cnt--;
106+
}
107+
108+
//Removo
109+
sN.erase(it);
110+
}
111+
112+
//Volto ao normal
113+
for(auto i : sN2){
114+
aux += v[ i.se ];
115+
ans[pos++] = i.se +1;
116+
}
117+
118+
for(auto i : sN3){
119+
aux += v[ i.se ];
120+
ans[pos++] = i.se +1;
121+
}
122+
123+
//Verifico se a string gerada está correta ou não
124+
cnt = 0;
125+
126+
for(int i=0;i<aux.size();i++){
127+
if(aux[i] == '(') cnt++;
128+
else cnt--;
129+
130+
if(cnt < 0){
131+
cout << "NO\n";
132+
return;
133+
}
134+
}
135+
136+
if(cnt){
137+
cout << "NO\n";
138+
return;
139+
}
140+
141+
cout << "YES\n";
142+
for(int i=0;i<N;i++) cout << ans[i] << " \n"[i==N-1];
143+
}
144+
145+
int main(){
146+
147+
optimize;
148+
149+
cin >> N;
150+
151+
for(int i=0;i<N;i++) cin >> v[i];
152+
153+
solve();
154+
155+
return 0;
156+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
5+
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
6+
7+
typedef long long ll;
8+
typedef long double ld;
9+
typedef pair<int,int> pii;
10+
11+
#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
12+
#define endl "\n"
13+
14+
#define fi first
15+
#define se second
16+
#define pb push_back
17+
18+
#define sz(x) (ll)(x.size())
19+
#define all(x) x.begin(),x.end()
20+
#define FOR(x,a,n) for(int x= (int)(a);(x) < int(n);(x)++)
21+
#define ms(x,a) memset(x,a,sizeof(x))
22+
23+
#define INF 0x3f3f3f3f
24+
#define INFLL 0x3f3f3f3f3f3f3f3f
25+
26+
#define mod 1000000007LL
27+
#define MAXN 200010
28+
29+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
30+
ll T,N,M,K;
31+
string s;
32+
33+
struct KMP{
34+
int m;
35+
string q;
36+
vector<int> lps;
37+
38+
KMP(string &_q) : q{_q} {
39+
m = q.size();
40+
pre_process();
41+
}
42+
43+
void pre_process(){
44+
lps.assign(m+1,-1);
45+
46+
for(int i=0, j=-1; i<m ; ){
47+
while(j>=0 && q[i] != q[j]) j = lps[j];
48+
lps[++i] = ++j;
49+
}
50+
}
51+
52+
vector<int> match(string &p){
53+
vector<int> ans;
54+
55+
ll n = p.size();
56+
57+
if(n < m) return ans;
58+
59+
for(int i=0,j=0; i<n ; ){
60+
while( j>=0 && p[i] != q[j]) j = lps[j];
61+
i++; j++;
62+
63+
if(j == m){
64+
ans.push_back(i-j);
65+
j = lps[j];
66+
}
67+
}
68+
return ans;
69+
}
70+
};
71+
72+
void solve(){
73+
74+
string aux = "happiness";
75+
76+
KMP kmp(aux);
77+
78+
vector<int> ans = kmp.match(s);
79+
80+
if(ans.size() == 0){
81+
cout << "YES\n";
82+
83+
vector<int> l[26], dif;
84+
85+
//7 0 15 8 13 4 18
86+
for(int i=0;i<s.size();i++){
87+
if( s[i] - 'a' != 7 && s[i] - 'a' != 0 && s[i] - 'a' != 15 && s[i] - 'a' != 8 &&
88+
s[i] - 'a' != 13 && s[i] - 'a' != 4 && s[i] - 'a' != 18){
89+
dif.push_back(i+1);
90+
}
91+
92+
l[ s[i] - 'a' ].push_back(i+1);
93+
}
94+
95+
for(int i=0;i<26;i++){
96+
if(l[i].size() >= 2){
97+
cout << l[i][0] << " " << l[i][1] << endl;
98+
return;
99+
}
100+
}
101+
102+
if(dif.size() >= 2) {
103+
cout << dif[0] << " " << dif[1] << endl;
104+
return;
105+
}
106+
107+
cout << "1 2\n";
108+
return;
109+
110+
}
111+
else if(ans.size() == 1){
112+
cout << "YES\n";
113+
cout << ans[0]+1 << " " << ans[0]+2 << endl;
114+
}
115+
else if(ans.size() == 2){
116+
cout << "YES\n";
117+
cout << ans[0]+1 << " " << ans[1]+2 << endl;
118+
}
119+
else if(ans.size() >= 3){
120+
cout << "NO\n";
121+
}
122+
123+
}
124+
125+
int main(){
126+
127+
optimize;
128+
129+
cin >> s;
130+
131+
solve();
132+
133+
return 0;
134+
}

0 commit comments

Comments
 (0)