Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <sstream> #include <vector> #include <algorithm> #include <cstring> #include <cstdlib> #include <iostream> #include <string> #include <cassert> #include <ctime> #include <map> #include <math.h> #include <cstdio> #include <set> #include <deque> #include <memory.h> #include <queue> #pragma comment(linker, "/STACK:64000000") typedef long long ll; using namespace std; const int MAXK = 19; const int MAXN = 1 << MAXK; const int INF = 2e9; char s[MAXN]; int main() { #ifdef _MSC_VER freopen("input.txt", "r", stdin); #endif bool first = 1; while (scanf("%s", s) == 1) { if (first) first = 0; else printf("\n"); int n = strlen(s); vector<vector<int> > frst(n + 2, vector<int>(26 + 1, n + 1)); for (int i = n - 1; i >= 0; i--) { frst[i][s[i] - 'a'] = i + 1; for (int j = s[i] - 'a' + 1; j <= 26; j++) { frst[i][j] = frst[frst[i][j - 1]][j - 1]; } for (int j = 0; j < s[i] - 'a'; j++) { frst[i][j] = frst[frst[i][26]][j]; } } vector<vector<int> > nxt(n + 2, vector<int>(MAXK)); for (int i = n + 1; i >= 0; i--) { for (int j = 0; j < MAXK; j++) { if (j == 0) nxt[i][j] = frst[i][26]; else nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; } } int q; scanf("%d", &q); for (int it = 0; it < q; it++) { int l, r; scanf("%d%d", &l, &r); --l; --r; bool ok = 1; if (l == r) ok = 0; else { int x = s[l] - 'a'; int p = l; p = frst[p][26]; for (int j = MAXK - 1; j >= 0; j--) { if (nxt[p][j] <= r + 1) p = nxt[p][j]; } ok = p == r + 1; } printf("%s\n", (ok ? "Yes" : "No")); } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<cstdio> #include<cstring> const int M=500009,INF=100000000; int n,i,j,q,l,r,a[M],f[M][20],dp[M][30]; char s[M]; int read() { int x=0; char ch=getchar(); while (ch<48 || ch>57) ch=getchar(); while (ch>=48 && ch<=57) x=(x<<1)+(x<<3)+ch-48,ch=getchar(); return x; } int main() { scanf("%s",s+1),n=strlen(s+1); for (i=1;i<=n;++i) a[i]=s[i]-96,dp[i][a[i]]=i+1; for (i=n;i;--i) { for (j=a[i]+1;j<27;++j) if (dp[i][j-1]) dp[i][j]=dp[dp[i][j-1]][j-1]; if (dp[i][26]) dp[i][0]=dp[dp[i][26]][26]; for (j=1;j<a[i];++j) if (dp[i][0]) dp[i][j]=dp[dp[i][0]][j]; if (dp[i][0]) f[i][0]=dp[i][0]; } for (i=1;i<19;++i) for (j=1;j<=n+1;++j) f[j][i]=f[f[j][i-1]][i-1]; for (q=read();q;--q) { l=read(),r=read(),++r; for (i=18;~i;--i) if (f[l][i]<=r && f[l][i]) l=f[l][i]; if (l==r) puts("Yes"); else puts("No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<cstdio> #include<cstring> #include<algorithm> #define rep(i,a,b) for(i=a;i<=b;i++) using namespace std; const int N=500010; const int K=25; int dp[N][26]; int fa[N][21]; int a[N];char s[N]; int main(){ //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); int i,j,x,n,last,l,r,Q; scanf("%s",s+1);n=strlen(s+1); rep(i,1,n) a[i]=s[i]-'a'; rep(i,1,n+1) rep(j,0,K) dp[i][j]=-1; rep(i,1,n+1) fa[i][0]=n+2; fa[n+2][0]=n+2; for(i=n;i;i--){ dp[i][a[i]]=i;last=i+1; rep(j,a[i]+1,K){ if(last==n+1) dp[i][j]=-1; else{ if(dp[last][j-1]!=-1){dp[i][j]=dp[last][j-1];last=dp[i][j]+1;} else last=n+1; } } if(dp[i][K]!=-1&&dp[dp[i][K]+1][K]!=-1){ x=dp[dp[i][K]+1][K]+1; rep(j,0,K) if(dp[i][j]==-1) dp[i][j]=dp[x][j]; fa[i][0]=x; } } rep(j,1,20) rep(i,1,n+2) fa[i][j]=fa[fa[i][j-1]][j-1]; scanf("%d",&Q); while(Q--){ scanf("%d%d",&l,&r);r++; for(i=20;i>=0;i--) if(fa[l][i]<=r) l=fa[l][i]; if(l==r) printf("Yes\n"); else printf("No\n"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <string> #include <vector> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <iostream> #include <sstream> #include <cstring> #include <numeric> using namespace std; typedef long long ll; typedef pair<int,int> pii; #define F0(i,n) for (int i = 0; i < n; i++) #define F1(i,n) for (int i = 1; i <= n; i++) #define CL(a,x) memset(x, a, sizeof(x)); #define SZ(x) ((int)x.size()) const double eps = 1e-10; const int inf = 1000000009; int i, j, k, m, n, l, q; const int z = 26; int ans; string s; int d[z + 1][500000]; int level[21][500000]; int main() { //freopen("x.in", "r", stdin); cin >> s; n = SZ(s); for (int i = n - 1; i >= 0; i--) { int c = s[i] - 'a'; d[c][i] = i + 1; for (int j = c + 1; j <= z; j++) { if (d[j - 1][i]) d[j][i] = d[j - 1][d[j - 1][i]]; else break; } if (d[z][i]) for (int j = 0; j < z; j++) if (!d[j][i]) { d[j][i] = d[j][d[z][i]]; } } for (int i = n - 1; i >= 0; i--) if (d[z][i]) { level[0][i] = d[z][i]; F1(j, 20) { if (level[j - 1][i]) { level[j][i] = level[j - 1][level[j - 1][i]]; } else break; } } cin >> q; while (q--) { int L, R; cin >> L >> R; L--; string ans = "No"; if (level[0][L]) { for (int k = 20; k >= 0; k--) { if (level[k][L] && level[k][L] <= R) { if (level[k][L] == R) { ans = "Yes"; break; } L = level[k][L]; } } } cout << ans << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> char str[500000]; int f[27][500002],g[19][500002],n,q; int main() { scanf("%s%d",str,&q),n=strlen(str); for(int i=0;i<27;++i)f[i][n+1]=f[i][n]=n+1; g[0][n+1]=g[0][n]=n+1; for(int i=n-1;i>=0;--i) { f[str[i]-='a'][i]=i+1; for(int j=str[i]+1;j<27;++j) f[j][i]=f[j-1][f[j-1][i]]; if(str[i])f[0][i]=f[0][f[26][i]]; for(int j=1;j<str[i];++j) f[j][i]=std::min(f[j-1][f[j-1][i]],f[j][f[26][i]]); g[0][i]=f[26][i]; } ++n; for(int i=1;i<19;++i) for(int j=0;j<=n;++j) g[i][j]=g[i-1][g[i-1][j]]; for(int i=q,l,r,j;i;--i) { scanf("%d%d",&l,&r),--l,j=18; while(l<r) { while(j&&g[j][l]>r)--j; l=g[j][l]; } puts(l==r?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <cstring> #include <queue> #include <stack> #include <functional> #include <set> #include <map> #include <deque> #define SIZE 500005 #define ALP 27 #define BT 22 using namespace std; int to[SIZE][BT]; int dp[SIZE][ALP]; int nxt[SIZE][ALP]; char str[SIZE]; int main() { int n; scanf("%s",&str); n=strlen(str); memset(dp,-1,sizeof(dp)); memset(nxt,-1,sizeof(nxt)); for(int i=n-1;i>=0;i--) { int c=str[i]-'a'; dp[i][c]=i; for(int j=c+1;j<ALP;j++) { if(dp[i][j-1]==n-1||dp[i][j-1]==-1) break; int to=dp[i][j-1]+1; if(nxt[to][j-1]==-1) break; to=nxt[to][j-1]; if(dp[to][j-1]!=-1) dp[i][j]=dp[to][j-1]; } to[i][0]=dp[i][ALP-1]+(dp[i][ALP-1]==-1?0:1); for(int j=c;j<ALP;j++) nxt[i][j]=i; if(to[i][0]!=-1&&to[i][0]!=n) { for(int j=0;j<c;j++) nxt[i][j]=nxt[to[i][0]][j]; } //printf("%d\n",to[i][0]); /* if(i>=6) { printf("%d %d %d\n",dp[i][ALP-3],dp[i][ALP-2],dp[i][ALP-1]); printf("* %d %d %d\n",nxt[i][ALP-3],nxt[i][ALP-2],nxt[i][ALP-1]); }*/ //puts(""); } //for(int i=0;i<n;i++) printf("%d ",to[i][0]); for(int i=0;i+1<BT;i++) { for(int j=0;j<n;j++) { if(to[j][i]==-1||to[j][i]==n) to[j][i+1]=-1; else to[j][i+1]=to[to[j][i]][i]; } } int Q; scanf("%d",&Q); for(int i=0;i<Q;i++) { int l,r; scanf("%d %d",&l,&r);l--,r--; int now=l; for(int j=BT-1;j>=0;j--) { if(to[now][j]==-1||to[now][j]>r+1) continue; now=to[now][j]; if(now==n) break; } //for(int j=0;j<BT;j++) printf("%d ",to[0][j]);puts(""); //printf("%d\n",now); if(now==r+1) puts("Yes"); else puts("No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <cstring> #include <deque> #include <stack> #include <stdio.h> #include <map> #include <set> #include <time.h> #include <string> #include <fstream> #include <queue> #include <bitset> #include <cstdlib> #include <assert.h> #define X first #define Y second #define mp make_pair #define pb push_back #define pdd pair<double,double> #define pii pair<ll,ll> #define PI 3.14159265358979323846 #define MOD 1000000007 #define MOD2 1000000009 #define INF ((ll)1e+18) #define x1 fldgjdflgjhrthrl #define x2 fldgjdflgrtyrtyjl #define y1 fldggfhfghjdflgjl #define y2 ffgfldgjdflgjl #define N 131072 #define SUM 377 #define MAG 33554431 #define RED 0 #define BLUE 1 #define ALP 26 typedef long long ll; typedef long double ld; using namespace std; ll i,j,n,k,l,m,tot, flag,r,ans,z, K,x1,y1,x2,y2,x3,y3,x,y,h,num,h2,timer,sz,q,nn; string s,t; ll a[500500][28], pa[500050], b[500500][20]; vector<pii> merged[30]; int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); cin >> s; n = s.size(); for (i = 0; i < n; i++) s[i] -= 'a'; for (i = 0; i <= ALP; i++) a[n][i] = a[n+1][i] = n+1; for (i = n-1; i >= 0; i--) { a[i][s[i]] = i+1; for (j = s[i]+1; j <= ALP; j++) a[i][j] = a[a[i][j-1]][j-1]; for (j = 0; j < s[i]; j++) a[i][j] = a[a[i][ALP]][j]; } for (i = 0; i <= n+1; i++) { b[i][0] = a[i][ALP]; } for (j = 0; j < 19; j++) for (i = 0; i <= n+1; i++) b[i][j+1] = b[b[i][j]][j]; cin >> q; for (i = 0; i < q; i++) { cin >> x >> y; x--; for (j = 19; j >= 0; j--) if (b[x][j] <= y) { x = b[x][j]; } if (x == y) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<cmath> #include<string.h> #include<stdio.h> #include<algorithm> using namespace std; char s[500011]; int nxt[26][500011]; int nxt0[500011]; int nxt2[21][500011]; int n; int q; int main() { int i,j,k; int l,r; scanf("%s",s); n=strlen(s); scanf("%d",&q); memset(nxt,-1,sizeof(nxt)); memset(nxt0,-1,sizeof(nxt0)); for(i=n-1;i>=0;i--) { int now=s[i]-'a'; nxt[now][i]=i+1; for(j=now+1;j<26;j++) { if(nxt[j-1][i]!=-1) { nxt[j][i]=nxt[j-1][nxt[j-1][i]]; } } if(nxt[25][i]!=-1) { nxt0[i]=nxt[25][nxt[25][i]]; } if(nxt0[i]!=-1) { for(j=0;j<now;j++) { nxt[j][i]=nxt[j][nxt0[i]]; } } } memset(nxt2,-1,sizeof(nxt2)); for(i=0;i<n;i++) { nxt2[0][i]=nxt0[i]; } for(i=1;i<=20;i++) { for(j=0;j<n;j++) { if(nxt2[i-1][j]==-1) { nxt2[i][j]=-1; } else { nxt2[i][j]=nxt2[i-1][nxt2[i-1][j]]; } } } /* for(i=0;i<n;i++) { for(j=0;j<10;j++) { cout<<nxt2[j][i]<<' '; } cout<<endl; } */ /* for(i=0;i<n;i++) { cout<<nxt0[i]<<' '; } cout<<"!!!"<<endl; */ for(i=1;i<=q;i++) { scanf("%d %d",&l,&r); l--; //cout<<l<<endl; int now=l; for(j=20;j>=0;j--) { if(nxt2[j][now]!=-1 && nxt2[j][now]<=r) { now=nxt2[j][now]; } //cout<<now<<endl; } if(now==r) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll,ll> pll; typedef vector<bool> vb; const ll oo = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-9; #define sz(c) ll((c).size()) #define all(c) begin(c), end(c) #define FOR(i,a,b) for (ll i = (a); i < (b); i++) #define FORD(i,a,b) for (ll i = (b)-1; i >= (a); i--) #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define xx first #define yy second #define has(c,i) ((c).find(i) != end(c)) #define DBGDO(X) ({ if(1) cerr << "DBGDO: " << (#X) << " = " << (X) << endl; }) int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; ll n = sz(s); vvl dp(n,vl(27,n+1)); FORD(i,0,n) { ll k = s[i]-'a'; dp[i][k] = i+1; FOR(j,k+1,27) { if (dp[i][j-1] >= n) continue; dp[i][j] = dp[dp[i][j-1]][j-1]; } FOR(j,0,k) { if (dp[i][26] >= n) continue; dp[i][j] = dp[dp[i][26]][j]; } } vvl step(20, vl(n,n+1)); FOR(i,0,n) step[0][i] = dp[i][26]; FOR(k,1,20) FOR(i,0,n) { if (step[k-1][i] >= n) continue; step[k][i] = step[k-1][step[k-1][i]]; } ll q; cin >> q; while (q--) { ll a, b; cin >> a >> b; a--; FORD(k,0,20) { if (step[k][a] > b) continue; a = step[k][a]; if (a == b) break; } cout << (a == b ? "Yes\n" : "No\n"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; const int N = 500010; const int LOG = 21; const int ALPHA = 28; int pr[N][LOG]; int f[N][ALPHA]; int a[N]; char foo[N]; int main() { scanf("%s", foo); int len = strlen(foo); for (int i = 0; i < len; i++) { a[i] = foo[i] - 'a'; } for (int c = 0; c <= 26; c++) { f[len][c] = -1; } for (int i = len - 1; i >= 0; i--) { for (int c = 0; c <= 26; c++) { f[i][c] = -1; } f[i][a[i]] = i + 1; bool err = false; for (int c = a[i] + 1; c <= 26; c++) { int at = f[f[i][c - 1]][c - 1]; if (at == -1) { err = true; break; } f[i][c] = at; } if (!err) { for (int c = 0; c < a[i]; c++) { f[i][c] = f[f[i][26]][c]; } } } for (int i = 0; i <= len; i++) { pr[i][0] = f[i][26]; } for (int j = 1; j < LOG; j++) { for (int i = 0; i <= len; i++) { if (pr[i][j - 1] == -1) { pr[i][j] = -1; } else { pr[i][j] = pr[pr[i][j - 1]][j - 1]; } } } int tt; scanf("%d", &tt); while (tt--) { int x, y; scanf("%d %d", &x, &y); x--; for (int j = LOG - 1; j >= 0; j--) { if (pr[x][j] == -1) { continue; } if (pr[x][j] <= y) { x = pr[x][j]; } } puts((x == y) ? "Yes" : "No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i < b; ++i) #define REP(i, n) FOR(i, 0, n) #define _ << " " << #define sz(x) ((int) x.size()) #define pb(x) push_back(x) #define TRACE(x) cerr << #x << " = " << x << endl typedef long long ll; typedef pair<int, int> point; const int mod = 1e9 + 7; int add(int x, int y) {x += y; if(x >= mod) return x - mod; return x;} int sub(int x, int y) {x -= y; if(x < 0) return x + mod; return x;} int mul(int x, int y) {return (ll) x * y % mod;} const int MAXN = 5e5 + 5, ABC = 27, LOG = 19; int q, nxt[MAXN][ABC], after[MAXN][LOG]; string s; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); REP(i, MAXN) REP(j, ABC) nxt[i][j] = MAXN; cin >> s; REP(i, sz(s)) nxt[i][ s[i] - 'a' ] = i; for(int i = sz(s) - 1; i >= 0; --i){ FOR(j, s[i] - 'a' + 1, 27) if(nxt[i][j - 1] + 1 < MAXN) nxt[i][j] = nxt[ nxt[i][j - 1] + 1 ][j - 1]; REP(j, s[i] - 'a') if(nxt[i][26] + 1 < MAXN) nxt[i][j] = nxt[ nxt[i][26] + 1 ][j]; } REP(i, sz(s)) after[i][0] = nxt[i][26] + 1; FOR(j, 1, LOG) REP(i, sz(s)){ int x = after[i][j - 1]; if(x >= sz(s)) after[i][j] = MAXN; else after[i][j] = after[x][j - 1]; } cin >> q; REP(z, q){ int l, r; cin >> l >> r; l --; for(int j = LOG - 1; j >= 0; --j){ if(after[l][j] <= r && l != r) l = after[l][j]; } if(l == r) cout << "Yes\n"; else cout << "No\n"; } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <set> #include <map> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <vector> #include <bitset> #include <string> #include <cstdio> #include <cassert> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define mk make_pair #define pb push_back #define fi first #define se second #define REP(i, x, y) for(int i = (int)x; i <= (int)y; i ++) #define FOR(i, x, y) for(int i = (int)x; i < (int)y; i ++) #define PER(i, x, y) for(int i = (int)x; i >= (int)y; i --) #define trace(x) cerr << #x << " " << x << endl; #define dprintf(...) fprintf(stderr, __VA__ARGS__) #define dln() fprintf(stderr, "\n") using namespace std; typedef long long LL; typedef long double db; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VPI; const int N = 500005; const int P = 1e9 + 7; const int inf = 1e9; const LL Inf = 1e15; inline int IN(){ char ch = getchar(); int x = 0, f = 0; while(ch < '0' || ch > '9') ch = getchar(), f = (ch == '-'); while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + ch - 48; ch = getchar(); } return f ? (-x) : x; } inline int Pow(int x, int y, int p){ int an = 1; for(; y; y >>= 1, x = (LL)x * x % p) if(y & 1) an = (LL)an * x % p; return an; } void renew(int &x, int y){ x += y; if(x < 0) x += P; else if(x >= P) x -= P; } template<typename T> inline void chkmin(T &a, const T &b) {if(a > b) a = b;} template<typename T> inline void chkmax(T &a, const T &b) {if(a < b) a = b;} int n, Q; char str[N]; int nxt[N][30], fa[21][N]; int main(){ scanf("%s", str + 1); n = strlen(str + 1); REP(i, 0, n + 1) REP(j, 0, 26) nxt[i][j] = inf; memset(fa, 0x3f, sizeof fa); PER(i, n, 1){ int x = str[i] - 'a'; nxt[i][x] = i + 1; REP(j, x + 1, 26){ nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; if(nxt[i][j] == inf) break; } if(nxt[i][26] != inf){ memcpy(nxt[i], nxt[nxt[i][26]], sizeof(int) * x); } fa[0][i] = nxt[i][26]; REP(j, 1, 19){ fa[j][i] = (fa[j - 1][i] >= inf) ? inf : fa[j - 1][fa[j - 1][i]]; } } scanf("%d", &Q); while(Q--){ int l = IN(), r = IN() + 1; PER(j, 19, 0) if(fa[j][l] <= r) l = fa[j][l]; puts(l == r ? "Yes" : "No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<bitset> #include<stack> #include<unordered_map> #include<utility> #include<cassert> #include<complex> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; const ll mod = 998244353; const ll INF = (1e+18)+7; typedef pair<int, int>P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) #define all(v) (v).begin(),(v).end() typedef pair<ll, ll> LP; typedef long double ld; typedef pair<ld, ld> LDP; const ld eps = 1e-6; const ld pi = acos(-1.0); //typedef vector<vector<ll>> mat; typedef vector<int> vec; using mat = pair<LP, LP>; void debug(vec &v) { int n = v.size(); rep(i, n) { if (i > 0)cout << " "; cout << v[i]; } cout << endl; } ll mod_pow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a%mod; a = a * a%mod; n >>= 1; } return res; } struct perm { private: int sz; vector<ll> p, invp; public: perm(int n) { sz = n + 1; p.resize(sz), invp.resize(sz); p[0] = 1; rep1(i, sz - 1) { p[i] = p[i - 1] * i%mod; } invp[sz - 1] = 1; ll cop = mod - 2, x = p[sz - 1]; while (cop) { if (cop % 2)invp[sz - 1] = invp[sz - 1] * x%mod; cop >>= 1; x = x * x % mod; } per(i, sz - 1) { invp[i] = invp[i + 1] * (i + 1) % mod; } } ll comb(ll x, ll y) { if (x < y || y < 0)return 0; ll ret = p[x]; (ret *= invp[y]) %= mod; (ret *= invp[x - y]) %= mod; return ret; } ll combP(ll x, ll y) { if (x < y || y < 0)return 0; return p[x] * invp[x - y] % mod; } }; int ri[1 << 19][27]; int nex[1 << 19][19]; int alnex[1 << 19][27]; void solve() { string s; cin >> s; int n = s.size(); per(i, n) { rep(j, 27)alnex[i][j] = -1; int t = s[i] - 'a'; alnex[i][t] = i + 1; for (int j = t + 1; j < 27; j++) { int to = alnex[i][j - 1]; if (to < 0||to==n) { alnex[i][j] = -1; } else { if (s[to] - 'a' < j) { alnex[i][j] = alnex[to][j - 1]; } else { to = ri[to][j]; if (to < 0 || to == n)alnex[i][j] = -1; else alnex[i][j] = alnex[to][j - 1]; } } } int cur = alnex[i][26]; //cout << i << " " << cur << endl; nex[i][0] = cur; int las = s[i] - 'a'; for (int j = 0; j <= las; j++) { if (cur < 0 || cur == n)ri[i][j] = cur; else { ri[i][j] = ri[cur][j]; } } for (int j = las + 1; j < 26; j++)ri[i][j] = i; } rep(j,18) { rep(i,n) { if (0 <= nex[i][j] && nex[i][j] < n) { nex[i][j + 1] = nex[nex[i][j]][j]; } else { nex[i][j + 1] = nex[i][j]; } } } int q; cin >> q; rep(aa, q) { int l, r; cin >> l >> r; l--; int cur = l; per(i, 19) { int to = nex[cur][i]; if (to >= 0 && to <= r) { //if (aa == 1)cout << cur << " " << i << " " << to << endl; cur = to; } if (cur < 0 || cur == n)break; } if (cur == r) { cout << "Yes" << endl; } else { //cout << cur << endl; cout << "No" << endl; } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(17); //init(); //int t; cin >> t; rep(i, t)solve(); solve(); stop return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 500500 #define rep(x, a, b) for(int x=a; x<=b; x++) #define drp(x, a, b) for(int x=a; x>=b; x--) int n, q, f[N][27], g[N][27]; char s[N]; int main(){ scanf("%s", s+1); n=strlen(s+1); drp(i, n, 1) { f[i][s[i]-'a']=i+1; rep(j, s[i]-'a'+1, 25) f[i][j]=f[f[i][j-1]][j-1]; f[i][26]=f[f[i][25]][25]; rep(j, 0, 25) if(!f[i][j]) f[i][j]=f[f[i][26]][j]; } rep(i, 1, n) g[i][0]=f[i][26]; rep(i, 1, 18) rep(j, 1, n) g[j][i]=g[g[j][i-1]][i-1]; scanf("%d", &q); while(q--) { int l, r; scanf("%d%d", &l, &r); int x=l; drp(i, 18, 0) if(g[x][i]<=r && g[x][i]) x=g[x][i]; if(f[x][26]==r+1) puts("Yes"); else puts("No"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<cstdio> #include<algorithm> #define MN 600001 using namespace std; int read_p,read_ca; inline int read(){ read_p=0;read_ca=getchar(); while(read_ca<'0'||read_ca>'9') read_ca=getchar(); while(read_ca>='0'&&read_ca<='9') read_p=read_p*10+read_ca-48,read_ca=getchar(); return read_p; } char s[MN]; int n,m,ne[MN][27],NE[MN][20],l,r; inline int min(int a,int b){return a==0?b:b==0?a:a<b?a:b;} int main(){ register int i,j; scanf("%s",s); for (n=0;s[n];n++); for (i=n-1;i>=0;i--){ ne[i][s[i]-='a']=i+1; for (j=s[i];j<26;j++) ne[i][j+1]=ne[ne[i][j]][j]; for (j=0;j<s[i];j++) ne[i][j]=min(ne[ne[i][j==0?26:j-1]][j==0?0:j-1],ne[ne[i][26]][j]); } for (i=n-1;i>=0;i--) if (NE[i][0]=ne[i][26]) for (j=1;NE[i][j]=NE[NE[i][j-1]][j-1];j++); for (m=read();m--;){ l=read()-1;r=read(); for (i=19;i>=0;i--) if (NE[l][i]&&NE[l][i]<=r) l=NE[l][i]; puts(l==r?"Yes":"No"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <cstdio> #include <cstring> #include <cassert> #include <iostream> #include <algorithm> #include <vector> #include <set> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR (i, 0, n) #define _ << " _ " << #define TRACE(x) cerr << #x << " = " << x << endl #define debug(...) fprintf(stderr, __VA_ARGS__) #define debug #define TRACE(x) using namespace std; typedef long long llint; const int MAXN = 500010; const int MAXC = 30; const int INF = 1e9; const int END = 26; const int MAXLG = 20; int n; char s[MAXN]; int nxt[MAXN][MAXC], emp[MAXN]; int pre[MAXN][MAXLG + 5]; void calc() { REP(c, 27) nxt[n][c] = INF; nxt[n][END] = n; for (int i = n - 1; i >= 0; --i) { REP(c, 27) nxt[i][c] = INF; nxt[i][s[i] - 'a'] = i + 1; for (int c = s[i] - 'a' + 1; c <= END; ++c) nxt[i][c] = nxt[i][c - 1] == INF ? INF : nxt[nxt[i][c - 1]][c - 1]; for (int c = 0; c < s[i] - 'a'; ++c) nxt[i][c] = nxt[i][END] == INF ? INF : nxt[nxt[i][END]][c]; } for (int i = n; i >= 0; --i) { pre[i][0] = nxt[i][END]; FOR(lg, 1, MAXLG) pre[i][lg] = (pre[i][lg - 1] == INF) ? INF : pre[pre[i][lg - 1]][lg - 1]; } } bool check(int lo, int hi) { // for (int x = nxt[lo][END]; x <= hi + 1; x = nxt[x][END]) { // if (x == hi + 1) // return true; // } // return false; for (int lg = MAXLG - 1; lg >= 0 && lo < INF; --lg) { if (pre[lo][lg] <= hi + 1) lo = pre[lo][lg]; if (lo == hi + 1) return true; } return false; } int main(void) { scanf("%s",s); n = strlen(s); calc(); int q, lo, hi; scanf("%d",&q); REP(i, q) { scanf("%d %d",&lo,&hi); --lo; --hi; printf(check(lo, hi) ? "Yes\n" : "No\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
//#pragma GCC optimize(2) //#include<bits/stdc++.h> //using namespace std; //stack <char> q; //char a[1200000]; //inline int read() { // int X=0,w=0; // char ch=0; // while(!isdigit(ch)) { // w|=ch=='-'; // ch=getchar(); // } // while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); // return w?-X:X; //} //int main() { // freopen("fusion.in","r",stdin); // freopen("fusion.out","w",stdout); // scanf("%s",a+1); // int m; // cin>>m; // for(int i=1; i<=m; i++) { // int x,y; // x=read(); // y=read(); // while(!q.empty()) q.pop(); // for(int j=x; j<=y; j++) { // if(q.empty()) { // q.push(char(a[j])); // continue; // } // char w=q.top(); // char at=a[j]; // q.push((char)a[j]); // if(w==at) { //L1: // ; // if(w=='z') { // q.pop(); // q.pop(); // } else { // q.pop(); // q.pop(); // // at=(char)w+1; // if(q.empty()) { // q.push(at); // continue; // } // w=q.top(); // q.push(at); // if(at==w) goto L1; // } // } // } // if(q.empty()) puts("Yes"); // else puts("No"); // } // return 0; //} #pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; char s[560000]; int n,m; int to[560000][30],nxt[560000][30]; inline int read() { int ret=0; char c=getchar(); while((c>'9')||(c<'0'))c=getchar(); while((c>='0')&&(c<='9'))ret=(ret<<1)+(ret<<3)+c-'0',c=getchar(); return ret; } void pre() { for(int i=1; i<=n+2; i++) { for(int j=0; j<=26; j++) to[i][j]=n+2; for(int j=0; j<=19; j++) nxt[i][j]=n+2; } for(int i=n; i; i--) { to[i][s[i]]=i+1;//更新自己这一位 for(int j=s[i]+1; j<=26; j++) to[i][j]=to[to[i][j-1]][j-1]; //从k变到j(合并操作) nxt[i][0]=to[i][26];//记录消除完的情况 for(int j=1; j<20; j++) nxt[i][j]=nxt[nxt[i][j-1]][j-1]; //更新多段消除完的情况 for(int j=0; j<26; j++) if(to[i][j]==n+2) to[i][j]=to[nxt[i][0]][j]; //如azzabcd…即对于前面没有算出东西的情况中,先消除掉一部分再进行计算。 } } bool check(int l,int r) { for(int i=19; i>=0; i--) { if(nxt[l][i]==r+1) return true; if(nxt[l][i]<=r) l=nxt[l][i]; } return false; } int main() { // freopen("fusion.in","r",stdin); // freopen("fusion.out","w",stdout); scanf("%s",s+1); n=strlen(s+1); for(int i=1; i<=n; i++) s[i]-='a'; pre(); m=read(); while(m--) { int l=read(),r=read(); if(check(l,r)) puts("Yes"); else puts("No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using db = double; #define pb push_back // 看了题解,发现当时想不出的原因主要不是倍长,而是倍长的前一步的DP没想清楚 const int mxn = 500006; const int emp = 26; char a[mxn]; int n; int f[mxn][27]; // 无效均为0 int s[mxn][20]; int main() { scanf("%s", a + 1); n = strlen(a + 1); for (int i = 1; i <= n; ++i) a[i] -= 'a'; for (int i = n; i >= 1; --i) { f[i][a[i]] = i + 1; for (int j = a[i] + 1; j <= 26; ++j) f[i][j] = f[f[i][j - 1]][j - 1]; for (int j = 0; j < a[i]; ++j) f[i][j] = f[f[i][emp]][j]; } for (int i = n; i >= 1; --i) { s[i][0] = f[i][emp]; for (int j = 1; j < 20; ++j) s[i][j] = s[s[i][j - 1]][j - 1]; } int q; cin >> q; while (q--) { int l, r; scanf("%d%d", &l, &r); ++r; for (int i = 20; i--; ) if (s[l][i] && s[l][i] <= r) l = s[l][i]; puts(l == r ? "Yes" : "No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> #define N 500005 using namespace std; int n,a[N],f[N][26],g[N][19]; char s[N]; int main(){ scanf("%s",s+1); n=strlen(s+1); int i,j; for (i=1; i<=n; i++) a[i]=s[i]-'a'; for (i=0; i<=25; i++) f[n+1][i]=f[n+2][i]=n+2; for (i=0; i<=18; i++) g[n+1][i]=g[n+2][i]=n+2; for (i=n; i; i--){ f[i][a[i]]=i+1; for (j=a[i]+1; j<26; j++) f[i][j]=f[f[i][j-1]][j-1]; g[i][0]=f[f[i][25]][25]; for (j=1; j<=18; j++) g[i][j]=g[g[i][j-1]][j-1]; for (j=0; j<a[i]; j++) f[i][j]=f[g[i][0]][j]; } int cas,l,r; scanf("%d",&cas); while (cas--){ scanf("%d%d",&l,&r); r++; for (i=18; i>=0; i--) if (g[l][i]<=r) l=g[l][i]; puts((l==r)?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head const int N=601000; char s[N]; int dp[N][30],r[N],q,x,y,n,go[N][21]; int main() { scanf("%s",s+1); n=strlen(s+1); rep(j,0,27) dp[n+1][j]=-1; dp[n+1][26]=n+1; per(i,1,n+1) { int w=s[i]-'a'; dp[i][w]=i+1; rep(j,w+1,27) if (dp[i][j-1]!=-1) { dp[i][j]=dp[dp[i][j-1]][j-1]; } else dp[i][j]=-1; rep(j,0,w) if (dp[i][26]!=-1) dp[i][j]=dp[dp[i][26]][j]; else dp[i][j]=-1; } rep(i,1,n+2) if (dp[i][26]!=-1) go[i][0]=dp[i][26]; else go[i][0]=i; rep(j,1,21) rep(i,1,n+2) go[i][j]=go[go[i][j-1]][j-1]; scanf("%d",&q); rep(i,0,q) { scanf("%d%d",&x,&y); per(j,0,21) if (go[x][j]<=y+1) x=go[x][j]; puts(x==y+1?"Yes":"No"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; int main() { static char s[500010]; scanf("%s",s); int n=strlen(s); static int T[500010][30],S[500010][30]; for(int i=0;i<=n;i++){ for(int c=0;c<=26;c++){ T[i][c]=-1; } } for(int c=0;c<26;c++){ S[n][c]=-1; } for(int i=n-1;i>=0;i--){ int p=s[i]-'a'; T[i][p]=i+1; for(int c=p;c+1<=26&&S[T[i][c]][c]!=-1&&T[S[T[i][c]][c]][c]!=-1;c++){ T[i][c+1]=T[S[T[i][c]][c]][c]; } for(int c=0;c<p;c++){ S[i][c]=(T[i][26]==-1?-1:S[T[i][26]][c]); } for(int c=p;c<26;c++){ S[i][c]=i; } } static int db[500010][19]; for(int i=0;i<=n;i++){ for(int k=0;k<19;k++){ db[i][k]=-1; } } for(int i=n;i>=0;i--){ db[i][0]=T[i][26]; int k=0; while(db[i][k]!=-1){ //printf("%d %d %d\n",i,k,db[i][k]); db[i][k+1]=db[db[i][k]][k]; k++; } } int q; scanf("%d",&q); while(q--){ int l,r; scanf("%d%d",&l,&r); l--; while(l<r){ int k=18; while(k>=0&&!(db[l][k]!=-1&&db[l][k]<=r)){ k--; } //printf("%d %d\n",l,k); if(k==-1){ break; } l=db[l][k]; } puts(l==r?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #include <math.h> #include <assert.h> #include <queue> #include <map> #include <set> #include <string> #include <algorithm> #include <iostream> #include <functional> #include <unordered_map> #include <unordered_set> #include <list> #include <bitset> using namespace std; typedef pair<int, int> Pi; typedef long long ll; #define pii Pi #define pll PL #define Fi first #define Se second #define pb(x) push_back(x) #define sz(x) ((int)(x).size()) #define rep(i, n) for(int i=0;i<n;i++) #define all(x) (x).begin(), (x).end() typedef tuple<int, int, int> t3; typedef pair<ll, ll> PL; int A[500050]; char temp[500050]; t3 Q[500050]; int D[500050][27]; int ans[500050]; int up[500050][20]; void solve(){ scanf("%s", temp+1); int n = (int)strlen(temp + 1); for(int i=1;i<=n;i++)A[i] = temp[i] - 'a'; int q; scanf("%d", &q); rep(i, 27)D[n+1][i] = -1; for(int i=n;i;i--){ int c = A[i]; int pp = i; for(int j=c;j<26;j++){ if(D[pp+1][j] != -1)pp = D[pp+1][j]; else{ pp = -1; break; } } if(pp == -1)rep(j, c)D[i][j] = -1; else rep(j, c)D[i][j] = D[pp+1][j]; D[i][c] = i; for(int j=c+1;j<=26;j++)D[i][j] = -1; for(int j=c, x=i+1;j<26;j++){ int y = D[x][j]; if(y == -1)break; D[i][j+1] = y; x = y + 1; } if(D[i][26] == -1)up[i][0] = -1; else up[i][0] = D[i][26] + 1; } rep(j, 20)up[n+1][j] = -1; for(int j=1;j<20;j++){ for(int i=1;i<=n;i++){ if(up[i][j-1] == -1)up[i][j] = -1; else up[i][j] = up[ up[i][j-1] ][j-1]; } } rep(i, q){ int l, r; scanf("%d%d", &l, &r); int ans = 0; for(int j=19;j>=0;j--){ if(up[l][j] == -1)continue; if(up[l][j] == r+1){ans = 1; break;} if(up[l][j] < r+1)l = up[l][j]; } printf("%s\n", ans ? "Yes" : "No"); } } int main(){ int Tc = 1; //scanf("%d", &Tc); for(int tc=1;tc<=Tc;tc++){ //printf("Case #%d: ", tc); solve(); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #define N 500005 #define LL long long #define oo (1<<30) using namespace std; int n,q,ans,f[27][N],F[20][N]; char st[N]; int main() { int i,j,l,r; scanf("%s",st+1); n = strlen(st+1); for(i=1;i<=n;i++) f[st[i]-'a'][i]=i; for(i=n-1;i>=1;i--){ for(j=st[i]-'a'+1;j<=26;j++) f[j][i]=f[j-1][f[j-1][i] ? f[j-1][i]+1 : 0]; for(j=0;j<st[i]-'a';j++) f[j][i]=f[j][f[26][i] ? f[26][i]+1 : 0]; } for(i=1;i<=n;i++) F[0][i]=f[26][i] ? f[26][i]+1 : 0; for(i=1;i<20;i++) for(j=1;j<=n;j++) F[i][j]=F[i-1][F[i-1][j]]; scanf("%d",&q); while(q--){ scanf("%d %d",&l,&r),ans=0; for(i=19;i>=0;i--) if(F[i][l]<=r+1&&F[i][l]) l=F[i][l]; if(l>r) printf("Yes\n"); else printf("No\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <vector> #include <iostream> #include <string> #include <map> #include <algorithm> #include <set> #include <queue> #include <cstdio> #include <utility> #include <bitset> #include <complex> #include <stack> #include <tuple> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef tuple<ll,ll,ll> T; const ll INF = 1e7; const ll MOD = (1e9)+7; int main(){ string s; cin >> s; vector<vector<ll>> nextc('z'-'a'+2, vector<ll>(s.size(), INF)); //nextc['x'-'a'][l] = next_x[l] const ll mu = 'z' - 'a' + 1; for(ll i=s.size()-1; i>=0; i--){ nextc[s[i]-'a'][i] = i+1; for(char c=s[i]+1-'a';c<='z'-'a'+1;c++){ if(nextc[c-1][i]<s.size()) nextc[c][i] = nextc[c-1][nextc[c-1][i]]; } for(char c=0; c<s[i]-'a';c++){ if(nextc[mu][i]<s.size()){ nextc[c][i] = nextc[c][nextc[mu][i]]; } } } vector<vector<ll>> nextl(s.size()); for(ll i=s.size()-1;i>=0;i--){ ll to = nextc[mu][i]; ll count=0; while(to!=INF){ nextl[i].push_back(to); if(to>=nextl.size() || count>=nextl[to].size()) break; to = nextl[to][count]; count++; } } ll Q; cin >> Q; for(ll q=0;q<Q;q++){ ll l,r; cin >> l >> r; l--; ll to=0; while(to+1<nextl[l].size() && nextl[l][to+1]<=r) to++; if(to<nextl[l].size()) l=nextl[l][to]; while(to>0){ to--; if(l<nextl.size() && to<nextl[l].size() && nextl[l][to]<=r) l=nextl[l][to]; } if(l==r) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; #define mp(x, y) make_pair((x), (y)) typedef long long ll; int n; char s[500005]; int dp[500005][27]; int nnext[500005][21]; int main() { scanf("%s\n", s); n=strlen(s); for(int i=0; i<n; i++) s[i]-='a'; for(int i=0; i<n; i++) for(int j=0; j<27; j++) dp[i][j]=-1; for(int i=0; i<n; i++) for(int j=0; j<21; j++) nnext[i][j]=-1; for(int i=n-1; i>=0; i--) { dp[i][s[i]]=i; int cur=i+1; for(int j=s[i]+1; j<27 && cur<n; j++) { if(dp[cur][j-1]==-1) break; dp[i][j]=dp[cur][j-1]; cur=dp[cur][j-1]+1; } if(dp[i][26]==-1 || dp[i][26]+1>=n) continue; for(int j=0; j<s[i]; j++) { dp[i][j]=dp[dp[i][26]+1][j]; } } for(int i=0; i<n; i++) { nnext[i][0]=dp[i][26]; } for(int j=1; j<21; j++) { for(int i=0; i<n; i++) { if(nnext[i][j-1]==-1 || nnext[i][j-1]+1>=n) continue; nnext[i][j]=nnext[nnext[i][j-1]+1][j-1]; } } int Q; scanf("%d\n", &Q); for(int x=0; x<Q; x++) { int l, r; scanf("%d %d\n", &l, &r); l-=2; r--; for(int k=20; k>=0 && l<r; k--) { if(nnext[l+1][k]!=-1 && nnext[l+1][k]<=r) { l=nnext[l+1][k]; } } if(l!=r) { printf("No\n"); } else { printf("Yes\n"); } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) #define eprintf(...) fprintf(stderr, __VA_ARGS__) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } const int MAX = 500111; int N, Q; char S[MAX]; int pos[MAX][27]; int cut[21][MAX]; int main() { scanf("%s%d", S, &Q); N = strlen(S); memset(pos, -1, sizeof pos); for (int i=N; i--;) { int d = S[i]-'a'; pos[i][d] = i+1; for (int k=d; k<26; k++) { if (pos[i][k] == -1) break; pos[i][k+1] = pos[pos[i][k]][k]; } if (pos[i][26] != -1) { for (int k=0; k<d; k++) pos[i][k] = pos[pos[i][26]][k]; } } REP (i, N+10) { if (pos[i][26] == -1) cut[0][i] = N + 1; else cut[0][i] = pos[i][26]; } REP (t, 20) REP (i, N+10) cut[t+1][i] = cut[t][cut[t][i]]; REP ($, Q) { int L, R; scanf("%d%d", &L, &R); L--; for (int t=21; t--;) { if (cut[t][L] <= R) L = cut[t][L]; } puts(L == R ? "Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; #define TRACE(x) x #define WATCH(x) TRACE(cout << #x" = " << x << endl) #define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl) #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());}) #define all(x) (x).begin(), (x).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); string str; cin >> str; int N = str.size(); vector<array<int, 27>> nxt(N + 2); fill(all(nxt[N+1]), N + 1); fill(all(nxt[N]), N + 1); for (int i = N - 1; i >= 0; i--) { int v = str[i] - 'a' + 1; nxt[i][v] = i + 1; for (int j = v + 1; j <= 26; j++) nxt[i][j] = nxt[nxt[i][j-1]][j-1]; nxt[i][0] = nxt[nxt[i][26]][26]; for (int j = 1; j < v; j++) nxt[i][j] = nxt[nxt[i][0]][j]; } vector<array<int, 20>> zero(N + 2); fill(all(zero[N+1]), N + 1); fill(all(zero[N]), N + 1); for (int i = N - 1; i >= 0; i--) { zero[i][0] = nxt[i][0]; for (int l = 1; l < 20; l++) zero[i][l] = zero[zero[i][l-1]][l-1]; } int Q, l, r; cin >> Q; for (int q = 0; q < Q; q++) { cin >> l >> r; l--; for (int i = 19; i >= 0; i--) if (zero[l][i] <= r) l = zero[l][i]; cout << (l == r ? "Yes" : "No") << "\n"; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5, Sigma = 27, bzmax = 20; int n, q; char s[N]; int nxt[N][Sigma], bz[N][bzmax]; int main() { scanf("%s", s + 1); n = strlen(s + 1); scanf("%d", &q); fill(nxt[n + 1], nxt[n + 1] + Sigma, N); fill(bz[n + 1], bz[n + 1] + bzmax, N); for (int i = n; i; i--) { int c = s[i] - 'a'; nxt[i][c] = i + 1; for (int j = c + 1; j < Sigma; j++) { nxt[i][j] = nxt[i][j - 1] == N ? N : nxt[nxt[i][j - 1]][j - 1]; } int p = nxt[i][26]; for (int j = c; j--; ) { nxt[i][j] = p == N ? N : nxt[p][j]; } bz[i][0] = p; for (int j = 1; j < bzmax; j++) { bz[i][j] = bz[i][j - 1] == N ? N : bz[bz[i][j - 1]][j - 1]; } } int l, r; while (q--) { scanf("%d%d", &l, &r); ++r; for (int i = bzmax; i--; ) { if (bz[l][i] <= r) { l = bz[l][i]; } } puts(l == r ? "Yes" : "No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> #define DEBUG(x) cout << #x << " = " << x << endl #define pb push_back using namespace std; typedef long long ll; const int MAXN = 500005; int d[MAXN][27]; int b[MAXN][30]; int main() { //freopen("input.txt", "r", stdin); string s; cin >> s; int sz = s.size(); for (int i = 0; i < 27; i++) d[sz][i] = sz+1; for (int i = 0; i < 27; i++) d[sz+1][i] = sz+1; for (int l = sz-1; l >= 0; l--) { int c = s[l]-'a'; d[l][c] = l+1; for (int i = c+1; i < 27; i++) { d[l][i] = d[ d[l][i-1] ][ i-1 ]; } for (int i = 0; i < c; i++) { d[l][i] = d[ d[l][26] ][i]; } } for (int i = 0; i <= sz+1; i++) { b[i][0] = d[i][26]; } for (int i = 1; i < 20; i++) { for (int j = 0; j <= sz+1; j++) { b[j][i] = b[ b[j][i-1] ][i-1]; } } int q; cin >> q; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; int k = 19; while(1) { while(k >= 0 && b[l][k] > r) k--; if (k < 0) { cout << "No" << endl; break; } if (b[l][k] == r) { cout << "Yes" << endl; break; } l = b[l][k]; } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define li long long #define itn int using namespace std; inline int nxt(){ int n; scanf("%d", &n); return n; } template <typename T> ostream& operator <<(ostream& ostr, const vector<T>& a) { ostr << "("; for (size_t i = 0; i < a.size(); ++i) { if (i) { ostr << ", "; } ostr << a[i]; } ostr << ")"; return ostr; } const int N = 555555; const int K = 26; const int M = 20; int nx[K][N]; int nxw[K][N]; int nxz[M][N]; int main(){ string s; cin >> s; int n = s.length(); for (int i = 0; i < K; ++i) { char c = 'a' + i; if (i == 0) { for (int j = 0; j < n; ++j) { nx[i][j] = (s[j] == c ? j + 1 : -1); } continue; } for (itn j = 0; j < n; ++j) { if (s[j] > c) { nx[i][j] = -1; } else if (s[j] == c) { nx[i][j] = j + 1; } else if (nx[i - 1][j] == -1 || nx[i - 1][j] == n) { nx[i][j] = -1; } else { nx[i][j] = nx[i - 1][nx[i - 1][j]]; } } } for (int i = 0; i < K; ++i) { nxw[i][n - 1] = nx[i][n - 1]; } nxz[0][n - 1] = -1; for (int i = n - 2; i >= 0; --i) { int c = s[i] - 'a'; int lst = i + 1; while (c < K && nx[c][i] > -1) { nxw[c][i] = nx[c][i]; lst = nx[c][i]; ++c; } while (c < K) { if (lst == n || lst == -1) { nxw[c][i] = -1; } else { nxw[c][i] = nxw[c - 1][lst]; lst = nxw[c - 1][lst]; } ++c; } if (nxw[25][i] == -1 || nxw[25][i] == n) { nxz[0][i] = -1; } else { nxz[0][i] = nxw[25][nxw[25][i]]; } c = 0; lst = nxz[0][i]; while (c < s[i] - 'a') { if (lst == -1 || lst == n) { nxw[c][i] = -1; } else { nxw[c][i] = nxw[c][lst]; } ++c; } } for (int i = 1; i < M; ++i) { for (int j = 0; j < n; ++j) { if (nxz[i - 1][j] == -1 || nxz[i - 1][j] == n) { nxz[i][j] = -1; } else { nxz[i][j] = nxz[i - 1][nxz[i - 1][j]]; } } } // for (int i = 0; i < n; ++i) { // cerr << nx[24][i] << " "; // } // cerr << "\n"; // for (int i = 0; i < n; ++i) { // cerr << nxz[0][i] << " "; // } // cerr << "\n"; // for (int i = 0; i < n; ++i) { // cerr << nxz[1][i] << " "; // } // cerr << "\n"; // for (int i = 0; i < n; ++i) { // cerr << nxz[2][i] << " "; // } // cerr << "\n"; int q = nxt(); while (q--) { int l = nxt() - 1, r = nxt(); // cerr << l << " " << r << "\n"; for (int i = M - 1; i >= 0; --i) { if (nxz[i][l] > -1 && nxz[i][l] <= r) { l = nxz[i][l]; } if (l == r) { break; } } // cerr << l << " .. " << r << "\n"; if (l == r) { puts("Yes"); } else { puts("No"); } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define mp make_pair #define SZ(x) ((int)x.size()) #define ALL(x) x.begin(),x.end() #define L(i,u) for (register int i=head[u]; i; i=nxt[i]) #define rep(i,a,b) for (register int i=(a); i<=(b); i++) #define per(i,a,b) for (register int i=(a); i>=(b); i--) using namespace std; typedef long double ld; typedef long long ll; typedef unsigned int ui; typedef pair<int,int> Pii; typedef vector<int> Vi; template<class T> inline void read(T &x){ x=0; char c=getchar(); int f=1; while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f; } template<class T> T gcd(T a, T b){return !b?a:gcd(b,a%b);} template<class T> inline void umin(T &x, T y){x=x<y?x:y;} template<class T> inline void umax(T &x, T y){x=x>y?x:y;} inline ui R() { static ui seed=416; return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13; } const int N = 505000; char s[N];int n,f[27][N],jmp[20][N]; int main() { scanf("%s",s+1);n=strlen(s+1); per(j,n,1){ f[s[j]-'a'][j]=j+1; rep(i,s[j]-'a'+1,26)f[i][j]=f[i-1][f[i-1][j]]; rep(i,0,s[j]-'a'-1)f[i][j]=f[i][f[26][j]]; } rep(i,1,n+1)jmp[0][i]=f[26][i]; rep(j,1,19)rep(i,1,n+1)jmp[j][i]=jmp[j-1][jmp[j-1][i]]; int q;read(q);while(q--){ int l,r;read(l);read(r); per(i,19,0)if(jmp[i][l]&&jmp[i][l]-1<=r)l=jmp[i][l]; printf("%s\n",l==r+1?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <cmath> using namespace std; #define mp make_pair #define pb push_back #define ll long long #define maxN 500011 #define sigma 26 #define maxLog 21 int n, i, q, l, r; char s[maxN]; int nxt[sigma][maxN]; int dad[maxLog][maxN]; void pre() { int i, j; for (i = 0; i < sigma; i++) nxt[i][n + 1] = n + 2; for (i = 0; i < sigma; i++) nxt[i][n + 2] = n + 2; for (i = n; i > 0; i--) { nxt[s[i] - 'a'][i] = i + 1; for (j = s[i] - 'a' + 1; j < sigma; j++) nxt[j][i] = nxt[j - 1][ nxt[j - 1][i] ]; dad[0][i] = nxt[25][ nxt[25][i] ]; for (j = 0; j < s[i] - 'a'; j++) nxt[j][i] = nxt[j][ dad[0][i] ]; } for (i = 0; i < maxLog; i++) dad[i][n + 1] = n + 2; for (i = 0; i < maxLog; i++) dad[i][n + 2] = n + 2; for (i = 1; i < maxLog; i++) for (j = 1; j <= n; j++) dad[i][j] = dad[i - 1][ dad[i - 1][j] ]; } int main() { // freopen("test.in","r",stdin); scanf("%s\n%d", s + 1, &q); n = strlen(s + 1); pre(); for (i = 1; i <= q; i++) { scanf("%d%d", &l, &r); for (int step = maxLog - 1; step >= 0; step--) if (dad[step][l] <= r + 1) l = dad[step][l]; if (l == r + 1) printf("Yes\n"); else printf("No\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <string> #include <utility> #include <vector> #include <set> using namespace std; typedef pair<int, int> P; string s; int Q; int l[100005], r[100005]; int rpos[500005][27]; int succ[500005][20]; bool calc(int l, int r) { int p = l; for(int i = 19; i >= 0; i--){ if(succ[p][i] <= r) p = succ[p][i]; } return p == r; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> s; int n = s.size(); s = "#" + s; cin >> Q; for(int i = 1; i <= n; i++) cin >> l[i] >> r[i]; for(int j = 0; j < 27; j++){ rpos[n+1][j] = n+1; rpos[n+2][j] = n+1; } for(int i = n; i >= 1; i--){ int c = s[i] - 'a'; rpos[i][c] = i; for(int j = c+1; j < 27; j++){ rpos[i][j] = rpos[rpos[i][j-1]+1][j-1]; } for(int j = 0; j < c; j++){ rpos[i][j] = rpos[rpos[i][26]+1][j]; } } for(int i = 1; i <= n+2; i++) succ[i][0] = rpos[i][26]+1; for(int i = 1; i < 20; i++){ for(int j = 1; j <= n+2; j++){ succ[j][i] = succ[succ[j][i-1]][i-1]; } } for(int i = 1; i <= Q; i++){ if(calc(l[i], r[i]+1)) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; string S; int Q; vector<vector<int> > cc; int dp(int l, int a) { if(l == S.size()) return 1e9; int &ret = cc[l][a]; if(ret != -1) return ret; if(S[l] - 'a' == a) return ret = l + 1; if(S[l] - 'a' < a) { if(dp(l, a - 1) == 1e9) return ret = 1e9; else return ret = dp(dp(l, a - 1), a - 1); } else { if(dp(l, 26) == 1e9) return ret = 1e9; else return ret = dp(dp(l, 26), a); } } vector<vector<int> > db; int main() { std::ios::sync_with_stdio(false); cin>>S>>Q; cc = vector<vector<int> >(S.size(), vector<int>(27, -1)); db = vector<vector<int> >(S.size(), vector<int>(20, 1e9)); for(int i = S.size() - 1; i >= 0; i--) { db[i][0] = dp(i, 26); for(int j = 1; j < 20; j++) { int t = db[i][j - 1]; if(t == S.size() || t == 1e9) break; db[i][j] = db[t][j - 1]; } } while(Q--) { int l, r; cin>>l>>r; l--; int p = l; for(int i = 20; i--;) { if(p != S.size() && db[p][i] <= r) { p = db[p][i]; } } if(p == r) printf("Yes\n"); else printf("No\n"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <deque> #include <algorithm> #include <queue> #include <cmath> #include <map> #include <complex> #include <cstring> #include <cassert> #include <bitset> using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); i++) #define rep_down(i, a, b) for(int i = (a) - 1; i >= (b); i--) #define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++) #define ft(a) __typeof((a).begin()) #define ll long long #define ld long double #define fi first #define se second #define mk make_pair #define pb push_back #define sz(a) (int)(a).size() #define all(a) (a).begin(), (a).end() #define bitcount(n) __builtin_popcountll(n) typedef complex<ld> cplex; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; const int N = 500000 + 7; const int M = 27; const int inf = 1e9 + 7; const long long linf = 1ll * inf * N * 1000; const double pi = acos(-1); const double eps = 1e-7; const bool multipleTest = 0; string s; int nxt[N][M]; // 26 = empty int n; int f[N][20]; void solve() { cin >> s; n = sz(s); rep(j, 0, 27) nxt[n][j] = n + 1; rep_down(i, n, 0) { int c = s[i] - 'a'; rep(j, 0, M) nxt[i][j] = n + 1; nxt[i][c] = i + 1; int cur = i + 1; rep(t, c, 26) { if (cur >= n) break; cur = nxt[cur][t]; nxt[i][t + 1] = cur; } if (nxt[i][M - 1] < n) { int rr = nxt[i][M - 1]; rep(t, 0, c) nxt[i][t] = nxt[rr][t]; } } rep_down(i, n, 0) { f[i][0] = nxt[i][M - 1]; rep(j, 1, 20) { if (f[i][j - 1] >= n) f[i][j] = inf; else f[i][j] = f[f[i][j - 1]][j - 1]; } } int query; cin >> query; while (query-- > 0) { int u, v; scanf("%d%d", &u, &v); // ++v; --u; rep_down(j, 20, 0) { // cout << u << ' ' << j << '\n'; if (f[u][j] <= v) { u = f[u][j]; } if (u == v) break; } if (u == v) puts("Yes"); else puts("No"); } } int main() { #ifdef _LOCAL_ freopen("in.txt", "r", stdin); //t1=clock(); #endif int Test = 1; if (multipleTest) { cin >> Test; } for(int i = 0; i < Test; ++i) { solve(); } #ifdef _LOCAL_ cout<<"\n" << clock() / CLOCKS_PER_SEC<<endl; #endif }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base :: sync_with_stdio(false); //ifstream cin("testC.in"); string a; cin >> a; int n = a.size(); for(int i = 0; i < n; ++i) a[i] -= 'a'; vector<vector<int>> nxt(n, vector<int> (27, n)); for(int i = n - 1; i >= 0; --i) { nxt[i][a[i]] = i; for(int c = a[i] + 1; c < 27; ++c) { int self = nxt[i][c - 1] + 1; if(self >= n) continue; nxt[i][c] = nxt[self][c - 1]; } for(int c = 0; c < 26; ++c) { if(nxt[i][26] + 1 < n) nxt[i][c] = min(nxt[i][c], nxt[nxt[i][26] + 1][c]); } } vector<vector<int>> anc(21, vector<int> (n, 0)); for(int i = 0; i < n; ++i) anc[0][i] = nxt[i][26]; for(int lg = 1; lg < 21; ++lg) { for(int i = 0; i < n; ++i) { if(anc[lg - 1][i] >= n - 1) { anc[lg][i] = n; } else { anc[lg][i] = anc[lg - 1][anc[lg - 1][i] + 1]; } } } int q; cin >> q; for(int i = 0; i < q; ++i) { int x, y; cin >> x >> y; x--, y--; bool good = false; int now = x; for(int lg = 20; lg >= 0; --lg) { if(anc[lg][now] <= y) { now = anc[lg][now] + 1; if(now == y + 1) good = true; } } if(not good) { cout << "No\n"; } else { cout << "Yes\n"; } } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
//bcw0x1bd2 {{{ #include<bits/stdc++.h> #include<unistd.h> using namespace std; #define F first #define S second #define MP make_pair #define PB push_back #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #define SZ(x) ((int)((x).size())) #define ALL(x) begin(x),end(x) #define REP(i,x) for (int i=0; i<(x); i++) #define REP1(i,a,b) for (int i=(a); i<=(b); i++) typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef long double ld; #ifdef DARKHH #define FILEIO(name) #else #define FILEIO(name) \ freopen(name".in", "r", stdin); \ freopen(name".out", "w", stdout); #endif #ifdef DARKHH template<typename T> void _dump( const char* s, T&& head ) { cerr<<s<<"="<<head<<endl; } template<typename T, typename... Args> void _dump( const char* s, T&& head, Args&&... tail ) { int c=0; while ( *s!=',' || c!=0 ) { if ( *s=='(' || *s=='[' || *s=='{' ) c++; if ( *s==')' || *s==']' || *s=='}' ) c--; cerr<<*s++; } cerr<<"="<<head<<", "; _dump(s+1,tail...); } #define dump(...) do { \ fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \ _dump(#__VA_ARGS__, __VA_ARGS__); \ } while (0) template<typename Iter> ostream& _out( ostream &s, Iter b, Iter e ) { s<<"["; for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it; s<<"]"; return s; } template<typename A, typename B> ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.first<<","<<p.second<<")"; } template<typename T> ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); } template<typename T, size_t N> ostream& operator <<( ostream &s, const array<T,N> &c ) { return _out(s,ALL(c)); } template<typename T> ostream& operator <<( ostream &s, const set<T> &c ) { return _out(s,ALL(c)); } template<typename A, typename B> ostream& operator <<( ostream &s, const map<A,B> &c ) { return _out(s,ALL(c)); } #else #define dump(...) #endif // }}} // Let's Fight! ~OAO~~ const int LOG = 22; const int MXN = 500005; const int INf = 1029384756; int N,ip[MXN]; int dp[MXN][27]; int ed[MXN]; int nxt[LOG][MXN]; string S; int go(int i, int c) { if (i >= N) return N; int &res = dp[i][c]; assert(res != -1); return res; } int main() { IOS; cin>>S; N = SZ(S); REP(i,N) ip[i] = S[i]-'a'; memset(dp,-1,sizeof(dp)); for (int i=N-1; i>=0; i--) { REP(c,26) { int &res = dp[i][c]; if (c >= ip[i]) { int j = i; REP1(x,ip[i]+1,c) { j++; j = go(j,x-1); } res = j; } else { int j = i+1; REP1(x,ip[i],25) { j = go(j,x); j++; } res = go(j,c); } } } REP(i,N) { int j = go(i,25); int k = go(j+1,25); ed[i] = k; //dump(i,ed[i]); } REP(i,N) nxt[0][i] = ed[i]; ed[N] = N; REP(i,LOG) nxt[i][N] = N; REP1(i,1,LOG-1) { REP(j,N) { nxt[i][j] = nxt[i-1][min(N,nxt[i-1][j]+1)]; } } int Q; cin>>Q; REP(q,Q) { int l,r; cin>>l>>r; l--; r--; for (int i=LOG-1; i>=0; i--) { if (nxt[i][l]+1 <= r) l = nxt[i][l]+1; } if (nxt[0][l] == r) cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; typedef long long lint; typedef pair<int, int> pi; const int MAXN = 500005; int n, dp[27][MAXN]; int nxt[19][MAXN]; char str[MAXN]; int main(){ scanf("%s", str + 1); n = strlen(str + 1); for(int i=0; i<27; i++) dp[i][n+1] = -1; for(int j=n; j; j--){ for(int i=0; i<27; i++){ if(str[j] == i + 'a') dp[i][j] = j+1; else if(i && dp[i-1][j] != -1) dp[i][j] = dp[i-1][dp[i-1][j]]; else dp[i][j] = -1; } if(dp[26][j] != -1){ for(int i=0; i<26; i++){ if(dp[i][j] == -1) dp[i][j] = dp[i][dp[26][j]]; } } } for(int i=1; i<=n+1; i++){ if(dp[26][i] == -1) nxt[0][i] = i; else nxt[0][i] = dp[26][i]; } for(int i=1; i<19; i++){ for(int j=1; j<=n+1; j++){ nxt[i][j] = nxt[i-1][nxt[i-1][j]]; } } int q; scanf("%d",&q); while(q--){ int l, r; scanf("%d %d",&l, &r); for(int i=18; i>=0; i--){ if(nxt[i][l] <= r) l = nxt[i][l]; } l = nxt[0][l]; puts(l == r+1 ? "Yes" : "No"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
//satyaki3794 #include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define MOD (1000000007LL) #define LEFT(n) (2*(n)) #define RIGHT(n) (2*(n)+1) using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef pair<int, ii> iii; ll pwr(ll base, ll p, ll mod = MOD){ ll ans = 1;while(p){if(p&1)ans=(ans*base)%mod;base=(base*base)%mod;p/=2;}return ans; } const int N = 500002; int n, next_[N][28], jump[20][N]; char str[N]; bool check(int src, int dest){ for(int i=19;i>=0;i--) if(jump[i][src] != -1 && jump[i][src] <= dest) src = jump[i][src]; return (src == dest); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>(str+1); n = strlen(str+1); memset(next_, -1, sizeof(next_)); for(int i=n;i>=1;i--){ int ch = str[i]-'a'+1; next_[i][ch] = i+1; for(int j=ch+1;j<=27;j++) if(next_[i][j-1] != -1) next_[i][j] = next_[next_[i][j-1]][j-1]; for(int j=1;j<ch;j++) if(next_[i][27] != -1) next_[i][j] = next_[next_[i][27]][j]; } // cout<<"next_:\n"; // for(int i=1;i<=n;i++){ // cout<<i<<": ";for(int j=1;j<=27;j++) cout<<next_[i][j]<<" ";cout<<endl; // }cout<<endl; memset(jump, -1, sizeof(jump)); for(int i=n;i>=1;i--){ jump[0][i] = next_[i][27]; for(int j=0;j<19;j++) if(jump[j][i] != -1) jump[j+1][i] = jump[j][jump[j][i]]; } // cout<<"jump:\n"; // for(int i=1;i<=n;i++){ // cout<<i<<": ";for(int j=0;j<5;j++) cout<<jump[j][i]<<" ";cout<<endl; // }cout<<endl; int q; cin>>q; while(q--){ int l, r; cin>>l>>r; if(check(l, r+1)) cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> using namespace std; const int INF = 1 << 29; int dp[500001][27]; int nxt[21][500001]; int main() { string s; cin >> s; for(auto &c : s) c -= 'a'; fill_n(*dp, 500001 * 27, INF); for(int i = (int) s.size() - 1; i >= 0; i--) { dp[i][s[i]] = i + 1; for(int j = s[i] + 1; j <= 26; j++) { if(dp[i][j - 1] == INF) break; dp[i][j] = dp[dp[i][j - 1]][j - 1]; } if(dp[i][26] != INF) { for(int j = 0; j < s[i]; j++) dp[i][j] = dp[dp[i][26]][j]; } } fill_n(*nxt, 21 * 500001, INF); for(int i = 0; i < s.size(); i++) { nxt[0][i] = dp[i][26]; } for(int i = 1; i < 21; i++) { for(int j = 0; j < s.size(); j++) { if(nxt[i - 1][j] == INF) continue; nxt[i][j] = nxt[i - 1][nxt[i - 1][j]]; } } int Q; cin >> Q; while(Q--) { int L, R; cin >> L >> R; --L; int pos = L; for(int i = 20; i >= 0; i--) { if(nxt[i][pos] <= R) pos = nxt[i][pos]; } if(pos == R) cout << "Yes\n"; else cout << "No\n"; } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <iomanip> #include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> #include <algorithm> #include <numeric> #include <random> #include <vector> #include <array> #include <bitset> #include <queue> #include <set> #include <unordered_set> #include <map> #include <unordered_map> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template<class T> using V = vector<T>; template<class T> using VV = V<V<T>>; constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); } int bsr(uint x) { return 31 - __builtin_clz(x); } int bsr(ull x) { return 63 - __builtin_clzll(x); } int bsf(uint x) { return __builtin_ctz(x); } int bsf(ull x) { return __builtin_ctzll(x); } using P = pair<int, int>; int n; V<int> s; V<int> la; //[p, ), c int memo[500500][26]; int nxt(int p, int c) { if (memo[p][c] != -1) return memo[p][c]; if (p >= n) return memo[p][c] = n+1; if (s[p] == c) return memo[p][c] = p+1; if (s[p] > c) return memo[p][c] = nxt(la[p], c); return memo[p][c] = nxt(nxt(p, c-1), c-1); } int main() { memset(memo, -1, sizeof(memo)); cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20); string ss; cin >> ss; n = int(ss.size()); s = V<int>(n); for (int i = 0; i < n; i++) s[i] = ss[i]-'a'; la = V<int>(n+1, n+1); for (int i = n-1; i >= 0; i--) { int l = i+1; int x = s[i]; for (int j = x; j < 26; j++) { l = nxt(l, j); } la[i] = l; } /* for (int i = 0; i < n; i++) { cout << la[i] << ", "; } cout << endl;*/ VV<int> dbl(20); dbl[0] = la; for (int fe = 1; fe < 20; fe++) { dbl[fe] = V<int>(n+1); for (int i = 0; i <= n; i++) { if (dbl[fe-1][i] == n+1) { dbl[fe][i] = n+1; } else { dbl[fe][i] = dbl[fe-1][dbl[fe-1][i]]; } } } int q; cin >> q; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; bool f = false; for (int fe = 19; fe >= 0; fe--) { if (l == n+1) break; if (dbl[fe][l] == r) { f = true; break; } if (dbl[fe][l] < r) l = dbl[fe][l]; } if (f) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<vector> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<ctime> #include<queue> #include<set> #define md double #define LL long long using namespace std; const int N=1e6; int gi() { int w=0;bool q=1;char c=getchar(); while ((c<'0'||c>'9') && c!='-') c=getchar(); if (c=='-') q=0,c=getchar(); while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar(); return q? w:-w; } int str[N]; int nxt[N][27],ST[N][21]; int main() { int n=0,i,m,t,l,r;char c; while ((c=getchar())<'a'||'z'<c); while ('a'<=c&&c<='z') str[++n]=c-'a',c=getchar(); for (i=n;i;i--) { nxt[i][str[i]]=i+1; for (t=str[i];t<26;t++) nxt[i][t+1]=nxt[nxt[i][t]][t]; for (t=0;t<str[i];t++) nxt[i][t]=nxt[nxt[i][26]][t]; ST[i][0]=nxt[i][26]; } for (t=0;t<20;t++) for (i=n-(1<<(t+1))+1;i>0;i--) ST[i][t+1]=ST[ST[i][t]][t]; m=gi(); while (m--) { l=gi(),r=gi()+1; for (t=0;ST[l][t]&&ST[l][t]<=r;t++); while (--t>=0) if (ST[l][t]&&ST[l][t]<=r) l=ST[l][t]; puts(l==r?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
/// c.cpp # include <stdio.h> # include <bits/stdc++.h> using namespace std; const pair < int , int > DD[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}}; # define fi cin # define fo cout # define x first # define y second # define ll long long # define IOS ios_base :: sync_with_stdio(0);cin.tie(0) # define p(v) cerr << #v << " = " << v << '\n' # define p2(v) cerr << #v << " = " << (complex < __typeof(v.x) > (v.x,v.y)) << '\n' # define vi vector < int > # define vl vector < ll > # define pll pair < ll , ll > # define pii pair < int , int > # define mp make_pair # define db long double # define fail puts("-1") # define yes puts("YES") # define no puts("NO") # define PP puts("Possible") # define II puts("Impossible") # define vii vector < pii > # define vll vector < ll > # define pb push_back # define pdd pair < db , db > template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} int main(void) { #ifdef CF freopen("input","r",stdin); #endif // CF srand(time(0)); fo << fixed << setprecision(7); cerr << fixed << setprecision(7); static char s[1 << 20]; fi>>(s + 1); static int D[1 << 20][30]; static int F[20][1 << 20]; int n = strlen(s + 1); for (int i = 1;i <= n;++i) s[i] -= 'a'; const int C = 26; for (int i = n;i;--i) { D[i][s[i]] = i + 1; int ok = 1; for (int k = s[i] + 1;k <= C;++k) { const int cnt = D[D[i][k - 1]][k - 1]; if (!cnt) { ok = 0; break; } D[i][k] = cnt; } if (ok) { for (int k = 0;k < s[i];++k) D[i][k] = D[D[i][26]][k]; } } for (int i = 1;i <= n;++i) F[0][i] = D[i][26]; for (int k = 1;k < 20;++k) for (int i = 1;i <= n;++i) F[k][i] = F[k - 1][F[k - 1][i]]; int m; fi>>m; while (m --) { int l,r; fi>>l>>r; ++r; for (int k = 19;k + 1;--k) if (F[k][l] && F[k][l] <= r) l = F[k][l]; puts(l == r ? "Yes" : "No"); } cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> #include<unordered_map> #include<unordered_set> using namespace std; int n; #define MAX 500002 char buf[MAX]; string s; int dp[MAX][27]; int can[MAX]; int lc[22][MAX]; bool search(int lef, int goal){ for (int i = 21; i >= 0; i--){ if (lc[i][lef] == -1)continue; if (lc[i][lef]==goal){ return true; } if (lc[i][lef] < goal){ lef = lc[i][lef]; lef++; } } return false; } int main(){ scanf("%s", buf); s = buf; n = s.size(); memset(dp, -1, sizeof(dp)); memset(lc, -1, sizeof(lc)); for (int i = n - 1; i >= 0; i--){ dp[i][s[i] - 'a'] = i; int id = s[i] - 'a'; for (int j = id; j + 'a' <= 'z'; j++){ dp[i][j + 1] = dp[dp[i][j] + 1][j]; if (dp[i][j + 1] == -1)break; } if (dp[i][26] != -1){ for (int j = 0; j < 26; j++){ if (dp[i][j] == -1){ dp[i][j] = dp[dp[i][26] + 1][j]; } } } } for (int i = n-1; i>=0; i--){ lc[0][i] = dp[i][26]; if (lc[0][i] > 0){ can[i] = 1 + can[dp[i][26] + 1]; } } for (int i = 1; i < 22; i++){ for (int j = 0; j < n; j++){ if (lc[i - 1][j] == -1){ lc[i][j] = -1; continue; } lc[i][j] = lc[i - 1][lc[i - 1][j]+1]; } } int q; cin >> q; while (q--){ int l, r; scanf("%d%d", &l, &r); l--; r--; if (dp[l][26] == -1){ puts("No"); continue; } if (search(l, r)){ puts("Yes"); } else{ puts("No"); } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> using namespace std; #define FOR(i,n) for (int i = 1 ; i <= n ; ++i) char s[500005]; int nxt[500005][30] ; int ST[500005][20] ; int n; void init () { n = strlen(s+1); for (int i = 0 ; i <= 'z' - 'a' ; ++i) nxt[n+1][i] = nxt[n+2][i] = n+1; for (int i = 0 ; i < 20 ; ++i) ST[n+1][i] = ST[n+2][i] = n + 1; for (int i = n ; i >= 1 ; --i) { nxt[i][s[i]-'a'] = i; for (int j = s[i] - 'a' + 1 ; j <= 'z' - 'a' ; ++j) nxt[i][j] = nxt[nxt[i][j-1]+1][j-1]; ST[i][0] = nxt[i]['z'-'a']; for (int j = 1 ; j < 20 ; ++j) ST[i][j] = ST[ST[i][j-1]+1][j-1]; for (int j = 0 ; j < s[i] - 'a' ; ++j) nxt[i][j] = nxt[ST[i][1]+1][j]; } } int gao (int l , int r) { for (int i = 19 ; l < r && i >= 1 ; --i) if (ST[l][i] < r) l = ST[l][i] + 1; else if (ST[l][i] == r) return 0; return 1; } int main (void) { scanf("%s",s+1); init (); int q , l , r ; cin >> q; FOR(i,q) { scanf("%d%d",&l,&r); cout << ((gao(l,r)&1)?"No":"Yes") << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> #define FOR(i,n) for(lli i=0;i<(lli)(n);++i) #define FORU(i,j,k) for(lli i=(j);i<=(lli)(k);++i) #define FORD(i,j,k) for(lli i=(j);i>=(lli)(k);--i) #define pb push_back #define mt make_tuple using namespace std; using lli = long long int; using pll = pair<lli, lli>; using vi = vector<lli>; using vvi = vector<vi>; using pii = tuple<lli, lli>; using vii = vector<pii>; using vvii = vector<vii>; #define X(a) get<0>(a) #define Y(a) get<1>(a) #define Z(a) get<2>(a) string TYPE(const int*) { return "%d"; } string TYPE(const lli*) { return "%lld"; } string TYPE(const char*) { return "%c"; } string TYPE(const char**) { return "%s"; } string TYPE(const unsigned int*) { return "%u"; } const int MAX_BUF = 5*100*1000+42; char buf[MAX_BUF]; void RD() {} template<typename T, typename... Args> void RD(T* v, Args... args) { scanf((" " + TYPE(v)).c_str(), v); RD(args...); } template<typename... Args> void RD(string* v, Args... args) { scanf(" %s", buf); (*v) = buf; RD(args...); } void PR(bool nl = true) { if(nl) printf("\n"); } template<typename T, typename... Args> void PR(bool nl, T v, Args... args) { printf((TYPE(&v) + " ").c_str(), v); PR(nl, args...); } template<typename... Args> void PR(bool nl, string& v, Args... args) { printf("%s", v.c_str()); PR(nl, args...); } template<typename... Args> void PR(Args... args) { PR(true, args...); } const long long int oo = 1000*1000*1000; struct Coord { int x, y; Coord(int x = 0, int y = 0) : x(x), y(y) {} Coord operator + (const Coord& droite) const { return Coord(x + droite.x, y + droite.y); } }; struct AB { int k; vector<lli> arbre; AB(int _k = 20, lli def = 0) { k = _k; FOR(i, 1 << k) arbre.push_back(i < (1 << (k-1)) ? 0LL : def); FORD(i, ((1 << (k-1)) - 1), 1) arbre[i] = arbre[i << 1] + arbre[(i << 1) ^ 1]; } void set(int i, lli x) { int feuille = i + (1 << (k-1)); arbre[feuille] = x; iset(feuille >> 1); } void iset(int noeud) { if(noeud) { arbre[noeud] = arbre[noeud << 1] + arbre[(noeud << 1) ^ 1]; iset(noeud >> 1); } } lli sum(int deb, int fin, int noeud = 1, int p = 0, int q = -1) { if(q < p) q = 1 << (k-1); if(deb <= p && q <= fin) return arbre[noeud]; if(deb >= q || fin <= p) return 0LL; int mil = (p + q) / 2; return sum(deb, fin, noeud << 1, p, mil) + sum(deb, fin, (noeud << 1) ^ 1, mil, q); } }; string s; int dyn[27][5*100*1000+42]; int suiv[5*100*1000+42]; vector<int> prec[5*100*1000+42]; int vin[5*100*1000+42], vout[5*100*1000+42]; bitset<5*100*1000+42> dejaVu; int cur; void dfs(int n) { assert(!dejaVu[n]); dejaVu[n] = true; vin[n] = cur++; for(auto v : prec[n]) dfs(v); vout[n] = cur++; } int main() { RD(&s); int n = s.length(); FOR(i, 26) dyn[i][n] = -1; dyn[26][n] = n; FOR(i, n) suiv[i] = -1; FORD(i, n-1, 0) { FOR(j, 27) { dyn[j][i] = -1; if(s[i]-'a' == j) dyn[j][i] = i; else if(s[i]-'a' < j) { int rjm = dyn[j-1][i]; if(rjm != -1) { int rjm2 = dyn[j-1][rjm+1]; if(rjm2 != -1) dyn[j][i] = rjm2; } } //printf("%2d%c", dyn[j][i], " \n"[j==26]); } FOR(j, 26) { if(dyn[26][i] != -1 && dyn[j][i] == -1) dyn[j][i] = dyn[j][dyn[26][i]+1]; } suiv[i] = dyn[26][i] == -1 ? -1 : dyn[26][i]+1; if(suiv[i] != -1) prec[suiv[i]].push_back(i); } FORD(i, n, 0) if(!dejaVu[i]) dfs(i); //FOR(i, n) // PR(vin[i], vout[i]); int q; RD(&q); FOR(iq, q) { int a, b; RD(&a, &b); --a; PR(vin[b] <= vin[a] && vout[a] <= vout[b] ? "Yes" : "No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; #define int long long #define inf (int)1000000007 #define mod (int)1000000007 #define rep(i, n) for(int i = 0; i < (n); i++) #define trep(i, n) for(int i = 0; i <= (n); i++) #define rrep(i, n) for(int i = (n) - 1; i >= 0; i--) #define rtrep(i, n) for(int i = (n); i >= 0; i--) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define mfor(i, s, t) for(int i = (s); i < (t); i++) #define tfor(i, s, t) for(int i = (s); i <= (t); i++) #define rfor(i, s, t) for(int i = (t) - 1; i >= (s); i--) string ds; int dp[514514][27]; int solve(int p, int m) { if(dp[p][m] < -1) { int w = ds[p] - 'a'; if(m == 26) { int now = p; bool ok = true; mfor(i, w, 26) { if(now >= ds.size() - 1) { dp[p][m] = -1; ok = false; break; } int t = solve(now + 1, i); if(t < 0) { dp[p][m] = -1; ok = false; break; } now = t; } if(ok) { dp[p][m] = now; } } else if(w == m) { dp[p][m] = p; } else if(w < m) { int t = solve(p, m - 1); if(t < 0 || t == ds.size() - 1) { dp[p][m] = -1; } else { int u = solve(t + 1, m - 1); if(u < 0) { dp[p][m] = -1; } else { dp[p][m] = u; } } } else { int t = solve(p, 26); if(t < 0 || t == ds.size() - 1) { dp[p][m] = -1; } else { int u = solve(t + 1, m); if(u < 0) { dp[p][m] = -1; } else { dp[p][m] = u; } } } } return dp[p][m]; } bool trw[514514]; vector<int> tree[514514]; vector<pair<int, int>> ddq[514514]; bool ns[514514]; bool ans[114514]; void solve2(int p) { for(auto i : ddq[p]) { if(ns[i.first]) { ans[i.second] = true; } else { ans[i.second] = false; } } ns[p] = true; for(auto i : tree[p]) { solve2(i); } ns[p] = false; } signed main() { cin >> ds; int dn = ds.size(); rep(i, dn) { rep(j, 27) { dp[i][j] = -2; } } rrep(i, dn) { rep(j, 27) { solve(i, j); } } trep(i, dn) { trw[i] = false; ns[i] = false; } rep(i, dn) { if(!trw[i]) { int a = i; while(a < dn && solve(a, 26) > 0 && !trw[a]) { trw[a] = true; tree[solve(a, 26) + 1].push_back(a); a = solve(a, 26) + 1; } } } int dq; cin >> dq; rep(i, dq) { int l, r; cin >> l >> r; l--; ddq[l].push_back(make_pair(r, i)); } trep(i, dn) { if(!trw[i]) { solve2(i); } } rep(i, dq) { if(ans[i]) { cout << "Yes" << endl; } else { cout << "No" << endl; } } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; const int N = 500010; const int LOG = 21; int pr[N][21], f[N][28], a[N]; char ch[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int main() { scanf("%s", ch); int len = strlen(ch); for (int i = 0; i < len; i ++) a[i] = ch[i] - 'a'; for (int j = 0; j <= 26; j ++) f[len][j] = -1; for (int i = len - 1; i >= 0; i --) { for (int j = 0; j <= 26; j ++) f[i][j] = -1; f[i][a[i]] = i + 1; bool flag = false; for (int j = a[i] + 1; j <= 26; j ++) { int at = f[f[i][j - 1]][j - 1]; if (at == -1) { flag = true; break; } f[i][j] = at; } if (!flag) for (int j = 0; j < a[i]; j ++) f[i][j] = f[f[i][26]][j]; } for (int i = 0; i <= len; i ++) pr[i][0] = f[i][26]; for (int j = 1; j < LOG; j ++) for (int i = 0; i <= len; i ++) { if (pr[i][j - 1] == -1) pr[i][j] = -1; else pr[i][j] = pr[pr[i][j - 1]][j - 1]; } int T; T = read(); for(int Case = 1; Case <= T; Case ++) { int x, y; x = read(); y = read(); x --; for(int i = LOG - 1; i >= 0; i --) { if(pr[x][i] == -1) continue; if(pr[x][i] <= y) x = pr[x][i]; } if(x == y) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <stdio.h> char S[500500]; int C[500500][27],V[500500][19]; int main() { scanf ("%s",S+1); int L = 0; while (S[L+1]) L++; for (int i=L;i>=1;i--){ int p = S[i]-'a'; C[i][p] = i+1; for (int j=p+1;j<=26;j++) C[i][j] = C[C[i][j-1]][j-1]; V[i][0] = C[i][26]; if (V[i][0]) for (int j=0;j<p;j++) C[i][j] = C[V[i][0]][j]; for (int j=1;j<19;j++) V[i][j] = V[V[i][j-1]][j-1]; } int Q; scanf ("%d",&Q); while (Q--){ int l,r; scanf ("%d %d",&l,&r); r++; for (int i=18;i>=0;i--){ if (V[l][i] && V[l][i] <= r) l = V[l][i]; } puts(l == r? "Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <numeric> #include <functional> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <sstream> #include <string> #define _repargs(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i) #define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define all(x) (x).begin(),(x).end() #define mod 1000000007 #define inf 2000000007 #define mp make_pair #define pb push_back typedef long long ll; using namespace std; template <typename T> inline void output(T a, int p = 0) { if(p) cout << fixed << setprecision(p) << a << "\n"; else cout << a << "\n"; } // end of template int main() { cin.tie(0); ios::sync_with_stdio(0); // source code string s; int Q; cin >> s >> Q; vector<pair<int, int>> A(Q); rep(i, Q) cin >> A[i].first >> A[i].second; // next[l][0]: s[l, r)が空となる最小のr // next[l]['#']: s[l, r)が"#"となる最小のr vector<vector<int>> next(s.size() + 1, vector<int>(27, inf));// "", a, b, c, ... for(int i = (int)s.size() - 1; i >= 0; i--){ int ind = s[i] - 'a' + 1; // next[i][s[i]] = s[i] + 1 next[i][ind] = i + 1; for(int j = (ind + 1) % 27; j != ind; j = (j + 1) % 27){ if(j == 1) continue; int prev = (j + 26) % 27; if(next[i][prev] != inf){ next[i][j] = min(next[i][j], next[next[i][prev]][prev]); } } if(next[i][0] != inf){ rep(j, 1, 27){ next[i][j] = min(next[i][j], next[next[i][0]][j]); } } } vector<vector<int>> D(20, vector<int> (s.size() + 1, inf)); rep(i, s.size() + 1){ D[0][i] = next[i][0]; } rep(i, 19){ rep(j, s.size() + 1){ if(D[i][j] != inf){ D[i + 1][j] = D[i][D[i][j]]; } } } rep(i, Q){ int l = A[i].first - 1, r = A[i].second; for(int j = 19; j >= 0; j--){ if(D[j][l] <= r) l = D[j][l]; // cout << l << ", " << j << ":" << D[j][l] << endl; } // cout << i << ":" << l << "," << r << endl; if(l == r){ output("Yes"); } else output("No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; template <typename T> using Matrix = vector<vector<T>>; template <typename T> Matrix<T> matrix(const int R, const int C, const T x = T()) { return Matrix<T>(R, vector<T>(C, x)); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; const auto n = int(s.size()); auto f = matrix<int>(n+2, 27, n+1); for (auto i = n-1; i >= 0; i--) { const auto start = s[i]-'a'; f[i][start] = i+1; for (auto c = start+1; c <= 26; c++) f[i][c] = f[f[i][c-1]][c-1]; for (auto c = 0; c < start; c++) f[i][c] = f[f[i][26]][c]; } const auto log = 18; auto jumps = matrix<int>(log+1, n+2); for (auto i = 0; i < n+2; i++) jumps[0][i] = f[i][26]; for (auto k = 1; k <= log; k++) for (auto i = 0; i < n+2; i++) jumps[k][i] = jumps[k-1][jumps[k-1][i]]; int Q; cin >> Q; for (auto q = 0; q < Q; q++) { int l, r; cin >> l >> r; l--; r--; auto x = l; for (auto k = log; k >= 0; k--) if (jumps[k][x] <= r+1) x = jumps[k][x]; cout << (x == r+1 ? "Yes" : "No") << "\n"; } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stack> #include <queue> #include <set> #include <map> using namespace std; #define MOD @ #define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; char S[505050]; int N, Q; int L[101010], R[101010]; int dp[505050][27]; /* int uf[505050]; int root(int p) { return uf[p] < 0 ? p : (uf[p] = root(uf[p])); } void join(int p, int q) { p = root(p); q = root(q); if (p != q) { uf[p] += uf[q]; uf[q] = p; } } */ vector<int> ch[505050]; int lf[505050], rg[505050]; int idx; void visit(int p) { if (lf[p] != -1) return; lf[p] = idx++; for (int q : ch[p]) visit(q); rg[p] = idx; // printf("%d: %d %d\n", p, lf[p], rg[p]); } int dbl[505050][20]; int main() { scanf("%s%d", S, &Q); N = strlen(S); for (int i = 0; i < Q; ++i) { scanf("%d%d", L + i, R + i); --L[i]; } for (int i = N - 1; i >= 0; --i) { for (int j = 0; j <= 26; ++j) { dp[i][j] = N + 10; } dp[i][S[i] - 'a'] = i + 1; for (int j = (int)(S[i] - 'a') + 1; j <= 26; ++j) { if (dp[i][j - 1] < N) { int ij = dp[i][j - 1]; dp[i][j] = min(dp[i][j], dp[ij][j - 1]); } } if (dp[i][26] < N) { int i26 = dp[i][26]; for (int j = 0; j < (int)(S[i] - 'a'); ++j) { dp[i][j] = min(dp[i][j], dp[i26][j]); } } } for (int i = 0; i < N; ++i) if (dp[i][26] <= N) { ch[dp[i][26]].push_back(i); } for (int i = 0; i < N; ++i) dbl[i][0] = dp[i][26]; for (int i = 1; i < 20; ++i) { for (int j = 0; j < N; ++j) { dbl[j][i] = (dbl[j][i - 1] < N ? dbl[dbl[j][i - 1]][i - 1] : (N + 10)); } } idx = 0; for (int i = 0; i <= N; ++i) lf[i] = -1; for (int i = N; i >= 0; --i) visit(i); for (int i = 0; i < Q; ++i) { int l = L[i], r = R[i]; /*for (int j = 19; j >= 0; --j) { if (l < N && dbl[l][j] <= r) { l = dbl[l][j]; } }*/ //bool isok = l == r; bool isok = (lf[r] <= lf[l] && rg[l] <= rg[r]); puts(isok ? "Yes" : "No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include "bits/stdc++.h" using namespace std; const int N = 5e5 + 5; const int C = 27; const int LN = 19; int n; char str[N]; int dp[N][C]; int nxt[N][C]; int q; int l , r; int arr[N]; int bin[LN][N]; int main(){ scanf("%s" , str + 1); n = strlen(str + 1); memset(dp , -1 , sizeof(dp)); memset(nxt , -1 , sizeof(nxt)); for(int i = n ; i >= 1 ; --i){ int c = str[i] - 'a'; dp[i][c] = i; for(int j = c + 1 ; j <= 26 ; ++j){ dp[i][j] = nxt[dp[i][j - 1] + 1][j - 1]; } for(int j = 0 ; j < C ; ++j){ if(dp[i][j] == -1){ nxt[i][j] = nxt[dp[i][26] + 1][j]; } else{ nxt[i][j] = dp[i][j]; } } } for(int i = 0 ; i <= n + 2 ; ++i){ if(dp[i][26] == -1){ arr[i] = n + 2; } else{ arr[i] = dp[i][26] + 1; } } for(int i = 0 ; i <= n + 2 ; ++i){ bin[0][i] = arr[i]; } for(int i = 1 ; i < LN ; ++i){ for(int j = 0 ; j <= n + 2 ; ++j){ bin[i][j] = bin[i - 1][bin[i - 1][j]]; } } scanf("%d" , &q); while(q--){ scanf("%d %d" , &l , &r); for(int i = LN - 1 ; i >= 0 ; --i){ if(bin[i][l] <= r){ l = bin[i][l]; } } puts((arr[l] == r + 1) ? "Yes" : "No"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<vector> #include<cmath> #include<map> #include<cstdlib> #include<iostream> #include<sstream> #include<fstream> #include<string> #include<algorithm> #include<cstring> #include<cstdio> #include<set> #include<stack> #include<bitset> #include<functional> #include<ctime> #include<queue> #include<deque> #include<complex> #include<cassert> using namespace std; #define pb push_back #define pf push_front typedef long long lint; typedef complex<double> P; #define mp make_pair #define fi first #define se second typedef pair<int,int> pint; #define All(s) s.begin(),s.end() #define rAll(s) s.rbegin(),s.rend() #define REP(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) REP(i,0,n) int dp[30][512810]; int dbl[512810][30]; string s;int n; int cal(int i,int j,int ne){ if(dp[i][j]>ne){ dp[i][j]=ne;return 1; } return 0; } int main() { int q,x,y,inf=512810; cin>>s; n=s.size(); //memset(dp,-1,sizeof(dp));memset(dbl,-1,sizeof(dbl)); rep(i,30) rep(j,inf-10) dp[i][j]=dbl[j][i]=inf; for(int j=n-1;j>=0;j--){ rep(k,2){ int f=0; rep(i,27){ if(s[j]==('a'+i)){ f|=cal(i,j,j+1);//dp[i][j]=j+1; } if(i>0 && dp[i-1][j]<inf){ f|=cal(i,j,dp[i-1][dp[i-1][j]]);//dp[i][j]=min(dp[i-1][dp[i-1][j]],dp[i][j]); } if(i<26 && dp[26][j]<inf){ f|=cal(i,j,dp[i][dp[26][j]]);//dp[i][j]=min(dp[i][dp[26][j]],dp[i][j]); } //if(dp[i][j+1]>=0) cout<<i<<' '<<j+1<<' '<<dp[i][j+1]<<endl; } //if(f<1) break; } } rep(i,n+10){ dbl[i][0]=dp[26][i]; //if(i<=n) cout<<i<<' '<<dp[26][i]<<endl; } rep(i,26) rep(j,n+10){ if(dbl[j][i]<inf) dbl[j][i+1]=dbl[dbl[j][i]][i]; //if(i<4 && j<=n) cout<<i<<' '<<j<<' '<<dbl[j][i]<<endl; } cin>>q; rep(i,q){ cin>>x>>y;x--; for(int i=22;i>=0;i--){ if(dbl[x][i]<=y) x=dbl[x][i]; //cout<<x<<endl; } if(y==x) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #define N 1000005 #define LL long long #define oo (1<<30) using namespace std; int n,q,ans,f[27][N],F[20][N]; char st[N]; int main() { int i,j,l,r; scanf("%s",st+1); n = strlen(st+1); for(i=1;i<=n;i++) f[st[i]-'a'][i]=i+1; for(i=n-1;i>=1;i--){ for(j=st[i]-'a'+1;j<=26;j++) f[j][i]=f[j-1][f[j-1][i]]; for(j=0;j<st[i]-'a';j++) f[j][i]=f[j][f[26][i]]; } for(i=1;i<=n;i++) F[0][i]=f[26][i]; for(i=1;i<20;i++) for(j=1;j<=n;j++) F[i][j]=F[i-1][F[i-1][j]]; scanf("%d",&q); while(q--){ scanf("%d %d",&l,&r),ans=0; for(i=19;i>=0;i--) if(F[i][l]<=r+1&&F[i][l]) l=F[i][l]; if(l>r) printf("Yes\n"); else printf("No\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; #define endl '\n' typedef long long int64; typedef pair<int,int> pii; typedef vector<int> vi; const int oo = 0x3f3f3f3f; const double eps = 1e-9; const int maxn = 100000 + 10; int mod(int a){ a = (a + 27) % 27; return a; } void putmin(int &a, int b){ a = min(a, b); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); #ifdef MARX freopen("data.in", "r", stdin); // freopen("data.out", "w", stdout); #endif string s; cin >> s; int n = (int)s.length(); vector<vi> nxt(n, vi(27, oo)); for (int i = n - 1; i >= 0; --i){ int x = s[i] - 'a'; nxt[i][x] = i + 1; for (int j = 0; j < 26; ++j){ if ( nxt[i][mod(x + j)] < n && mod(x + j + 1) != 0){ nxt[i][mod(x + j + 1)] = nxt[ nxt[i][mod(x + j)] ][ mod(x + j) ]; } if ( nxt[i][26] < n ) putmin( nxt[i][ mod(x + j + 1) ], nxt[ nxt[i][26] ][ mod(x + j + 1) ] ); } } vector<vi> go(1, vi(n, oo)); bool some = false; for (int i = 0; i < n; ++i){ go[0][i] = nxt[i][26]; if (go[0][i] <= n) some = true; } while (some){ some = false; vi ngo(n, oo); for (int i = 0; i < n; ++i){ if (go.back()[i] < n){ some = true; ngo[i] = go.back()[ go.back()[i] ]; } } go.push_back( ngo ); } int q; cin >> q; while (q--){ int b, e; cin >> b >> e; b--; for (int i = go.size() - 1; i >= 0 && b < e; --i) while (b < e && go[i][b] <= e) b = go[i][b]; cout << (b == e ? "Yes" : "No") << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <stdio.h> #include <iostream> using namespace std; const int maxn = 500000 + 10; int f[maxn][27]; int step[maxn][27]; int main() { int n; string s; cin >> s; n = s.size(); for (int i = n + 1; i > -1; i--) for (int j = 0; j < 26; j++) f[i][j] = n + 1; for (int i = n - 1; i > -1; i--) { f[i][ s[i] - 'a' ] = i + 1; for (int c = s[i] - 'a'; c < 26; c++) { f[i][c + 1] = f[ f[i][c] ][c]; } for (int c = 0; c < s[i] - 'a'; c++) f[i][c] = f[ f[i][26] ][c]; } for (int i = 0; i < n; i++) step[i][0] = f[i][26]; step[n][0] = n + 1; step[n + 1][0] = n + 1; int bits; for (bits = 1; (1 << bits) < n; bits++) for (int i = 0; i <= n + 1; i++) step[i][bits] = step[step[i][bits - 1]][bits - 1]; int q, l, r; cin >> q; while (q--) { cin >> l >> r; int cur = l - 1; for (int i = bits - 1; i > -1; i--) { if (step[cur][i] <= r) cur = step[cur][i]; } if (cur == r) { printf("Yes\n"); } else printf("No\n"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; template <typename T> using Matrix = vector<vector<T>>; int main() { string s; cin >> s; const auto n = int(s.size()); auto f = Matrix<int>(n+2, vector<int>(27, n+1)); auto children = vector<vector<int>>(n+2); children[n+1].push_back(n); for (auto i = n-1; i >= 0; i--) { const auto start = s[i]-'a'; f[i][start] = i+1; for (auto c = start+1; c <= 26; c++) f[i][c] = f[f[i][c-1]][c-1]; for (auto c = 0; c < start; c++) f[i][c] = f[f[i][26]][c]; children[f[i][26]].push_back(i); } auto time = 0; auto start_times = vector<int>(n+2), exit_times = vector<int>(n+2); const function<void(int)> dfs = [&](const int v) { start_times[v] = time++; for (const auto c : children[v]) dfs(c); exit_times[v] = time; }; dfs(n+1); int Q; cin >> Q; for (auto q = 0; q < Q; q++) { int l, r; cin >> l >> r; l--; r--; cout << (start_times[r+1] <= start_times[l] && start_times[l] < exit_times[r+1] ? "Yes": "No") << endl; } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define FORD(i,b,a) for (int i = (int)(b) - 1; i >= a; --i) #define REP(i,N) FOR(i,0,N) #define st first #define nd second #define pb push_back typedef pair<int, int> PII; typedef long long LL; const int MAXN=6e5; char S[MAXN]; int DP[MAXN][27]; int reach[MAXN]; vector<PII> queries[MAXN]; bool results[MAXN]; set<int> positions[MAXN]; int N; int nextgood[MAXN][20]; void init() { for (int j = 1, shift = 1; j < 20; ++j, (shift <<= 1)) { REP(i,N+1) { if (nextgood[i][j-1] == -1) { nextgood[i][j] = -1; continue; } nextgood[i][j] = nextgood[nextgood[i][j-1]][j-1]; } } } bool is_good(int L, int R) { FORD(j,20,0) { if (nextgood[L][j] != -1 && nextgood[L][j] <= R) { L = nextgood[L][j]; // printf("%d %d->%d\n", L, R, DP[L][]) } } return L == R; } int main() { scanf("%s", S); N = strlen(S); REP(i,N) S[i] -= 'a'; REP(i,26) DP[N][i] = -1; DP[N][26] = N; reach[N] = N; FORD(i,N,0) { REP(j,27) DP[i][j] = -1; DP[i][S[i]] = i + 1; FOR(j,S[i]+1,27) { if (DP[i][j-1] == -1) break; DP[i][j] = DP[DP[i][j-1]][j-1]; } if (DP[i][26] != -1) REP(j,26) if (DP[i][j] == -1) DP[i][j] = DP[DP[i][26]][j]; } REP(i,N+1) nextgood[i][0] = DP[i][26]; init(); // REP(i,N+1) REP(j,20) { // if (nextgood[i][j] != -1) printf("%d %d=%d\n", i, j, nextgood[i][j]); // } int Q; scanf("%d", &Q); REP(i,Q) { int l, r; scanf("%d%d", &l, &r); --l, --r; bool res = is_good(l, r+1); printf(res ? "Yes\n" : "No\n"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <fstream> #include <set> #include <map> #include <string> #include <vector> #include <bitset> #include <algorithm> #include <cstring> #include <cstdlib> #include <cmath> #include <cassert> #include <queue> #define mp make_pair #define pb push_back typedef long long ll; typedef long double ld; using namespace std; const int MAXN = 510000; const int LOG = 20; char s[MAXN]; int go[MAXN][27]; int up[LOG][MAXN]; int n; int main() { scanf("%s", s); n = strlen(s); for (int i = 0; i <= 26; ++i) go[n][i] = n + 1; for (int i = 0; i <= 26; ++i) go[n + 1][i] = n + 1; for (int i = n - 1; i >= 0; --i) { int c = s[i] - 'a'; go[i][c] = i + 1; int now = i + 1; for (int j = c + 1; j <= 26; ++j) { now = go[now][j - 1]; go[i][j] = now; } for (int j = 0; j < c; ++j) go[i][j] = go[now][j]; } for (int i = 0; i <= n + 1; ++i) up[0][i] = go[i][26]; for (int i = 1; i < LOG; ++i) for (int j = 0; j <= n + 1; ++j) up[i][j] = up[i - 1][up[i - 1][j]]; int q; scanf("%d", &q); for (int i = 0; i < q; ++i) { int l, r; scanf("%d%d", &l, &r); --l; for (int j = LOG - 1; j >= 0; --j) { if (up[j][l] <= r) l = up[j][l]; } if (l != r) printf("No\n"); else printf("Yes\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
// <string.cpp> - Sat Mar 25 20:06:07 2017 // This file is created by XuYike's black technology automatically. // Copyright (C) 2015 ChangJun High School, Inc. // I don't know what this program is. #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long lol; template<typename T> inline void gg(T &res){ res=0;T fh=1;char ch=getchar(); while((ch>'9'||ch<'0')&&ch!='-')ch=getchar(); if(ch=='-')fh=-1,ch=getchar(); while(ch>='0'&&ch<='9')res=res*10+ch-'0',ch=getchar(); res*=fh; } inline int gi(){int x;gg(x);return x;} inline lol gl(){lol x;gg(x);return x;} const int MAXN=500010; const int K=21; const int INF=1e9; char s[MAXN]; int a[MAXN][27],b[K][MAXN]; int main(){ scanf("%s",s+1); int n=strlen(s+1); for(int i=n;i;i--){ a[i][s[i]-'a']=i+1; for(int j=s[i]-'a'+1;j<=26;j++)a[i][j]=a[a[i][j-1]][j-1]; for(int j=0;j<s[i]-'a';j++)a[i][j]=a[a[i][26]][j]; } for(int i=1;i<=n;i++)b[0][i]=a[i][26]; for(int j=1;j<K;j++) for(int i=1;i<=n;i++)b[j][i]=b[j-1][b[j-1][i]]; int q=gi(); while(q--){ int l=gi(),r=gi()+1; for(int i=K-1;i>=0;i--) if(b[i][l]&&b[i][l]<=r)l=b[i][l]; puts(l==r?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout); using namespace std; const int MAXN = 1000005; int n, s[MAXN], Q; int ne[MAXN][27], ST[MAXN][19]; void get_str() { char ch = getchar(); while (ch < 'a' && ch > 'z') ch = getchar(); while (ch >= 'a' && ch <= 'z') s[++n] = ch - 'a', ch = getchar(); } int main() { get_str(); for (int i = n; i; i--) { ne[i][s[i]] = i + 1; for (int j = s[i] + 1; j <= 26; j++) ne[i][j] = ne[ne[i][j - 1]][j - 1]; for (int j = 0; j < s[i]; j++) ne[i][j] = ne[ne[i][26]][j]; ST[i][0] = ne[i][26]; } for (int j = 1; j <= 18; j++) for (int i = 1; i <= n; i++) ST[i][j] = ST[ST[i][j - 1]][j - 1]; scanf("%d", &Q); for (int t = 1, l, r; t <= Q; t++) { scanf("%d %d", &l, &r); for (int i = 18; ~i; i--) if (ST[l][i] <= r + 1 && ST[l][i]) l = ST[l][i]; printf(l == r + 1 ? "Yes\n" : "No\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> #define X first #define Y second #define mp make_pair using namespace std; typedef pair<int,int> PII; const int N = 500010; char s[N]; int n,m,x,y,f[N][30],a[N]; bool ans[100010]; PII q[100010]; bool pd[N]; vector<int> v[N],Q[N]; void dfs(int x){ pd[x]=true; int t; for(int i=Q[x].size()-1;i>=0;i--) { if(pd[q[t=Q[x][i]].Y]) ans[t]=true; // printf("%d %d %d\n",x,Q[x][i],ans[Q[x][i]]); } for(int i=v[x].size()-1;i>=0;i--) dfs(v[x][i]); pd[x]=false; } int S[N],tail,now[N]; void work(){ S[1] = n+1; tail = 1; int tmp,t; for(int i=1;i<=n+1;i++) now[i]=v[i].size(); while(tail){ t = S[tail]; pd[t]=true; if(now[t]){ S[++tail] = v[t][--now[t]]; } else{ for(int i=Q[t].size()-1;i>=0;i--) if(pd[q[tmp=Q[t][i]].Y]) ans[tmp]=true; pd[t]=false; tail--; } } } int main(){ //gets(s+1); scanf("%s",s+1); n = strlen(s+1); for(int i=1;i<=n;i++) a[i]=s[i]-'a'; for(int i=0;i<=26;i++) f[n+2][i]=f[n+1][i]=n+1; //cout<<n<<endl; for(int i=n;i>0;i--){ f[i][a[i]]=i; for(int j=a[i]+1;j<=26;j++) f[i][j]=f[f[i][j-1]+1][j-1]; for(int j=0;j<a[i];j++) f[i][j]=f[f[i][26]+1][j]; if(f[i][26]<=n) v[f[f[i][26]+1][26]].push_back(f[i][26]); } // for(int i=1;i<=n;i++) printf("%d ",f[i][26]); puts(""); scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); q[i] = mp(x,y); if(f[x][26]<=n) Q[f[x][26]].push_back(i); } // dfs(n+1); work(); for(int i=1;i<=m;i++) if(ans[i]) puts("Yes"); else puts("No"); return 0; } /* yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 */
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <fstream> #include <sstream> #include <algorithm> #include <cstdio> #include <cctype> #include <cassert> #include <cmath> #include <ctime> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <vector> #include <list> #include <map> #include <set> #include <bitset> #include <stack> #include <iomanip> #include <utility> #include <functional> using namespace std; #define mp make_pair #define pb push_back #define pf push_front #define X first #define Y second #define rg register #define il inline #define lch(x) ((x)<<1) #define rch(x) ((x)<<1^1) #define eprintf(...) fprintf(stderr,__VA_ARGS__) #define rep0(i,n) for(register int (i)=0;(i)<(n);++(i)) #define rep(i,st,ed) for(register int (i)=(st);(i)<=(ed);++(i)) typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef double dbl; typedef long double ldb; typedef pair<int,int> pii; typedef pair<ll,ll> pll; template<typename T> inline T qmin(const T a,const T b) {return a<b?a:b;} template<typename T> inline T qmax(const T a,const T b) {return a>b?a:b;} template<typename T> inline void getmin(T &a,const T b) {if(a>b) a=b;} template<typename T> inline void getmax(T &a,const T b) {if(a<b) a=b;} inline void fileio(string s){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } const int inf=1e9+7; const ll linf=1e17+7; const int N=5e5+7,S=26,logN=21; char s[N]; int succ[N][S],nxt[N][logN]; int n,q; int main(){ scanf("%s",s+1),n=strlen(s+1); for(int i=0;i<S;++i) succ[n+2][i]=succ[n+1][i]=n+2; nxt[n+2][0]=nxt[n+1][0]=n+2; for(int i=n;i>=1;--i){ int cur=s[i]-'a'; succ[i][cur]=i+1; for(int c=cur+1;c<S;++c) succ[i][c]=succ[succ[i][c-1]][c-1]; nxt[i][0]=succ[succ[i][S-1]][S-1]; int pos=nxt[i][0]; for(int c=0;c<cur;++c) succ[i][c]=succ[pos][c]; } for(int i=1;i<=n+2;++i) eprintf("%d ",nxt[i][0]);eprintf("\n"); for(int i=1;i<logN;++i){ for(int j=1;j<=n+2;++j){ nxt[j][i]=nxt[nxt[j][i-1]][i-1]; // if(i<=2) cerr<<"pos="<<j<<" level="<<i<<" val="<<nxt[j][i]<<endl; } } scanf("%d",&q); while(q--){ int l,r; scanf("%d%d",&l,&r),++r; int pos=l; for(int i=logN-1;~i;--i){ // cerr<<pos<<"->"; if(nxt[pos][i]<=r) pos=nxt[pos][i]; // cerr<<pos<<endl; } // cerr<<pos<<endl; puts(pos==r?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; #define sz(x) (int)(x.size()) #define fi(a, b) for(int i=a;i<b;++i) #define fj(a, b) for(int j=a;j<b;++j) #define fk(a, b) for(int k=a;k<b;++k) #define mp make_pair #define pb push_back ///////////////////////// int const N = 5e5 + 41; int const K = 27; int const M = 20; int n, m, dp[N][K], ans[N], par[N]; string s; vector<pair<int, int> > q[N]; int len[N], spt[N][M]; void buildSpt(){ fi(0, N) fj(0, M) spt[i][j] = n; fi(0, n){ spt[i][0] = dp[i][26]; } for(int i=1;i<M;++i){ for(int j=0;j<n;++j){ int nxt = spt[j][i-1] + 1; spt[j][i] = spt[nxt][i-1]; } } } void solve(){ cin >> s; n = sz(s); fi(0, n) s[i] -= ('a'); cin >> m; fi(0, m){ int l, r; cin >> l >> r; --l;--r; q[l].pb(mp(r, i)); } for(int i=n-1;i>=0;--i){ fj(0, K) dp[i][j] = n; dp[i][s[i]] = i; fj(s[i]+1, K){ int nxt = dp[i][j-1] + 1; if(nxt == n || dp[nxt][j-1] == n) break; dp[i][j] = dp[nxt][j-1]; } int nxt = dp[i][26] + 1; if(nxt < n){ fj(0, s[i]){ dp[i][j] = dp[nxt][j]; } } } buildSpt(); fi(0, n){ fj(0, sz(q[i])){ int r = q[i][j].first; int id = q[i][j].second; int p = i; for(int k=M-1;k>=0;--k){ if(spt[p][k] <= r) p = spt[p][k] + 1; } if(p == r + 1) ans[id] = 1; } } fi(0, m) if(ans[i]) cout << "Yes" << endl; else cout << "No" << endl; } int main(){ #ifdef _DEBUG freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif solve(); return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <iomanip> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> #include <cstdint> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } template<class T> inline bool chmax(T &a, T b){ if(a<b){ a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } int dp[500010][61]; int main(){ string s; cin >> s; int n = s.size(); Fill(dp,n+1); for(int i=n-1;i>=0;i--){ int k = s[i]-'a'; dp[i][k] = i+1; for(int j=k+1;j<=26;j++){ dp[i][j] = dp[dp[i][j-1]][j-1]; } for(int j=0;j<k;j++){ dp[i][j] = dp[dp[i][26]][j]; } for(int j=27;j<=60;j++){ dp[i][j] = dp[dp[i][j-1]][j-1]; } } int q; cin >> q; rep(i,q){ int l,r; cin >> l >> r; l--; bool ok = 0; int id = l; for(int j=60;j>=26;j--){ if(dp[id][j]<=r)id = dp[id][j]; } ok = id==r; if(ok){ cout << "Yes\n"; }else{ cout << "No\n"; } } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <string> #include <cstring> using namespace std; const int A = 26; const int N = 500100; const int LOG = 20; int n; char s[N]; int a[N][A]; int b[N][LOG]; int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); scanf(" %s ", s); n = strlen(s); for (int i = 0; i <= n; i++) { for (int j = 0; j < A; j++) a[i][j] = -1; for (int j = 0; j < LOG; j++) b[i][j] = -1; } for (int i = n - 1; i >= 0; i--) { int x = (int)(s[i] - 'a'); a[i][x] = i + 1; for (int c = x + 1; c < A; c++) { if (a[i][c - 1] == -1) { a[i][c] = -1; continue; } a[i][c] = a[a[i][c - 1]][c - 1]; } if (a[i][A - 1] == -1) continue; b[i][0] = a[a[i][A - 1]][A - 1]; if (b[i][0] == -1) continue; for (int c = 0; c < x; c++) a[i][c] = a[b[i][0]][c]; for (int j = 1; j < LOG; j++) { if (b[i][j - 1] == -1) break; b[i][j] = b[b[i][j - 1]][j - 1]; } } int q; scanf("%d", &q); while(q--) { int l, r; scanf("%d%d", &l, &r); l--; for (int k = LOG - 1; k >= 0; k--) { if (b[l][k] == -1 || b[l][k] > r) continue; l = b[l][k]; } if (l == r) printf("Yes\n"); else printf("No\n"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<iostream> #include<cstdio> #include<algorithm> #include<set> #include<map> #include<queue> #include<cassert> #define PB push_back #define MP make_pair #define sz(v) (in((v).size())) #define forn(i,n) for(in i=0;i<(n);++i) #define forv(i,v) forn(i,sz(v)) #define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i) #define all(v) (v).begin(),(v).end() using namespace std; typedef long long in; typedef vector<in> VI; typedef vector<VI> VVI; const in asz=26; const in mx=500009; in fst[mx][asz+1]; string s; in n; in nxte[mx][22]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>s; n=sz(s); forn(i,asz+1) fst[n][i]=n; for(in i=n-1;i>=0;--i){ forn(j,asz+1) fst[i][j]=n; fst[i][s[i]-'a']=i; in u=i+1; in cr=s[i]-'a'; while(u<n && cr<asz){ fst[i][cr+1]=fst[u][cr]; u=fst[i][cr+1]+1; ++cr; } if(fst[i][asz]<n){ for(in j=0;fst[i][j]==n;++j) fst[i][j]=fst[fst[i][asz]+1][j]; } } forn(i,n) nxte[i][0]=fst[i][asz]; forn(j,22) nxte[n][j]=nxte[n+1][j]=n; for(in i=n-1;i>=0;--i){ for(in j=1;j<22;++j) nxte[i][j]=nxte[nxte[i][j-1]+1][j-1]; } in q; cin>>q; in l,r; forn(zz,q){ cin>>l>>r; --l; --r; in cu=21; while(cu>=0){ if(nxte[l][cu]<=r) l=nxte[l][cu]+1; --cu; } if(nxte[l][0]<=r) l=nxte[l][0]+1; if(l==r+1) cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<stdio.h> #include<vector> #include<algorithm> #include<string> #include<iostream> using namespace std; int dp[505050][27]; int nxt[20][505050]; int main() { string s; cin >> s; for (int i = 0; i < 27; i++)dp[s.size()][i] = dp[s.size() + 1][i] = s.size() + 1; for (int i = s.size() - 1; i >= 0; i--) { dp[i][s[i] - 'a'] = i + 1; for (char z = s[i] + 1; z <= 'z' + 1; z++)dp[i][z - 'a'] = dp[dp[i][z - 'a' - 1]][z - 'a' - 1]; for (char z = 'a'; z < s[i]; z++)dp[i][z - 'a'] = dp[dp[i][26]][z - 'a']; nxt[0][i] = dp[i][26]; } nxt[0][s.size()] = nxt[0][s.size() + 1] = s.size() + 1; for (int i = 1; i < 20; i++)for (int j = 0; j <= s.size() + 1; j++)nxt[i][j] = nxt[i - 1][nxt[i - 1][j]]; int query; scanf("%d", &query); for (int p = 0; p < query; p++) { int za, zb; scanf("%d%d", &za, &zb); za--; for (int i = 19; i >= 0; i--)if (nxt[i][za] <= zb)za = nxt[i][za]; printf((za == zb) ? "Yes\n" : "No\n"); } }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include <bits/stdc++.h> using namespace std; void chmin(int& a, int b){ a = min(a, b); } int main(){ string S; cin >> S; int N = S.size(); vector<vector<int>> dp(N+2, vector<int>(27, N+1)); for(int i=0; i<N; i++) dp[i][S[i]-'a'] = i+1; for(int i=N-1; i>=0; i--){ for(int j=0; j<26; j++) chmin(dp[i][j+1], dp[dp[i][j]][j]); for(int j=0; j<26; j++) chmin(dp[i][j], dp[dp[i][26]][j]); } vector<vector<int>> doub(N+2, vector<int>(19, N+1)); for(int i=0; i<N; i++) if(dp[i][26] != N+1) doub[i][0] = dp[i][26]; for(int k=0; k<18; k++) for(int i=0; i<N; i++) doub[i][k+1] = doub[doub[i][k]][k]; int Q; cin >> Q; while(Q--){ int l, r; cin >> l >> r; l--; for(int k=18; k>=0; k--) if(doub[l][k] <= r) l = doub[l][k]; cout << (l==r ? "Yes" : "No") << endl; } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; const int inf = 0x3f3f3f3f; #define Rep(i,a,b) for(register int i=(a);i<=int(b);++i) #define Dep(i,a,b) for(register int i=(a);i>=int(b);--i) #define rep(i,a,b) for(register int i=(a);i<int(b);++i) #define mem(x,v) memset(x,v,sizeof(x)) #define pc putchar #define gc getchar #define fi first #define se second #define debug(x) cout << #x" = " << x << endl; inline ll read(){ register ll x=0,f=1;register char c=gc(); for(;!isdigit(c);c=gc())if(c=='-')f=-1; for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48); return x*f; } #define rd read void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');} void writeln(ll x){write(x);puts("");} const int N=500006; char s[N]; int jump[N],nxt[N],fa[N][22],n,to[N][31],q[N],trans[N][31]; int main(){ // freopen("fusion.in","r",stdin); // freopen("fusion.out","w",stdout); scanf("%s",s+1); n=strlen(s+1); to[n][s[n]-'a']=n; Dep(i,n-1,1){ to[i][s[i]-'a']=i; int now=i+1,c=s[i]-'a'; while(1){ int tmp=now,top=0; while(!to[now][c]&&nxt[now]){ q[++top]=now; now=nxt[now]+1; if(trans[now][c]) now=trans[now][c]; } if(!to[now][c]) break; Rep(j,1,top) trans[q[j]][c]=now; now=to[now][c]+1;c++;to[i][c]=now-1; if(c>'z'-'a') break; } nxt[i]=to[i]['z'-'a'+1]; } Rep(i,1,n-1) if(nxt[i]) fa[i][0]=nxt[i]+1; Rep(j,1,21) Rep(i,1,n-1) if(fa[i][j-1]) fa[i][j]=fa[fa[i][j-1]][j-1]; for(int T=rd();T;T--){ int l=read(),r=read(); bool flag=0; Dep(j,21,0){ if(fa[l][j]) if(fa[l][j]<=r) l=fa[l][j]; else if(fa[l][j]-1==r){puts("Yes");flag=1;break;} } if(!flag) puts("No"); } } /* xxyzzwwxzzyz 1 6 12 */
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
#include<map> #include<set> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cctype> #include<cstdlib> #include<cstring> #include<utility> #include<algorithm> #define fi first #define se second #define pb push_back #define mp make_pair #define For(i,x,y) for(int i=((int)x);i<=((int)y);i++) #define Dep(i,y,x) for(int i=((int)y);i>=((int)x);i--) #define Rep(i,x) for (int y,i=head[x];i;i=E[i].nxt) using namespace std; const int N=500015; const int inf=1000000009; typedef double db; typedef long long ll; typedef vector<int> VI; typedef unsigned int uint; typedef pair<int,int> pii; int rd() { char c=getchar(); int t=0,f=1; while (!isdigit(c)) f=(c=='-')?-1:1,c=getchar(); while (isdigit(c)) t=t*10+c-48,c=getchar(); return t*f; } void wt(int x) { if (x<0) putchar('-'),wt(-x); else { if (x>9) wt(x/10); putchar(x%10+48); } } char s[N]; int Len,f[N][27],g[N][21],l,r,Q; int main() { scanf("%s",s+1); Len=strlen(s+1); Dep (i,Len,1) { f[i][s[i]-97]=i+1; For (j,s[i]-97+1,26) f[i][j]=f[f[i][j-1]][j-1]; For (j,0,s[i]-97-1) f[i][j]=f[f[i][ 26]][j]; } Dep (i,Len,1) { g[i][0]=f[i][26]; For (j,1,20) g[i][j]=g[g[i][j-1]][j-1]; } Q=rd(); while (Q--) { l=rd(),r=rd()+1; Dep (i,20,0) if (g[l][i] && g[l][i]<=r) l=g[l][i]; puts(l==r?"Yes":"No"); } return 0; }
CPP
p03793 Mujin Programming Challenge 2017 - Robot and String
You are developing a robot that processes strings. When the robot is given a string t consisting of lowercase English letters, it processes the string by following the procedure below: 1. Let i be the smallest index such that t_i = t_{i + 1}. If such an index does not exist, terminate the procedure. 2. If t_i is `z`, remove t_i and t_{i + 1} from t. Otherwise, let c be the next letter of t_i in the English alphabet, and replace t_i and t_{i + 1} together with c, reducing the length of t by 1. 3. Go back to step 1. For example, when the robot is given the string `axxxxza`, it will be processed as follows: `axxxxza` → `ayxxza` → `ayyza` → `azza` → `aa` → `b`. You are given a string s consisting of lowercase English letters. Answer Q queries. The i-th query is as follows: * Assume that the robot is given a substring of s that runs from the l_i-th character and up to the r_i-th character (inclusive). Will the string be empty after processing? Constraints * 1 ≤ |s| ≤ 5 × 10^5 * s consists of lowercase English letters. * 1 ≤ Q ≤ 10^5 * 1 ≤ l_i ≤ r_i ≤ |s| Input The input is given from Standard Input in the following format: s Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query: `Yes` or `No`. Examples Input axxxxza 2 1 7 2 6 Output No Yes Input aabcdefghijklmnopqrstuvwxyz 1 1 27 Output Yes Input yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 Output Yes Yes Yes Yes No No No No
6
0
import java.io.*; import java.util.*; public class Main { FastScanner in; PrintWriter out; void solve() { char[] s = in.next().toCharArray(); int n = s.length; int[][] dp = new int[27][n]; final int MAX = 20; int[][] go = new int[MAX][n]; for (int i = n - 1; i >= 0; i--) { int cur = s[i] - 'a'; dp[cur][i] = 1; for (int need = cur + 1; need < 27; need++) { int posAsk = i + dp[need - 1][i]; if (posAsk >= n) { dp[need][i] = Integer.MAX_VALUE / 2; } else { int askLen = dp[need - 1][posAsk]; if (askLen >= Integer.MAX_VALUE / 2) { dp[need][i] = Integer.MAX_VALUE / 2; } else { dp[need][i] = dp[need - 1][i] + askLen; } } } int posAsk = i + dp[26][i]; go[0][i] = dp[26][i]; for (int j = 1; j < MAX; j++) { int len = go[j - 1][i]; if (i + len >= n) { go[j][i] = Integer.MAX_VALUE / 2; } else { go[j][i] = go[j - 1][i] + go[j - 1][i + len]; } } for (int j = 0; j < cur; j++) { if (posAsk >= n) { dp[j][i] = Integer.MAX_VALUE / 2; } else { dp[j][i] = dp[26][i] + dp[j][posAsk]; } } } int q = in.nextInt(); for (int i = 0; i < q; i++) { int fr = in.nextInt() - 1; int to = in.nextInt() - 1; int needLen = to - fr + 1; for (int j = MAX - 1; j >= 0; j--) { if (needLen > 0 && go[j][fr] <= needLen) { needLen -= go[j][fr]; fr += go[j][fr]; } } out.println(needLen == 0 ? "Yes" : "No"); } } void run() { try { in = new FastScanner(new File("object.in")); out = new PrintWriter(new File("object.out")); solve(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new Main().runIO(); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int result = 0; int a = sc.nextInt(),b = sc.nextInt(),c = sc.nextInt(); if(a != b) result++; if(a != c) result++; if(b != c) result++; if(a == b && b == c) result++; System.out.println(result); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.*; class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); Set<Integer> hs = new HashSet<>(); hs.add(sc.nextInt()); hs.add(sc.nextInt()); hs.add(sc.nextInt()); System.out.println(hs.size()); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); Set s = new HashSet<Integer>(); s.add(a); s.add(b); s.add(c); System.out.println(s.size()); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
print(len(set(input().rstrip().split())))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
c = list(set(map(int,input().split()))) print(len(c))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashSet<Integer> hs = new HashSet<Integer>(); for (int i= 0;i<3;i++) { hs.add(sc.nextInt());} System.out.println(hs.size()); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
l = raw_input().split() map_ = {} for data in l: map_[l[0]] = 1 map_[l[1]] = 1 map_[l[2]] = 1 print len(map_)
PYTHON
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c; cin >> a >> b >> c; set <int>d{a,b,c}; cout << d.size() ; return 0; }
CPP
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
print(len(set([int(_) for _ in input().split()])))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.*; class Main { public static void main(String args[]) { Scanner inp = new Scanner(System.in); Set<Integer> s = new HashSet<Integer>(); for (int i = 0; i < 3; i++) { s.add(inp.nextInt()); } System.out.println(s.size()); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); hmap.put(sc.nextInt(), 1); hmap.put(sc.nextInt(), 1); hmap.put(sc.nextInt(), 1); System.out.println(hmap.size()); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; set<int>s={a,b,c}; cout<<(int)s.size(); }
CPP
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import re x = raw_input() x = re.split(" ", x) print len(set(x))
PYTHON
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); if(a == b && b == c){ System.out.println(1); } else if(a == b || a == c || b == c){ System.out.println(2); } else{ System.out.println(3); } } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
li = set(map(int,input().split())) print(len(li))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
li=input().split() Ans=list(set(li)) print(len(Ans))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
S = set(input().split()) print(len(S))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
#include <set> #include <iostream> using namespace std; int x; int main() { set<int> s; for(int i = 0; i < 3; i++) cin >> x, s.insert(x); cout << s.size() << endl; return 0; }
CPP
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt(); System.out.println((a==b)?(a==c)?1:2:(a==c)?2:(b==c)?2:3); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; cout<<max(1,3-(a==b)-(b==c)-(a==c))<<endl; return 0; }
CPP
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d=2; cin >>a>>b>>c; if(a==b&&b==c)d=1; if(a!=b&&b!=c&&c!=a)d=3; cout << d<< endl; }
CPP
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int count = 1; if (a != b) count++; if (a != c && b != c) count++; System.out.println(count); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); Set<Integer> set = new HashSet<>(); set.add(a); set.add(b); set.add(c); System.out.println(set.size()); sc.close(); } }
JAVA
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
print(len(list(set(list(input().split())))))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
a = input().split() print(len(list(set(a))))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
A = set(map(int,input().split())) print(len(A))
PYTHON3
p03962 AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive. Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him. Constraints * 1≦a,b,c≦100 Input The input is given from Standard Input in the following format: a b c Output Print the number of different kinds of colors of the paint cans. Examples Input 3 1 4 Output 3 Input 3 3 33 Output 2
6
0
abc = input().split() print(len(set(abc)))
PYTHON3