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
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
def ng():print("NO");exit() n, k = map(int,input().split()) if (n,k)==(1,0):print("YES");print(0);exit() ans = [0] * n;u = 0;popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1;u = 4 for _ in range(k - 1):ans[u + 1] = ans[u + 2] = u+1;u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES");print(*ans)
PYTHON3
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; int s[MAXN]; int isComplete(int n) { return (n & (n + 1)) == 0; } int gen(int n, int niche) { for (int i = 2; i <= niche; i += 2) { s[i] = i / 2; s[i + 1] = i / 2; } int last = 1; for (int i = niche + 1; i <= n; i += 2) { s[i] = i + 1; s[last] = i + 1; last = i + 1; } } int main() { int n, k; cin >> n >> k; if (n % 2 == 0) { cout << "NO" << endl; return 0; } int leaves = (n + 1) / 2; int hi = leaves - 2; int lo = 0; if (hi < 0) hi = 0; if (!isComplete(n)) lo++; if (k < lo || hi < k) { cout << "NO" << endl; return 0; } if (k == 1 && isComplete(n)) { cout << "NO" << endl; return 0; } if (isComplete(n - 2 * k)) { gen(n, n - 2 * k); } else { assert(k > 0); if (!isComplete(n - 2 * (k - 1))) { gen(n, n - 2 * (k - 1)); } else { assert(k > 1); if (k == 2) { cout << "NO" << endl; return 0; } gen(n, n - 2 * (k - 2)); int niche = n - 2 * (k - 2); s[niche - 2] = n - 1; s[niche - 3] = n - 1; } } cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << s[i] << " "; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int ans[100010]; void solve(const int &l, const int &r, const int &k); inline char legal(const int &N, const int &k) { if (N < 0 || k < 0) return false; char flag; int n = N + 1 >> 1; if (n == 1) flag = !k; else if (n == 5) flag = (k == 1 || k == 3); else if ((n & -n) == n) flag = (k <= n - 2 && k != 1); else flag = (k <= n - 2 && k != 0); return flag; } int main() { int n, k; scanf("%d%d", &n, &k); if (!(n & 1)) { puts("NO"); return 0; } char flag = legal(n, k); if (!flag) { puts("NO"); return 0; } puts("YES"); solve(1, n, k); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); putchar('\n'); return 0; } void solve(const int &l, const int &r, const int &k) { if (r == l + 10 && k == 3) { ans[l + 1] = ans[l + 4] = l; solve(l + 1, l + 3, 0); solve(l + 4, l + 10, 2); } else if (r != l) { if (legal(r - l - 1, k - 1)) { ans[l + 1] = ans[l + 2] = l; solve(l + 2, r, k - 1); } else if (legal(r - l - 3, k - 1)) { ans[l + 1] = ans[l + 4] = l, ans[l + 2] = ans[l + 3] = l + 1; solve(l + 4, r, k - 1); } else if (k == 0) { ans[l + 1] = ans[(l + r >> 1) + 1] = l; solve(l + 1, l + r >> 1, k); solve((l + r >> 1) + 1, r, k); } else if (k == 1) { int n = (r - l + 2) >> 1; int t = 1 << 31 - __builtin_clz(n); if (n >= t + (t >> 1)) { ans[l + 1] = ans[l + (t << 1)] = l; solve(l + 1, l + (t << 1) - 1, 0); solve(l + (t << 1), r, n != t + (t >> 1)); } else { ans[l + 1] = ans[l + t] = l; solve(l + 1, l + t - 1, 0); solve(l + t, r, 1); } } } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
import java.io.*; import java.util.*; public class Solution { int bound = 100000; int N = 31; private void solve() throws Exception { memo = new Boolean[N+1][N+1]; nodes = new Node[N+1][N+1]; // for(int i = 1; i <= n; i+=2) { // String a = String.format("%3d", i); // System.out.print(a + ": "); // for(int j = 0; j <= i; j++) { // System.out.print((doit(i,j)?1:0) + " "); // } // System.out.println(); // } // System.out.println(doit_(65,2)); int n = nextInt(); int k = nextInt(); powers = new HashSet<Integer>(); int pow = 1; while(pow-1 <= bound) { powers.add(pow-1); pow = pow*2; } if(n == 1 && k == 0) { out.println("YES"); out.println(0); } else if(n%2 == 1 && k <= (n-3)/2) { if(powers.contains(n) && k == 1) out.println("NO"); else if(!powers.contains(n) && k == 0) out.println("NO"); else if(n == 9 && k == 2) out.println("NO"); else { Node res = doit(n,k); d = new int[n]; p = new int[n]; idx = 0; visit(res, -1); System.out.println("YES"); for(int x : p) out.print((x+1) + " "); } } else out.println("NO"); } int[] d; int[] p; int idx; void visit(Node u, int par){ int cur = idx++; p[cur] = par; if(u.left == null) return; visit(u.left, cur); visit(u.right, cur); } HashSet<Integer> powers; boolean possible = true; Node doit(int n, int k) { if(n <= N && k <= N) { possible = possible && doit_(n,k); return nodes[n][k]; } else if(k <= 1) { int pow = 1; int l = 0; int r = n-1; while(l <= n-1) { l = pow-1; r = n-1-l; if(l >= 0 && r >= 0 && ((l <= r && r < 2*l) || (r <= l && l < 2*r)) ) { break; } else if(r >= 2*l && powers.contains(l) && powers.contains(r)) { Node node = new Node(); node.left = doit(l,0); node.right = doit(r,0); return node; } pow = 2*pow; } Node node = new Node(); node.left = doit(l,0); node.right = doit(r,k); return node; } else if(k == 2 && powers.contains(n-2)) { Node node = new Node(); node.left = doit(3,0); node.right = doit(n-4, k-1); return node; } else { Node node = new Node(); node.left = doit(1,0); node.right = doit(n-2, k-1); return node; } } Boolean[][] memo; Node[][] nodes; boolean doit_(int n, int k) { if(memo[n][k] == null) { if(n == 1 && k == 0) { nodes[n][k] = new Node(); memo[n][k] = true; } else { boolean res = false; for(int l = 1; l <= (n-1)/2 && !res; l++) { int r = n-1-l; int kk = k; if(r >= 2*l) kk--; for(int j = 0; j <= kk && !res; j++) { boolean cur = (doit_(l,j) && doit_(r,kk-j)); res = res || cur; if(res) { nodes[n][k] = new Node(); nodes[n][k].left = nodes[l][j]; nodes[n][k].right = nodes[r][kk-j]; } } } memo[n][k] = res; } } return memo[n][k]; } public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Solution().run(); } }, "1", 1 << 27).start(); } private BufferedReader in; private PrintWriter out; private StringTokenizer tokenizer; public void run() { try { // in = new BufferedReader(new FileReader("ks_10000_0")); in = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; // out = new PrintWriter(new File("outputPQ.txt")); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private float nextFloat() throws IOException { return Float.parseFloat(nextToken()); } private String nextLine() throws IOException { return new String(in.readLine()); } private String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } } class Node { Node left; Node right; }
JAVA
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == '-') nega = -1; ch = getchar(); } int ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } int fa[100005], cnt; bool ok(int n, int k) { if (n < 0) return 0; if (n % 2 == 0) return 0; if (2 * k + 3 > n) return 0; if (k == 0) { for (int i = 1; i <= 18; i++) { if ((1 << i) - 1 == n) return 1; } return 0; } if (k == 1) { if (n <= 4) return 0; for (int i = 1; i <= 18; i++) { if ((1 << i) - 1 == n) return 0; } } if (k == 2) { if (n == 9) return 0; } return 1; } void solve(int n, int k, int f) { if (k == 0) { int R = cnt; fa[++cnt] = f; for (int i = 2; i <= n; i++) { fa[++cnt] = i / 2 + R; } return; } if (k == 1) { for (int i = 1; i <= 18; i++) { int R = (1 << i) - 1; int A = n - R - 1, B = R; if (A > B) swap(A, B); if (A * 2 > B) { if (ok(n - R - 1, 1)) { fa[++cnt] = f; int X = cnt; solve(R, 0, X), solve(n - R - 1, 1, X); return; } } else { if (ok(n - R - 1, 0)) { fa[++cnt] = f; int X = cnt; solve(R, 0, X), solve(n - R - 1, 0, X); return; } } } } if (n == 11 && k == 3) { fa[++cnt] = f; int R = cnt; solve(3, 0, R), solve(7, 2, R); return; } if (2 * k + 3 == n) { fa[++cnt] = f; fa[cnt + 1] = cnt; cnt++; solve(n - 2, k - 1, cnt - 1); return; } if (k == 2) { if (!ok(n - 2, 1)) { fa[++cnt] = f; int R = cnt; solve(3, 0, R), solve(n - 4, 1, R); return; } } fa[++cnt] = f; fa[cnt + 1] = cnt; cnt++; solve(n - 2, k - 1, cnt - 1); } signed main() { int n = read(), k = read(); if (n % 2 == 0) { cout << "NO"; return 0; } if (k == 0) { for (int i = 1; i <= 18; i++) { if ((1 << i) - 1 == n) { cout << "YES\n"; for (int i = 1; i <= n; i++) printf("%d ", i / 2); return 0; } } cout << "NO"; return 0; } if (2 * k + 3 > n) { cout << "NO"; return 0; } if (k == 1) { if (n <= 4) { cout << "NO"; return 0; } for (int i = 1; i <= 18; i++) { if ((1 << i) - 1 == n) { cout << "NO"; return 0; } } } if (k == 2) { if (n == 9) { cout << "NO"; return 0; } } solve(n, k, 0); cout << "YES\n"; for (int i = 1; i <= n; i++) printf("%d ", fa[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 100009; int judge(int n, int k) { if (k < 0) return 0; if (n % 2 == 0) return 0; if (k == 0) { if (__builtin_popcount(n + 1) == 1) return 1; else return 0; } if (k == 1) { if (__builtin_popcount(n + 1) == 1) return 0; else return 1; } if (n == 9 && k == 2) return 0; return 2 * k + 3 <= n; } int nn; int father[maxn]; void dfs(int n, int k, int fa) { int now = ++nn; father[now] = fa; if (n == 1) return; for (int i = 1; (1 << i) < n; ++i) { int lsiz = (1 << i) - 1; int rsiz = n - 1 - lsiz; int nk = k - (max(lsiz, rsiz) >= min(lsiz, rsiz) * 2); if (judge(rsiz, nk)) { dfs(lsiz, 0, now); dfs(rsiz, nk, now); return; } } } int n, k; int main() { cin >> n >> k; if (!judge(n, k)) { printf("NO\n"); return 0; } printf("YES\n"); dfs(n, k, 0); for (int i = 1; i <= n; ++i) printf("%d ", father[i]); printf("\n"); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const long long inf = 1e15 + 7; bool check(long long n) { return __builtin_popcount(n + 1) == 1; } vector<long long> res; bool solve(long long n, long long k, long long par) { if (n == 11 && k == 3) { res[10] = par; res[9] = 10; res[8] = 9; res[7] = 9; return solve(7, 2, 10); } if (k <= 1) { if ((!check(n) && k == 0) || (check(n) && k == 1)) return false; res[0] = par; for (long long i = 1; i < n; i++) res[i] = (i - 1) / 2; return true; } if (k == 2 && check(n - 2)) { res[n - 1] = par; res[n - 2] = n - 1; res[n - 3] = n - 2; res[n - 4] = n - 2; return solve(n - 4, k - 1, n - 1); } res[n - 1] = par; res[n - 2] = n - 1; return solve(n - 2, k - 1, n - 1); } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long n, k; cin >> n >> k; if (n == 1 && k == 0) { cout << "YES" << endl << 0; return 0; } if (n % 2 == 0 || (n - 3) / 2 < k || (k == 0 && !check(n)) || (k == 1 && check(n)) || (k == 2 && n == 9)) { cout << "NO"; return 0; } res.resize(n); bool temp = solve(n, k, -1); if (!temp) cout << "Bad" << endl; cout << "YES" << endl; for (long long i : res) cout << i + 1 << " "; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; inline int lowbit(int x) { return x & -x; } void get_lian(int num) { printf("0 "); for (int i = 1; i <= num; i++) { printf("%d %d ", i * 2 - 1, i * 2 - 1); } } void get_binary(int root, int num) { for (int i = 2; i <= num; i++) { printf("%d ", i / 2 + root - 1); } } int main() { scanf("%d%d", &n, &k); if (n == 1) { printf("YES\n0\n"); return 0; } if (n % 2 == 0) { printf("NO\n"); return 0; } int lim = (n - 3) / 2; if (k > lim) { printf("NO\n"); return 0; } bool flag = (n + 1) - lowbit(n + 1) ? 0 : 1; if (k == 0) { if (!flag) { printf("NO\n"); } else { printf("YES\n"); printf("0 "); get_binary(1, n); } return 0; } if (flag && k == 1) { printf("NO\n"); return 0; } if (n == 9 && k == 2) { printf("NO\n"); return 0; } printf("YES\n"); get_lian(k - 1); n -= (k - 1) * 2; flag = (n + 1) - lowbit(n + 1) ? 0 : 1; if (!flag) { get_binary(k * 2 - 1, n); } else { get_binary(k * 2 - 1, n - 2); printf("2 2"); } printf("\n"); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int cnt; int par[MAXN], br[MAXN]; void solve(int n, int k, int root) { if (n == 0) return; int curr = ++cnt; par[curr] = root; if (k == 0) { solve((n - 1) / 2, 0, curr); solve((n - 1) / 2, 0, curr); } else if (k == 1) { int pot = 1; while (pot <= n) pot *= 2; pot /= 2; if (n - pot >= pot / 2) { solve(pot - 1, 0, curr); solve(n - pot, 1, curr); } else { solve(pot / 2 - 1, 0, curr); solve(n - pot / 2, 1, curr); } } else { if (__builtin_popcount(n - (k - 1) + 1) == 1) { solve(1, 0, curr); solve(n - 2, k - 1, curr); } else { solve(n - 1, k - 1, curr); } } } void add_leaves(int n) { for (int i = 1; i <= n; i++) br[par[i]]++; for (int i = 1; i <= n; i++) { for (int j = 0; j < 2 - br[i]; j++) { par[++cnt] = i; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; if (n == 1) { if (k == 0) cout << "YES\n" << 0; else cout << "NO"; return 0; } if (n % 2 == 0) { cout << "NO"; return 0; } n = (n - 1) / 2; if (n == 3 && k == 1 || n == 4 && k == 2) { cout << "NO"; return 0; } if (__builtin_popcount(n + 1) == 1) { if (k == 1 || k >= n) { cout << "NO"; return 0; } solve(n, k, 0); } else { if (k == 0 || k >= n) { cout << "NO"; return 0; } solve(n, k, 0); } add_leaves(n); cout << "YES\n"; for (int i = 1; i <= 2 * n + 1; i++) cout << par[i] << " "; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, fa[100005]; bool check(int x) { return x - (x & -x) == 0; } int main() { scanf("%d%d", &n, &k); int mx = max((n - 3) / 2, 0); if (k > mx || (n % 2 == 0) || (n == 9 && k == 2) || (check(n + 1) && k == 1) || (!check(n + 1) && k == 0)) { printf("NO"); return 0; } printf("YES\n"); mx = 2 * max(k - 1, 0); for (register int i = 1; i <= mx; i += 2) fa[i] = max(i - 2, 0), fa[i + 1] = i; for (register int i = 1; i <= n - mx; i++) { if (i == 1) fa[i + mx] = max(0, mx - 1); else fa[i + mx] = i / 2 + mx; } if (check(n - mx + 1) && k) fa[n] = fa[n - 1] = 2; for (register int i = 1; i <= n; i++) printf("%d ", fa[i]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def ng(): print("NO") exit() n, k = map(int,input().split()) if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans)
PYTHON3
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
def ng(): print("NO") exit() n, k = map(int,input().split()) if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans)
PYTHON3
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int mx(int u) { if (u < 5) return 0; return (u - 3) / 2; } bool can(int u, int v) { if (u == 9 && v == 2) { return false; } if ((u & 1) == 0) { return false; } if (v == 0) { return __builtin_popcount(u + 1) == 1; } if (v > 1) { return mx(u) >= v; } return __builtin_popcount(u + 1) != 1; } const int N = 1e5 + 10; int ed; int fa[N]; bool can(int L, int R, int tot, int& LSZ) { if (tot < 0) return false; for (int f = min(tot, min(10, mx(L))); f >= 0; f--) { if (!can(L, f)) continue; if (!can(R, tot - f)) continue; LSZ = f; return true; } for (int f = min(tot, min(10, mx(R))); f >= 0; f--) { if (!can(R, f)) continue; if (!can(L, tot - f)) continue; LSZ = tot - f; return true; } return false; } void dfs(int n, int k, int f = 0) { int u = ++ed; fa[u] = f; if (n == 1) { return; } for (int ls = 1, is, tmp; ls < n; ls += 2) { is = (ls > 2 * (n - 1 - ls) || 2 * ls < n - 1 - ls); if (can(ls, n - 1 - ls, k - is, tmp)) { dfs(ls, tmp, u); dfs(n - 1 - ls, k - is - tmp, u); return; } } } int main() { int n, k; cin >> n >> k; if (!can(n, k)) { puts("NO"); return 0; } dfs(n, k); puts("YES"); for (int i = 1; i <= n; i++) { printf("%d ", fa[i]); } puts(""); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int fa[100005]; void js() { puts("NO"); exit(0); } int check(int x) { return x == (x & -x); } int main() { int n, k; scanf("%d%d", &n, &k); if (!(n & 1)) js(); if (k > max(0, (n - 3) / 2)) js(); if (check(n + 1) && k == 1) js(); if (!check(n + 1) && k == 0) js(); if (n == 9 && k == 2) js(); if (k >= 2) { for (int i = 2; i <= 2 * (k - 1); ++i) if (i & 1) fa[i] = i - 2; else fa[i] = i - 1; int t = 2 * (k - 1) - 1; int tot = n - 2 * (k - 1), f = 0; if (check(tot + 1)) tot -= 2, f = 1; for (int i = 2; i <= tot; ++i) fa[i + 2 * (k - 1)] = (i >> 1) + 2 * (k - 1); fa[2 * (k - 1) + 1] = t; if (f) fa[n - 1] = fa[n] = 2; puts("YES"); for (int i = 1; i <= n; ++i) printf("%d ", fa[i]); } else { puts("YES"); for (int i = 1; i <= n; ++i) printf("%d ", i >> 1); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int maxn = 1e5 + 5; const int pow2 = 1 << 20; int fa[maxn], N; int cnt = 1; void dfs(int o, int n, int k) { if (k <= 1) { for (int i = o + 1; i <= n; i++) fa[i] = (i - o + 1) / 2 + o - 1; if (k == 1 && pow2 % (n - o + 2) == 0) { fa[n] = N; fa[n - 1] = N; } return; } else { fa[o + 1] = o; fa[n--] = o; dfs(o + 1, n, k - 1); } } int main() { int n, k; scanf("%d%d", &n, &k); N = n; if (n % 2 == 0) { printf("NO\n"); return 0; } if (n == 1) { if (k == 0) { printf("YES\n"); printf("0\n"); } else printf("NO\n"); return 0; } if (n == 9 && k == 2) { printf("NO\n"); return 0; } if (pow2 % (n + 1) == 0) { if ((k >= 2 || k == 0) && k <= n / 2 - 1) { printf("YES\n"); dfs(1, n, k); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } else printf("NO\n"); return 0; } else { if (k >= 1 && k <= n / 2 - 1) { printf("YES\n"); dfs(1, n, k); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } else printf("NO\n"); return 0; } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using max_heap = priority_queue<T>; const int mod = 998244353; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } struct PairHash { template <typename T1, typename T2> std::size_t operator()(const pair<T1, T2> &p) const { return hash<T1>()(p.first) ^ hash<T2>()(p.second); } }; bool is_power_of_2(int n) { return (n & (n - 1)) == 0; } bool can(int n, int k) { if (n % 2 == 0 || k > max(0, (n - 3) / 2)) { return false; } if (is_power_of_2(n + 1)) { if (k == 1) { return false; } } else { if (k == 0) { return false; } } return !(n == 9 && k == 2); } void solve(int ncase) { int n, k; scanf("%d%d", &n, &k); if (!can(n, k)) { puts("NO"); return; } puts("YES"); vector<int> p(n + 1); int idx = 1; int sub_root = 0; while (k > 1) { if (can(n + 1 - idx - 2, k - 1)) { p[idx] = sub_root; p[idx + 1] = idx; sub_root = idx; idx += 2; } else { p[idx] = sub_root; p[idx + 1] = idx; p[idx + 2] = p[idx + 3] = idx + 1; sub_root = idx; idx += 4; } k--; } int left = n + 1 - idx; if (left == 1) { p[idx] = sub_root; } else if (left == 3) { p[idx] = sub_root; p[idx + 1] = p[idx + 2] = idx; } else { int m = 0; while ((1 << m) < left) m++; m--; int left_sub = (1 << m) - 1; int right_sub = left - 1 - left_sub; if (k == 1 && left_sub > right_sub * 2) { m--; left_sub = (1 << m) - 1; right_sub = left - 1 - left_sub; } (left_sub, right_sub); p[idx] = sub_root; p[idx + 1] = idx; for (int i = 2; i <= left_sub; i++) { p[i + idx] = idx + i / 2; } p[idx + left_sub + 1] = idx; for (int i = 2; idx + left_sub + i <= n; i++) { p[idx + left_sub + i] = idx + left_sub + i / 2; } } for (int i = 1; i <= n; i++) printf("%d%c", p[i], " \n"[i == n]); } void solve_all_cases() { int T = 1; int ncase = 0; while (T--) { solve(++ncase); } } int main() { cout << std::fixed; cout << setprecision(9); solve_all_cases(); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline int msbp(int x) { return 31 - __builtin_clz(x); } inline int msb(int x) { return 1 << msbp(x); } const int INF = 0x3f3f3f3f; const int N = 1e5 + 10; void bad() { cout << "NO" << endl; exit(0); } int ans[N]; inline bool ispow2(int x) { return !(x & (x - 1)); } bool can(int n, int k) { if (n == 1 && k == 0) return true; if (!(n & 1)) return false; if (k >= (n - 1) / 2) return false; if (k == 0 && !ispow2(n + 1)) return false; if (k == 1 && ispow2(n + 1)) return false; if (n == 9 && k == 2) return false; return true; } void rec(int n, int k, int r, int start) { ans[start] = r; if (ispow2(n + 1) && k == 0) { int last = start; for (int i = (1); i <= (n - 1); ++i) { ans[start + i] = last; if (!(i & 1)) ++last; } return; } if (k == 1) { int y = n - 2; for (int x = 1; x <= y; x = x * 2 + 1) { y = n - 1 - x; int add = y >= 2 * x; for (int j = 0; j <= min(k - add, 1); ++j) { if (can(x, j) && can(y, k - add - j)) { rec(x, j, start, start + 1); rec(y, k - add - j, start, start + x + 1); return; } } } } for (int x = 1, y = n - 2; x <= y; x += 2, y -= 2) { int add = y >= 2 * x; for (int j = 0; j <= min(k - add, 1); ++j) { if (can(x, j) && can(y, k - add - j)) { rec(x, j, start, start + 1); rec(y, k - add - j, start, start + x + 1); return; } } } } int n, k; signed main() { ios::sync_with_stdio(false); cin >> n >> k; if (!can(n, k)) bad(); rec(n, k, 0, 1); cout << "YES" << endl; for (int i = (1); i <= (n); ++i) cout << ans[i] << ' '; cout << endl; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EInverseGenealogy solver = new EInverseGenealogy(); solver.solve(1, in, out); out.close(); } static class EInverseGenealogy { private static final String NO = "NO"; private static final String YES = "YES"; private static final int[][] small = { // x = 0 null, // x = 1 {-1}, // x = 2 {-1, 0, 1, 2, 0}, // x = 3 {-1, 0, 0}, // x = 4 {-1, 0, 0, 1, 1}, // x = 5 {-1, 0, 0, 1, 1, 2}, // x = 6 {-1, 0, 0, 1, 1, 3, 3, 4}, // x = 7 {-1, 0, 0, 1, 1, 2, 2}}; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), k = in.nextInt(); if (n % 2 == 0) { out.println(NO); return; } if (n == 1) { if (k == 0) { out.println(YES); out.println(0); } else { out.println(NO); } return; } int balanced = n - k; int leaves = n / 2 + 1; int x = balanced - leaves; // number of balanced internal nodes n -= leaves; // number of internal nodes if (x <= 0 || x > n) { out.println(NO); return; } Util.ASSERT(x > 0 && n > 0); int[] answer = solve(x, n); if (answer == null) { out.println(NO); return; } int[] degree = new int[answer.length]; for (int i = 0; i < answer.length; i++) { if (answer[i] >= 0) degree[answer[i]]++; } out.println(YES); for (int i = 0; i < answer.length; i++) { answer[i]++; } out.print(Util.join(answer) + " "); for (int i = 0; i < answer.length; i++) { for (int j = degree[i]; j < 2; j++) { out.print((i + 1) + " "); } } } private int[] solve(int x, int n) { // // System.out.println("x = " + x); // // System.out.println("n = " + n); int[] ans = f(x); if (ans == null || ans.length > n) return null; int[] big = new int[n]; System.arraycopy(ans, 0, big, big.length - ans.length, ans.length); for (int i = 0; i < big.length - ans.length + 1; i++) { big[i] = i - 1; } for (int i = big.length - ans.length + 1; i < big.length; i++) { big[i] += big.length - ans.length; } // // System.out.println("ans = " + Arrays.toString(ans)); // // System.out.println("big = " + Arrays.toString(big)); return big; } private int[] f(int x) { if (x < small.length) { // System.out.println("x = " + x); // System.out.println("length = " + small[x].length); // System.out.println(); return small[x]; } if (Integer.bitCount(x + 1) == 1) { // System.out.println("x = " + x); // System.out.println("length = " + x); // System.out.println(); return powerOfTwoMinus1(x); } int powerOfTwoMinus1 = getPowerOfTwoMinus1(x); int remaining = x - powerOfTwoMinus1 - 1; if (remaining + 1 != powerOfTwoMinus1 && sz(remaining + 1) == remaining + 1) { // System.out.println("x = " + x); // System.out.println("powerOfTwoMinus1 = " + powerOfTwoMinus1); // System.out.println("powerOfTwoMinus1 = " + (remaining + 1)); return merge(f(powerOfTwoMinus1), f(remaining + 1)); } if (sz(remaining) == remaining) { return merge(f(powerOfTwoMinus1), solve(remaining, remaining + 1)); } int[] left = f(powerOfTwoMinus1); int[] right = f(x - powerOfTwoMinus1 - 1); int[] ans = merge(left, right); // System.out.println("x = " + x); // System.out.println("powerOfTwoMinus1 = " + powerOfTwoMinus1); // System.out.println("remaining = " + remaining); // System.out.println("sz(remaining) = " + sz(remaining) + " actual: " + right.length); // System.out.println("length = " + ans.length); // System.out.println("sz(x) = " + sz(x)); // System.out.println(); return ans; } private int[] merge(int[] left, int[] right) { int[] answer = new int[1 + left.length + right.length]; answer[0] = -1; System.arraycopy(left, 0, answer, 1, left.length); answer[1] = 0; System.arraycopy(right, 0, answer, 1 + left.length, right.length); answer[1 + left.length] = 0; for (int i = 1 + 1; i < 1 + left.length; i++) { answer[i] += 1; } for (int i = 1 + left.length + 1; i < answer.length; i++) { answer[i] += 1 + left.length; } return answer; } private int getPowerOfTwoMinus1(int x) { int powerOfTwoMinus1 = 3; while (true) { if (x <= powerOfTwoMinus1 * 3) { return powerOfTwoMinus1; } powerOfTwoMinus1 = 2 * (powerOfTwoMinus1 + 1) - 1; } } private int sz(int x) { if (x < small.length) { return small[x].length; } if (Integer.bitCount(x + 1) == 1) return x; if (Integer.bitCount(x + 2) == 1) return x + 2; return x + 1; } private int[] powerOfTwoMinus1(int x) { int[] ans = new int[x]; for (int i = 0; i < x; i++) { ans[i] = (i - 1) / 2; } return ans; } } static class Util { public static String join(int... i) { StringBuilder sb = new StringBuilder(); for (int o : i) { sb.append(o); sb.append(" "); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } public static void ASSERT(boolean assertion) { if (!assertion) throw new AssertionError(); } private Util() { } } static class InputReader { public final BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int bufSize = 1e6; inline char nc() { static char buf[bufSize], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2) ? EOF : *p1++; } template <typename T> inline T read(T &r) { static char c; r = 0; for (c = nc(); !isdigit(c); c = nc()) ; for (; isdigit(c); c = nc()) r = r * 10 + c - 48; return r; } const int maxn = 1e5 + 100; int N, K, cnt; inline bool check(int n, int k) { if (n % 2 == 0) return false; if (k == 0) return ((n & (n + 1)) == 0); if (k == 1) return (n & (n + 1)); if (n == 9 && k == 2) return false; return k > 0 && k * 2 + 3 <= n; } void dfs(int n, int k, int fa) { printf("%d ", fa); int now = ++cnt; if (n == 1) return; for (int l = 1; l < n; l = l * 2 + 1) { int r = n - l - 1; int more = k - (std::max(l, r) >= std::min(l, r) * 2); if (check(l, 0) && check(r, more)) { dfs(l, 0, now), dfs(r, more, now); return; } } } int main() { read(N), read(K); if (!check(N, K)) puts("NO"); else puts("YES"), dfs(N, K, 0); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; inline int lowbit(int x) { return x & (-x); } bool check() { if (n % 2 == 0) return 0; if (max((n - 3) / 2, 0) < k) return 0; if (n == 9 && k == 2) return 0; if (n + 1 == lowbit(n + 1) && k == 1) return 0; if (n + 1 != lowbit(n + 1) && k == 0) return 0; return 1; } int fa[100000 + 5]; int main() { scanf("%d %d", &n, &k); if (check()) { puts("YES"); int tk = 2 * max(0, k - 1); for (int i = 1; i < tk; i += 2) { fa[i] = max(0, i - 2); fa[i + 1] = i; } fa[tk + 1] = max(0, tk - 1); for (int i = tk + 2; i <= n; i++) fa[i] = ((i - tk) / 2 + tk); if (n - tk + 1 == lowbit(n - tk + 1) && k != 0) fa[n] = fa[n - 1] = 2; for (int i = 1; i <= n; i++) printf("%d ", fa[i]); } else puts("NO"); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int ans[maxn]; int lowbit(int x) { return x & -x; } bool is_two(int x) { return x == lowbit(x); } bool dfs(int n, int k, int id) { if (k == 1) { printf("%d %d %d\n", n, k, id); if (is_two(n)) { return false; } ans[id] = max(id - 2, 0); for (int i = 2; i <= n; ++i) { ans[id + i - 1] = id + i / 2 - 1; } return true; } else if (k == 0) { if (is_two(n)) { ans[1] = 0; for (int i = 2; i <= n; ++i) { ans[i] = i / 2; } return true; } else return false; } else { while (k >= 2) { ans[id] = max(id - 2, 0); ans[id + 1] = id; n -= 2; k -= 1; id += 2; } return dfs(n, k, id); } } int main() { int n, k; scanf("%d%d", &n, &k); int lim = max(0, (n - 3) / 2); if (n % 2 == 0 || k > lim || (n == 9 && k == 2) || (!is_two(n + 1) && k == 0) || (is_two(n + 1) && k == 1)) { printf("NO\n"); } else { int id = 1; while (k >= 2) { ans[id] = max(id - 2, 0); ans[id + 1] = id; n -= 2; k -= 1; id += 2; } ans[id] = max(id - 2, 0); for (int i = 2; i <= n - 2; ++i) { ans[id + i - 1] = id + i / 2 - 1; } if (is_two(n + 1)) { if (k == 0) ans[id + n - 2] = id + (n - 1) / 2 - 1, ans[id + n - 1] = id + n / 2 - 1; else ans[id + n - 2] = 2, ans[id + n - 1] = 2; } else { if (k == 0) return printf("NO\n"), 0; else ans[id + n - 2] = id + (n - 1) / 2 - 1, ans[id + n - 1] = id + n / 2 - 1; } printf("YES\n"); for (int i = 1; i < id + n - 1; ++i) { printf("%d ", ans[i]); } printf("%d\n", ans[id + n - 1]); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; bool isbalanced(int x, int y) { return (2 * x > y && 2 * y > x); } bool ispow(int x) { return (__builtin_popcount(x + 1) == 1); } int getdep(int x) { return __builtin_ctz(x + 1); } int n, k, cnt; int ans[MAXN]; void build(int fa, int dep) { if (dep == 0) return; ans[++cnt] = fa; build(cnt, dep - 1); ans[++cnt] = fa; build(cnt, dep - 1); } void fail() { puts("NO"); exit(0); } void dfs(int n, int k, int rt) { if (!n) return; if (n < 13) { if (n == 1) { if (k == 0) return; else fail(); } else if (n == 3) { if (k == 0) { ans[++cnt] = rt; dfs(1, 0, cnt); ans[++cnt] = rt; dfs(1, 0, cnt); } else fail(); } else if (n == 5) { if (k == 1) { ans[++cnt] = rt; dfs(1, 0, cnt); ans[++cnt] = rt; dfs(3, 0, cnt); } else fail(); } else if (n == 7) { if (k == 0) { build(rt, getdep(n) - 1); } else if (k == 2) { ans[++cnt] = rt; dfs(1, 0, cnt); ans[++cnt] = rt; dfs(5, 1, cnt); } else fail(); } else if (n == 9) { if (k == 1) { ans[++cnt] = rt; dfs(1, 0, cnt); ans[++cnt] = rt; dfs(7, 0, cnt); } else if (k == 3) { ans[++cnt] = rt; dfs(1, 0, cnt); ans[++cnt] = rt; dfs(7, k - 1, cnt); } else fail(); } else if (n == 11) { if (k == 1) { ans[++cnt] = rt; dfs(3, 0, cnt); ans[++cnt] = rt; dfs(7, 0, cnt); } else if (k == 2) { ans[++cnt] = rt; dfs(5, 1, cnt); ans[++cnt] = rt; dfs(5, 1, cnt); } else if (k == 3) { ans[++cnt] = rt; dfs(3, 0, cnt); ans[++cnt] = rt; dfs(7, 2, cnt); } else if (k == 4) { ans[++cnt] = rt; dfs(1, 0, cnt); ans[++cnt] = rt; dfs(9, 3, cnt); } else fail(); } } else { if (k == 0) { if (!ispow(n)) fail(); build(rt, getdep(n) - 1); } else if (k == 1) { for (int y = (1 << (31 - __builtin_clz(n))); y >= 2; y /= 2) { if (isbalanced(n - y, y - 1)) { ans[++cnt] = rt; build(cnt, getdep(y - 1) - 1); ans[++cnt] = rt; rt = cnt; dfs(n - y, k, rt); return; } else if (ispow(n - y) && ispow(y - 1)) { ans[++cnt] = rt; build(cnt, getdep(n - y) - 1); ans[++cnt] = rt; build(cnt, getdep(y - 1) - 1); return; } } } else { bool ok = 0; for (int l = 1; l <= n; l = l * 2 + 1) { if (!isbalanced(l, n - l - 1) && ispow(l) && (!ispow(n - l - 1) || k != 2)) { ans[++cnt] = rt; dfs(l, 0, cnt); ans[++cnt] = rt; dfs(n - l - 1, k - 1, cnt); ok = 1; break; } } assert(ok); } } } int main() { scanf("%d %d", &n, &k); if ((n + 1) & 1) fail(); if (k > max(0, (n - 3) / 2)) fail(); ans[++cnt] = 0; dfs(n, k, 1); puts("YES"); for (int i = 1; i <= cnt; i++) printf("%d%c", ans[i], " \n"[i == n]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int cur; bool possible(int n, int k) { if (n % 2 == 0) return false; if ((n & (n + 1)) == 0) { if (k == 0) return true; else if (k == 1) return false; } if (n == 9 && k == 2) return false; return k > 0 && k < n / 2; } void dfs(int n, int k, int p) { cout << p << " "; int u = ++cur; if (n == 1) return; for (int l = 1; l < n; l = l * 2 + 1) { int r = n - l - 1; int kr = k - (max(l, r) > min(l, r) * 2); if (possible(l, 0) && possible(r, kr)) { dfs(l, 0, u); dfs(r, kr, u); return; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (!possible(n, k)) { cout << "NO\n"; exit(0); } cout << "YES\n"; dfs(n, k, 0); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int memo[105][6]; pair<int, int> next1[105][6]; pair<int, int> next2[105][6]; bool boleh(int n, int k); int dp(int n, int k) { if (n % 2 == 0) { return false; } if (k < 0) { return false; } if (n == 1) { return k == 0; } if (memo[n][k] != -1) { return memo[n][k]; } bool ans = false; for (int i = 1; i <= n - 2; i += 2) { bool subtract1 = false; if (n - i - 1 >= 2 * i || i >= 2 * (n - i - 1)) { subtract1 = true; } for (int j = 0; j <= k - subtract1; j++) { if (boleh(i, j) && boleh(n - i - 1, k - subtract1 - j)) { next1[n][k] = pair<int, int>(i, j); next2[n][k] = pair<int, int>(n - i - 1, k - subtract1 - j); ans = true; break; } } if (ans) { break; } } return memo[n][k] = ans; } int cnt = 2; int p[1000005]; void rec(int n, int k, int p2) { if (n == 1) { return; } int temp = cnt; p[temp] = p2; p[temp + 1] = p2; cnt += 2; pair<int, int> val1 = next1[n][k]; pair<int, int> val2 = next2[n][k]; rec(val1.first, val1.second, temp); rec(val2.first, val2.second, temp + 1); } bool isPowerOf2(int x) { return (x & (x - 1)) == 0; } void rec2(int n, int k, int p2) { if (k <= 3 && n <= 100) { rec(n, k, p2); return; } int temp = cnt; p[temp] = p2; p[temp + 1] = p2; cnt += 2; pair<int, int> val1, val2; if (k == 0) { val1 = pair<int, int>((n - 1) / 2, 0); val2 = pair<int, int>((n - 1) / 2, 0); } else if (k > 1) { if (k == 2 && isPowerOf2(n - 1)) { val1 = pair<int, int>(3, 0); val2 = pair<int, int>(n - 4, k - 1); } else { val1 = pair<int, int>(1, 0); val2 = pair<int, int>(n - 2, k - 1); } } else if ((n + 1) % 3 == 0 && isPowerOf2((n + 1) / 3)) { val1 = pair<int, int>(n / 3, 0); val2 = pair<int, int>(2 * n / 3 + 1, 0); } else { int size2 = (1 << (int)log2(n)) - 1; int size1 = n - 1 - size2; if (size2 >= 2 * size1) { size2 /= 2; size1 = n - 1 - size2; } val1 = pair<int, int>(size1, 1); val2 = pair<int, int>(size2, 0); } rec2(val1.first, val1.second, temp); rec2(val2.first, val2.second, temp + 1); } bool boleh(int n, int k) { if (n == 1) { return k == 0; } else if (n % 2 == 0) { return false; } else if (k > (n - 3) / 2) { return false; } else { if (n == 9 && k == 2) { return false; } if (isPowerOf2(n + 1) && k == 1) { return false; } else if (!isPowerOf2(n + 1) && k == 0) { return false; } return true; } } int main() { int n, k; scanf("%d%d", &n, &k); memset(memo, -1, sizeof(memo)); memset(p, 0, sizeof(p)); int temp = boleh(n, k); if (temp) { for (int i = 1; i <= min(n, 100); i += 2) { for (int j = 0; j <= 3; j++) { dp(i, j); } } printf("YES\n"); cnt = 2; memset(p, 0, sizeof(p)); rec2(n, k, 1); for (int i = 1; i <= n; i++) { printf("%d ", p[i]); } } else { printf("NO\n"); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int pow2 = 1 << 20; int fa[maxn], N; int cnt = 1; void dfs(int o, int n, int k) { if (k <= 1) { for (int i = o + 1; i <= n; i++) fa[i] = (i - o + 1) / 2 + o - 1; if (k == 1 && pow2 % (n - o + 2) == 0) { fa[n] = N; fa[n - 1] = N; } return; } else { fa[o + 1] = o; fa[n--] = o; dfs(o + 1, n, k - 1); } } int fakemain() { int n, k; scanf("%d%d", &n, &k); N = n; if (n % 2 == 0) { printf("NO\n"); return 0; } if (n == 1) { if (k == 0) printf("YES\n0\n"); else printf("NO\n"); return 0; } if (n == 9 && k == 2) { printf("NO\n"); return 0; } if (pow2 % (n + 1) == 0) { if ((k >= 2 || k == 0) && k <= n / 2 - 1) { printf("YES\n"); dfs(1, n, k); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } else printf("NO\n"); return 0; } else { if (k >= 1 && k <= n / 2 - 1) { printf("YES\n"); dfs(1, n, k); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } else printf("NO\n"); return 0; } return 0; } int main() { return fakemain(); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> namespace ztd { using namespace std; template <typename T> inline T read(T& t) { t = 0; short f = 1; char ch = getchar(); double d = 0.1; while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar(); if (ch == '.') { ch = getchar(); while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar(); } t *= f; return t; } const int mod = 998244353; inline long long ksm(long long x, long long y) { long long ret = 1; while (y) { if (y & 1) ret = ret * x % mod; y >>= 1; x = x * x % mod; } return ret; } inline long long inv(long long x) { return ksm(x, mod - 2); } } // namespace ztd using namespace ztd; const int maxn = 1e5 + 7; int n, k; inline int lowbit(int x) { return x & (-x); } inline bool ermi(int x) { return (x)-lowbit(x) == 0; } int fa[maxn]; signed main() { read(n); read(k); if (n % 2 == 0) { puts("NO"); return 0; } if (k > max((n - 3) / 2, 0)) { puts("NO"); return 0; } if (!ermi(n + 1) && k == 0) { puts("NO"); return 0; } if (ermi(n + 1) && k == 1) { puts("NO"); return 0; } if (n == 9 && k == 2) { puts("NO"); return 0; } puts("YES"); int base = max(2 * k - 2, 0); for (int i = 1; i < base; i += 2) fa[i + 1] = i, fa[i] = max(0, i - 2); for (int i = 1; i <= n - base; ++i) { fa[i + base] = max(i / 2 + base, 0); } fa[base + 1] = max(base - 1, 0); if (ermi(n - base + 1) && k) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; ++i) cout << fa[i] << ' '; cout << '\n'; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; bool can(long long n, long long k) { if (n % 2 == 0) return 0; if (k == 0) return !(n & (n + 1)); if (k == 1) return n & (n + 1); return (k > 0 && k * 2 + 3 <= n) && !(k == 2 && n == 9); } long long i = 1; void construct(long long n, long long k, long long a = 0) { cout << a << " "; long long j = i++; if (n <= 1) return; for (long long l = 1; l < n; l++) { long long r = n - l - 1; long long p = max(l, r) >= (min(l, r) * 2) ? k - 1 : k; if (can(l, 0) && can(r, p)) { construct(l, 0, j); construct(r, p, j); return; } } } int main() { long long n, k; cin >> n >> k; if (!can(n, k)) cout << "NO\n"; else { cout << "YES\n"; construct(n, k); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, w; void v(int a) { cout << a << " "; } void t(int a, int b) { for (int i = a + 1; i <= b; i++) v(a + (i - a - 1) / 2); } void s(int db, int p, int f) { v(f); if (db < 2) t(p, n); else { if (db == 3 && n - p == 10) t(p, p + 6), t(p + 6, p + 8), t(p + 8, p + 10); else if (db == 2 && ((n - p) & (n - p - 1)) == 0) v(p), t(p + 1, p + 3), v(p), t(p + 4, n); else v(p), s(db - 1, p + 2, p); } } int main() { cin >> n >> k, w = n & (n + 1); if (n % 2 == 0 || 2 * k > max(0, n - 3) || (w && !k) || (!w && k == 1) || n == 9 && k == 2) { cout << "NO\n"; return 0; } cout << "YES\n"; s(k, 1, 0); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <class T> inline void cmin(T &a, T b) { ((a > b) && (a = b)); } template <class T> inline void cmax(T &a, T b) { ((a < b) && (a = b)); } char IO; template <class T = int> T rd() { T s = 0; int f = 0; while (!isdigit(IO = getchar())) f |= IO == '-'; do s = (s << 1) + (s << 3) + (IO ^ '0'); while (isdigit(IO = getchar())); return f ? -s : s; } const int N = 1e5 + 10; int n, m; int fa[N]; int chk(int a, int b) { if (a > b) swap(a, b); return a * 2 <= b; } void Out() { puts("YES"); for (int i = 1, iend = n; i <= iend; ++i) printf("%d ", fa[i]); exit(0); } int Get(int l, int r) { for (int i = l + 1, iend = r; i <= iend; ++i) fa[i] = l - 1 + (i - l + 1) / 2; return l; } int Mincost(int x) { if (~x & 1) return 1e9; for (int i = 0, iend = 17; i <= iend; ++i) if (x + 1 == (1 << i)) return 0; return 1; } int main() { n = rd(), m = rd(); if (Mincost(n) == m) Get(1, n), Out(); if (m == 0) puts("NO"), exit(0); if (~n & 1) puts("NO"), exit(0); if ((m + 1) * 2 >= n) puts("NO"), exit(0); int r = (m + 1) * 2 + 1; if (r == n) { for (int i = 1, iend = m + 1; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; Out(); } r -= 2; int c = n - r + 2; if (m > 1) for (int x = 1, xend = c - 1; x <= xend; ++x) if (Mincost(x) + Mincost(c - x) - !chk(c - x, n - 1 - (c - x)) + (x >= 3) == 1) { for (int i = 1, iend = m; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; int t = max(0, r - 2); fa[Get(r - 1, r + x - 2)] = t; fa[2] = t; fa[Get(r + x - 1, n)] = 1; Out(); } puts("NO"), exit(0); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int ans[100011]; int n, k; inline int ok(int n, int k) { if (n % 2 == 0) return 0; if (n == 1 and k == 0) return 1; if (k > n / 2 - 1) return 0; if (n == 9 and k == 2) return 0; if (__builtin_popcount(n + 1) == 1) { if (k == 1) return 0; return 1; } return k != 0; } void solve(int st, int ed, int k) { int n = ed - st; if (ed == st + 1) { assert(k == 0); return; } for (int i = st + 1; i < ed; i += 2) { int pj = i - st; int pk = n - pj - 1; int nk = k; if (pj * 2 <= pk or pk * 2 <= pj) { nk--; } if (nk < 0) continue; for (int j = 0; j <= max(0, pj / 2 - 1) and j <= nk; j++) { if (ok(pj, j) and ok(pk, nk - j)) { ans[st + 1] = st; ans[i + 1] = st; solve(st + 1, i + 1, j); solve(i + 1, ed, nk - j); return; } } } assert(0); } inline int solve() { if (ok(n, k) == 0) return 0; solve(1, n + 1, k); return 1; } int main() { cin >> n >> k; if (solve() == 0) return puts("NO"); puts("YES"); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int ans[100011]; int n, k; inline int ok(int n, int k) { if (n % 2 == 0) return 0; if (n == 1 and k == 0) return 1; if (k > n / 2 - 1) return 0; if (n == 9 and k == 2) return 0; if (__builtin_popcount(n + 1) == 1) { if (k == 1) return 0; return 1; } return k != 0; } void solve(int st, int ed, int k) { int n = ed - st; if (ed == st + 1) { assert(k == 0); return; } for (int i = st + 1; i < ed; i += 2) { int pj = i - st; int pk = n - pj - 1; int nk = k; if (pj * 2 <= pk or pk * 2 <= pj) { nk--; } if (nk < 0) continue; for (int j = 0; j <= max(0, pj / 2 - 1) and j <= nk; j++) { if (ok(pj, j) and ok(pk, nk - j)) { ans[st + 1] = st; ans[i + 1] = st; solve(st + 1, i + 1, j); solve(i + 1, ed, nk - j); return; } } } assert(0); } inline int solve() { if (ok(n, k) == 0) return 0; solve(1, n + 1, k); return 1; } int main() { cin >> n >> k; if (solve() == 0) return puts("NO"); puts("YES"); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; bool ok(int n, int k) { if (n % 2 == 0) return false; if (n == 1) return k == 0; bool pw2 = ((n + 1) & n) == 0; if (pw2 && k == 1) return false; if (!pw2 && k == 0) return false; if (k > (n - 3) / 2) return false; if (n == 9 && k == 2) return false; return true; }; int main() { int n, k; cin >> n >> k; if (!ok(n, k)) { cout << "NO\n"; return 0; } cout << "YES\n"; int cnt = 0; auto range = [&](int n) { vector<pair<int, int>> a; bool pw2 = ((n + 1) & n) == 0; if (n == 9) { a.emplace_back(1, 1); a.emplace_back(3, 3); } else if (pw2) { a.emplace_back(0, 0); if (n > 3) a.emplace_back(2, (n - 3) / 2); } else { a.emplace_back(1, (n - 3) / 2); } return a; }; auto get = [&](int n, int l, int k) { int r = n - l - 1; int g = (2 * l <= r); k -= g; auto a = range(l); auto b = range(r); int x = -1, y = -1; for (auto [l1, r1] : a) { for (auto [l2, r2] : b) { if (l1 + l2 <= k && k <= r1 + r2) { if (k <= r1 + l2) { y = l2; x = k - y; } else { x = r1; y = k - x; } } } } return make_pair(x, y); }; int res = 0; function<void(int, int, int)> build = [&](int n, int k, int p) { cout << p << " "; int x = ++cnt; if (n == 1) return; for (int l = 1;; l += 2) { auto [a, b] = get(n, l, k); if (a == -1) continue; assert(l <= n - l - 1); res += (2 * l <= n - l - 1); build(l, a, x); build(n - l - 1, b, x); return; } }; build(n, k, 0); assert(res == k); cout << "\n"; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const long long inf = 1e15 + 7; bool check(long long n) { return __builtin_popcount(n + 1) == 1; } vector<long long> res; bool solve(long long n, long long k, long long par) { if (n == 11 && k == 3) { res[10] = par; res[9] = 10; res[8] = 9; res[7] = 9; return solve(7, 2, 10); } if (k <= 1) { if ((!check(n) && k == 0) || (check(n) && k == 1)) return false; res[0] = par; for (long long i = 1; i < n; i++) res[i] = (i - 1) / 2; return true; } if (k == 2 && check(n - 2)) { res[n - 1] = par; res[n - 2] = n - 1; res[n - 3] = n - 2; res[n - 4] = n - 2; return solve(n - 4, k - 1, n - 1); } res[n - 1] = par; res[n - 2] = n - 1; return solve(n - 2, k - 1, n - 1); } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long n, k; cin >> n >> k; if (n == 1 && k == 0) { cout << "YES" << endl << 0; return 0; } if (n % 2 == 0 || (n - 3) / 2 < k || (k == 0 && !check(n)) || (k == 1 && check(n)) || (k == 2 && n == 9)) { cout << "NO"; return 0; } res.resize(n); bool temp = solve(n, k, -1); if (!temp) cout << "Bad" << endl; cout << "YES" << endl; for (long long i : res) cout << i + 1 << " "; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const long long INF = (long long)5e18; const int MOD = 998244353; int _abs(int x) { return x < 0 ? -x : x; } int add(int x, int y) { x += y; return x >= MOD ? x - MOD : x; } int sub(int x, int y) { x -= y; return x < 0 ? x + MOD : x; } void Add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void Sub(int &x, int y) { x -= y; if (x < 0) x += MOD; } void Mul(int &x, int y) { x = (long long)(x) * (y) % MOD; } int qpow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = (long long)(ret) * (x) % MOD; x = (long long)(x) * (x) % MOD; y >>= 1; } return ret; } void checkmin(int &x, int y) { if (x > y) x = y; } void checkmax(int &x, int y) { if (x < y) x = y; } void checkmin(long long &x, long long y) { if (x > y) x = y; } void checkmax(long long &x, long long y) { if (x < y) x = y; } inline int read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } const int N = 100005; int n, k, bl[N], fa[N], mx[N], pos[N], top = 0; int vis[25][25]; bool judge(int n, int k) { if (n % 2 == 0 || k < 0) return 0; if (n <= 10 && !vis[n][k]) return 0; if (k > mx[n]) return 0; if (bl[n] && k == 1) return 0; if (!bl[n] && k == 0) return 0; return 1; } void solve(int l, int r, int F, int k) { int sz = r - l + 1; fa[l] = F; if (sz == 1) return; if (sz == 3) { solve(l + 1, l + 1, l, 0); solve(r, r, l, 0); return; } if (sz == 5) { solve(l + 1, l + 1, l, 0); solve(l + 2, r, l, 1); return; } if (sz == 7) { if (k == 0) { solve(l + 1, l + 3, l, 0); solve(l + 4, r, l, 0); return; } else { solve(l + 1, l + 1, l, 0); solve(l + 2, r, l, 1); return; } } if (sz == 9) { if (k == 1) { solve(l + 1, l + 1, l, 0); solve(l + 2, r, l, 0); return; } if (k == 3) { solve(l + 1, l + 1, l, 0); solve(l + 2, r, l, 2); return; } } if (k == mx[sz]) { solve(l + 1, l + 1, l, 0); solve(l + 2, r, l, k - 1); return; } for (int x = 1; x <= 19; x += 2) { for (int i = 0; i <= min(8, k); i++) { int y = sz - x - 1; int bl = (x * 2 <= y || y * 2 <= x); if (vis[x][i] && judge(y, k - i - bl)) { solve(l + 1, l + x, l, i); solve(l + x + 1, r, l, k - i - bl); return; } } } for (int t = 1; t <= top; t++) { int x = pos[t], y = sz - x - 1; int bl = (x * 2 <= y || y * 2 <= x); if (y < 0) break; for (int i = min(k, mx[x]); i >= 0; i--) { if (judge(x, i) && judge(y, k - i - bl)) { solve(l + 1, l + x, l, i); solve(l + x + 1, r, l, k - i - bl); return; } } } } int main() { n = read(); k = read(); vis[1][0] = 1; vis[3][0] = 1; vis[5][1] = 1; vis[7][0] = 1; vis[7][2] = 1; vis[9][1] = 1; vis[9][3] = 1; mx[1] = 1; mx[3] = 1; mx[5] = 1; mx[7] = 2; mx[9] = 3; for (int i = 1; i <= n; i = i * 2 + 1) bl[i] = 1, pos[++top] = i; for (int i = 11; i <= n; i += 2) mx[i] = (i - 3) / 2; for (int i = 11; i <= 20; i += 2) { for (int j = 0; j <= i; j++) vis[i][j] = judge(i, j); } if (!judge(n, k)) { puts("NO"); return 0; } puts("YES"); solve(1, n, 0, k); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); puts(""); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; int ans[MX]; bool isPower(int N) { bool isPow = false; int cur = 1; while (cur < MX) { cur *= 2; if (cur - 1 == N) isPow = true; } return isPow; } void solve(int N, int p) { if (N == 1) return; int cur = 1; int A, B; A = -1; B = -1; int best = 2; while (cur * 2 < N) { cur *= 2; int val = 0; int X = cur - 1; int Y = N - 1 - X; if (max(X, Y) >= 2 * min(X, Y)) val++; if (!isPower(Y)) val++; if (ckmin(best, val)) { A = X; B = Y; } } assert(A != -1); ans[p + 1] = p + 1; solve(A, p + 1); ans[p + A + 1] = p + 1; solve(B, p + A + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N, K; cin >> N >> K; if (N % 2 == 0) { cout << "NO" << nl; return 0; } if (N == 0 && K == 0) { cout << "YES\n0\n" << nl; return 0; } else if (N == 0 && K == 1) { cout << "NO" << nl; return 0; } if (K > max((N - 3) / 2, 0)) { cout << "NO" << nl; return 0; } bool isPow = isPower(N); if (K == 0 && !isPow) { cout << "NO" << nl; return 0; } if (K == 1 && isPow) { cout << "NO" << nl; return 0; } if (N == 9 && K == 2) { cout << "NO" << nl; return 0; } int M = N; ans[0] = 0; int R = 0; int oK = K; while (K > 1) { ans[R] = M; ans[M - 2] = M; ans[M - 1] = 0; R = M - 1; K--; M -= 2; } if (isPower(M) && K == 1) { ans[0] = 0; R = 0; M = N; K = oK; while (K > 2) { ans[R] = M; ans[M - 2] = M; ans[M - 1] = 0; R = M - 1; K--; M -= 2; } ans[R] = M; ans[M - 1] = 0; ans[M - 2] = M; ans[M - 3] = M - 1; ans[M - 4] = M - 1; K--; M -= 4; } solve(M, 0); cout << "YES" << nl; for (int i = 0; i < (N); i++) { cout << ans[i] << " "; } cout << nl; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≀ s_i ≀ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int nax = 100; int mem[nax][nax]; int ok(int n, int k) { if (n % 2 == 0 || n <= 0 || k < 0) return 0; if (n == 1) return (k == 0); int& memo = mem[n][k]; if (!memo) { int r = 0; for (int na = 1; na < n - 1; na = na * 2 + 1) { int ka = 0; int nb = n - 1 - na; int got = (na >= nb * 2 || nb >= na * 2); int kb = k - ka - got; r |= (ok(na, ka) && ok(nb, kb)); } memo = r + 1; } return memo - 1; } int fastOK(int n, int k) { int balance = !(n & (n + 1)); return (n > 0 && k >= 0 && n % 2 && (2 * k + 3 <= n || k == 0) && (k > 1 || k != balance) && (n != 9 || k != 2)); } int id; void rec(int n, int k, int par = 0) { assert(fastOK(n, k)); cout << par << ' '; int myid = ++id; if (n == 1) return; for (int na = 1; na < n - 1; na = na * 2 + 1) { int nb = n - 1 - na; int kb = k - (na >= nb * 2 || nb >= na * 2); if (fastOK(na, 0) && fastOK(nb, kb)) { rec(na, 0, myid); rec(nb, kb, myid); return; } } assert(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (!fastOK(n, k)) { cout << "NO" << endl; return 0; } cout << "YES" << endl; rec(n, k); cout << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; typedef struct { char c[15000]; int ipos, len; } st; bool glas(char s) { if (s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u') return true; return false; } int findPos(st* a, int k) { if (a->len == -1) a->len = strlen(a->c); if (a->ipos == -2) return -1; int i, p = 0; for (i = a->len - 1; i >= 0; --i) { if (glas(a->c[i])) { p++; if (p == k) { a->ipos = i; return i; } } } a->ipos = -2; return -1; } int rif(st* a, st* b, int k) { int r1 = a->ipos, r2 = b->ipos, i; if (a->ipos == -1) r1 = findPos(a, k); if (r1 == -1) return -1; if (b->ipos == -1) r2 = findPos(b, k); if (r2 == -1) return -1; if (a->len - a->ipos != b->len - b->ipos) return -1; for (i = 0; i < a->len - a->ipos; ++i) { if (a->c[i + a->ipos] != b->c[i + b->ipos]) return -1; } return 1; } int main() { int i, j, n, k; st chet[5]; cin >> n >> k; bool bSmeg = true, bPer = true, bOp = true, bAll = true; for (i = 0; i < n; ++i) { for (j = 0; j < 4; ++j) { cin >> chet[j].c; chet[j].ipos = -1; chet[j].len = -1; } int y1 = rif((st*)&chet[0], (st*)&chet[1], k); int y2 = rif((st*)&chet[2], (st*)&chet[3], k); if (y1 != 1 || y2 != 1) { bSmeg = false; bAll = false; } y1 = rif((st*)&chet[0], (st*)&chet[2], k); y2 = rif((st*)&chet[1], (st*)&chet[3], k); if (y1 != 1 || y2 != 1) { bPer = false; bAll = false; } y1 = rif((st*)&chet[0], (st*)&chet[3], k); y2 = rif((st*)&chet[1], (st*)&chet[2], k); if (y1 != 1 || y2 != 1) { bOp = false; bAll = false; } } if (bAll) { cout << "aaaa"; return 0; } if (bSmeg) { cout << "aabb"; return 0; } if (bPer) { cout << "abab"; return 0; } if (bOp) { cout << "abba"; return 0; } cout << "NO"; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const double PI = 4 * atan(1.0); void fast_stream() { std::ios_base::sync_with_stdio(0); } int K; bool isVowel(char a) { return a == 'a' || a == 'i' || a == 'u' || a == 'e' || a == 'o'; } bool match(string &a, string &b) { int cnt = 0; for (int i = 0;; i++) { if (a[a.size() - i - 1] == b[b.size() - i - 1]) { if (isVowel(a[a.size() - i - 1])) cnt++; } else return false; if (cnt == K) return true; if (a.size() - i - 2 < 0 || b.size() - i - 2 < 0) return false; } return false; } int a[10001]; void solve() { int n; cin >> n >> K; string s; vector<int> kinds; for (int i = 0; i < n; i++) { vector<string> vs; for (int j = 0; j < 4; j++) { cin >> s; vs.push_back(s); } bool ok = true; for (int j = 1; j < 4; j++) { if (!match(vs[0], vs[j])) ok = false; } if (ok) kinds.push_back(3); else { ok = match(vs[0], vs[2]) && match(vs[1], vs[3]); if (ok) kinds.push_back(1); else { ok = match(vs[0], vs[1]) && match(vs[2], vs[3]); if (ok) kinds.push_back(0); else { ok = match(vs[0], vs[3]) && match(vs[2], vs[1]); if (ok) kinds.push_back(2); else kinds.push_back(4); } } } } for (int i = 0; i < (int)kinds.size(); i++) { if (kinds[i] == 4) { cout << "NO" << endl; return; } } for (int i = 0; i < (int)kinds.size(); i++) { a[kinds[i]]++; } if (a[0] > 0 && a[1] == 0 && a[2] == 0) cout << "aabb" << endl; else if (a[1] > 0 && a[0] == 0 && a[2] == 0) cout << "abab" << endl; else if (a[2] > 0 && a[1] == 0 && a[0] == 0) cout << "abba" << endl; else if (a[3] > 0 && a[0] == 0 && a[1] == 0 && a[2] == 0) cout << "aaaa" << endl; else cout << "NO" << endl; } int main() { solve(); return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
n, k = [int(it) for it in raw_input().split()] Quatrains = [] Results = [] for i in range(0, n): temp = [] temp.append(raw_input()) temp.append(raw_input()) temp.append(raw_input()) temp.append(raw_input()) Quatrains.append(temp) def CheckQuatrain(a): def Suffix(a): result = [] count = k for it in a[::-1]: result.insert(0, it) if it in ["a", "e", "i", "o", "u"]: count -= 1 if count==0: return ''.join(result) return None a = map(Suffix, a) if a[0] == None or a[1] == None or a[2] == None or a[3] == None: return [] temp = [] if a[0] == a[1] and a[2] == a[3]: temp.append("aabb") if a[0] == a[2] and a[1] == a[3]: temp.append("abab") if a[0] == a[3] and a[1] == a[2]: temp.append("abba") if a[0] == a[1] == a[2] == a[3]: temp.append("aaaa") return temp for it in Quatrains: Results.append(CheckQuatrain(it)) temp = { "aaaa": True, "aabb": True, "abab": True, "abba": True } for it in Results: if not "aaaa" in it: temp["aaaa"] = False if not "aabb" in it: temp["aabb"] = False if not "abab" in it: temp["abab"] = False if not "abba" in it: temp["abba"] = False if temp["aaaa"]: print "aaaa" elif temp["aabb"]: print "aabb" elif temp["abab"]: print "abab" elif temp["abba"]: print "abba" else: print "NO"
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int style[2500]; int n, k; string lk[4]; string getLaskK(const string& str) { if (str.size() < k) return ""; int l = str.size() - 1; int tot = 0; while (l >= 0) { if (str[l] == 'a' || str[l] == 'e' || str[l] == 'i' || str[l] == 'o' || str[l] == 'u') { tot++; if (tot == k) break; } l--; } if (tot == k) return str.substr(l); else return ""; } inline bool isRhy(int ind1, int ind2) { if (lk[ind1].size() == 0 || lk[ind2].size() == 0) return false; return lk[ind1] == lk[ind2]; } int main() { bool a[4][4]; string strr; getline(cin, strr); stringstream ss; ss << strr; ss >> n >> k; bool ok = true; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { string str; getline(cin, str); lk[j] = getLaskK(str); if (lk[j].size() == 0) { ok = false; break; } } if (!ok) break; a[0][1] = isRhy(0, 1); a[2][3] = isRhy(2, 3); a[0][2] = isRhy(0, 2); a[1][3] = isRhy(1, 3); a[0][3] = isRhy(0, 3); a[1][2] = isRhy(1, 2); if (a[0][1] && a[1][2] && a[2][3]) { style[i] = 4; } else if (a[0][1] && a[2][3]) { style[i] = 1; } else if (a[0][2] && a[1][3]) { style[i] = 2; } else if (a[0][3] && a[1][2]) { style[i] = 3; } else { ok = false; break; } } if (!ok) { cout << "NO" << endl; return 0; } int curStyle = style[0]; for (int i = 1; i < n; i++) { if (curStyle == style[i]) continue; if (style[i] == 4) continue; if (curStyle == 4) { curStyle = style[i]; continue; } cout << "NO" << endl; return 0; } if (curStyle == 1) { cout << "aabb" << endl; } else if (curStyle == 2) { cout << "abab" << endl; } else if (curStyle == 3) { cout << "abba" << endl; } else if (curStyle == 4) { cout << "aaaa" << endl; } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; string s[4]; int t, n, k; int pd[4][4]; map<char, int> mp; char ss; bool xd(string s1, string s2) { int l1, l2; int tt = 0; l1 = s1.size() - 1; l2 = s2.size() - 1; while (l1 >= 0 && tt < k) { if (mp[s1[l1]]) { tt++; } l1--; } if (tt < k) { return 0; } tt = 0; while (l2 >= 0 && tt < k) { if (mp[s2[l2]]) { tt++; } l2--; } if (tt < k) { return 0; } return s1.substr(l1 + 1) == s2.substr(l2 + 1); } int main() { ss = 'a'; mp[ss] = 1; ss = 'e'; mp[ss] = 1; ss = 'i'; mp[ss] = 1; ss = 'o'; mp[ss] = 1; ss = 'u'; mp[ss] = 1; cin >> n >> k; for (int x = 0; x < n; x++) { for (int y = 0; y < 4; y++) { cin >> s[y]; } for (int y = 0; y < 4; y++) { for (int z = 0; z < y; z++) { if (!xd(s[y], s[z])) { pd[y][z] = 1; pd[z][y] = 1; } } } } int an = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { an += pd[x][y]; } } if (!an) { cout << "aaaa" << endl; } else if (!pd[0][1] && !pd[2][3]) { cout << "aabb" << endl; } else if (!pd[0][2] && !pd[1][3]) { cout << "abab" << endl; } else if (!pd[0][3] && !pd[1][2]) { cout << "abba" << endl; } else { cout << "NO" << endl; } }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int n, k; int a[2510]; int ans; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { string s[4]; string p[4]; for (int j = 0; j < 4; j++) { cin >> s[j]; int t = s[j].size() - 1; int cur = 0; while (t >= 0 && cur != k) { if (s[j][t] == 'a' || s[j][t] == 'e' || s[j][t] == 'o' || s[j][t] == 'i' || s[j][t] == 'u') cur++; if (cur != k) t--; } if (cur != k) { cout << "NO"; return 0; } p[j] = s[j].substr(t, s[j].size() - t); } if (p[0] == p[1] && p[1] == p[2] && p[2] == p[3]) a[i] = 4; else if (p[0] == p[1] && p[2] == p[3]) a[i] = 1; else if (p[0] == p[2] && p[1] == p[3]) a[i] = 2; else if (p[0] == p[3] && p[1] == p[2]) a[i] = 3; else { cout << "NO"; return 0; } if (i == 0) ans = a[0]; if (i > 0) { if (a[i] == 4 && a[i - 1] != 4) a[i] = a[i - 1]; if (a[i] != a[i - 1] && a[i - 1] != 4) { cout << "NO"; return 0; } if (a[i] != 4) ans = a[i]; } } if (ans == 1) cout << "aabb"; else if (ans == 2) cout << "abab"; else if (ans == 3) cout << "abba"; else cout << "aaaa"; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const long long Maxn = 1e5 + 7; const long long Max = 1e3 + 7; const long long Mod = 1e9 + 7; const long long Inf = 1e9 + 7; string s[Maxn], V[Maxn]; long long ans[5]; long long check(char t) { if (t == 'u' || t == 'o' || t == 'a' || t == 'i' || t == 'e') return true; return false; } void f(long long a, long long b, long long c, long long d) { if (V[a] == V[b] && V[c] == V[d]) ; else ans[1] = 0; if (V[a] == V[c] && V[b] == V[d]) ; else ans[2] = 0; if (V[a] == V[d] && V[c] == V[b]) ; else ans[3] = 0; if (V[a] == V[b] && V[c] == V[d] && V[c] == V[b]) ; else ans[4] = 0; } void print(long long i) { if (i == 1) cout << "aabb" << endl; if (i == 2) cout << "abab" << endl; if (i == 3) cout << "abba" << endl; if (i == 4) cout << "aaaa" << endl; } int main() { long long n, k; cin >> n >> k; for (long long i = 1; i <= 4 * n; i++) { cin >> s[i]; long long K = k; for (long long j = s[i].size() - 1; K && j >= 0; j--) { V[i] += s[i][j]; if (check(s[i][j])) K--; } if (K) { cout << "NO" << endl; return 0; } } ans[1] = ans[2] = ans[3] = ans[4] = 1; for (long long i = 1; i <= n; i++) { long long st = (i - 1) * 4; f(st + 1, st + 2, st + 3, st + 4); } for (long long i = 4; i >= 1; i--) { if (ans[i]) { print(i); return 0; } } cout << "NO" << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.*; import java.lang.*; import java.io.*; import java.awt.Point; // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// public static int mod(int x) { if(x <0)return -1*x ; else return x ; } public static long mod(long x) { if(x <0)return -1*x ; else return x ; } //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// static long factorial(long a) { if(a== 0L || a==1L)return 1L ; return a*factorial(a-1L) ; } /////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int countBits(long n) { int count = 0; while (n != 0) { count++; n = (n) >> (1L) ; } return count; } /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { long x = a% 2L; long y = b%2L ; if(x==y)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// static long xnor(long num1, long num2) { if (num1 < num2) { long temp = num1; num1 = num2; num2 = temp; } num1 = togglebit(num1); return num1 ^ num2; } static long togglebit(long n) { if (n == 0) return 1; long i = n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return i ^ n; } /////////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } public static long lcm(long a , long b ) { long gc = gcd(a,b); return (a*b)/gc ; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } static boolean isPrime(int n) { if(n==1) { return false ; } boolean ans = true ; for(int i = 2; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) { l++ ; } lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 1000001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } static ArrayList<Integer> getPrimeFactorization(int x) { ArrayList<Integer> ret = new ArrayList<Integer>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(int a ,int b) { //time comp : o(logn) long x = (long)(a) ; long n = (long)(b) ; if(n==0)return 1 ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n = n/2L ; x = x*x ; } return ans ; } public static long power(long a ,long b) { //time comp : o(logn) long x = (a) ; long n = (b) ; if(n==0)return 1L ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n = n/2L ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printArrayln(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printLArray(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printLArrayln(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. long res = 1; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x /= 2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod p. //(assuming p is prime). return modPow(a, p-2, p); } static long modBinomial(long n, long k, long p) { // calculates C(n,k) mod p (assuming p is prime). long numerator = 1; // n * (n-1) * ... * (n-k+1) for (int i=0; i<k; i++) { numerator = (numerator * (n-i) ) % p; } long denominator = 1; // k! for (int i=1; i<=k; i++) { denominator = (denominator * i) % p; } // numerator / denominator mod p. return ( numerator* modInverse(denominator,p) ) % p; } static void update(int val , long[] bit ,int n) { for( ; val <= n ; val += (val &(-val)) ) { bit[val]++ ; } } static long query(int val , long[] bit , int n) { long ans = 0L; for( ; val >=1 ; val-=(val&(-val)) )ans += bit[val]; return ans ; } static int countSetBits(long n) { int count = 0; while (n > 0) { n = (n) & (n - 1L); count++; } return count; } static int abs(int x) { if(x < 0)x = -1*x ; return x ; } static long abs(long x) { if(x < 0)x = -1L*x ; return x ; } ///////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// // static ArrayList<Integer>[] tree ; // static long[] child; // static int mod= 1000000007 ; // static int[][] pre = new int[3001][3001]; // static int[][] suf = new int[3001][3001] ; //program to calculate noof nodes in subtree for every vertex including itself // static void dfs(int sv) // { // child[sv] = 1L; // for(Integer x : tree[sv]) // { // if(child[x] == 0) // { // dfs(x) ; // child[sv] += child[x] ; // } // } // } static int bs(long[] arr , long key , int n, int s , int e) { int ans = 0 ; while( s <= e) { int m = (s+e)/2 ; if(arr[m] <= key) { ans =m ; s=m+1 ; } else{ e=m-1 ; } } return ans ; } // static long[][][] dp = new long[20][2][180] ; // public static long help(int pos , int tight , int sum , String s , int n){ // if(pos == n) // { // return sum ; // } // if(dp[pos][tight][sum] != -1)return dp[pos][tight][sum] ; // if(tight == 1) // { // int ans = 0; // } // } ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; //sieve() ; //ArrayList<Integer> arr[] = new ArrayList[n] ; ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> lista = new ArrayList<>() ; ArrayList<Long> listb = new ArrayList<>() ; // ArrayList<Integer> lista = new ArrayList<>() ; // ArrayList<Integer> listb = new ArrayList<>() ; //ArrayList<String> lists = new ArrayList<>() ; HashMap<Integer,Integer> map = new HashMap<>() ; //HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> mapx = new HashMap<>() ; HashMap<Integer,Integer> mapy = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcase = 1; //testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; // tree = new ArrayList[n] ; // child = new long[n] ; // for(int i = 0; i< n; i++) // { // tree[i] = new ArrayList<Integer>(); // int n = scn.nextInt() ;int k = scn.nextInt() ; int ans = 1 ; while(n-- > 0) { String s1 = scn.next() ; String s2 = scn.next() ; String s3 = scn.next() ; String s4 = scn.next() ; ///////////////////////////////////////////////////////////////////////////////////////////////////// int c =0 ;int i = s1.length()-1 ; for( ; i>=0 ;i-- ) { if(s1.charAt(i) =='a' || s1.charAt(i) =='e' || s1.charAt(i) =='i'||s1.charAt(i) =='o' ||s1.charAt(i) =='u') { c++ ; } if(c== k){ break ; } } if(i==-1) { ans = 0 ;break ; } s1 = s1.substring(i) ; //////////////////////////////////////////////////////////////////////////////////////////////////// c =0 ; i = s2.length()-1 ; for( ; i>=0 ;i-- ) { if(s2.charAt(i) =='a' || s2.charAt(i) =='e' || s2.charAt(i) =='i'||s2.charAt(i) =='o' ||s2.charAt(i) =='u') { c++ ; } if(c== k){ break ; } } if(i==-1) { ans = 0 ;break ; } s2 = s2.substring(i) ; /////////////////////////////////////////////////////////////////////////////////////////////////// c =0 ; i = s3.length()-1 ; for( ; i>=0 ;i-- ) { if(s3.charAt(i) =='a' || s3.charAt(i) =='e' || s3.charAt(i) =='i'||s3.charAt(i) =='o' ||s3.charAt(i) =='u') { c++ ; } if(c== k){ break ; } } if(i==-1) { ans = 0 ;break ; } s3 = s3.substring(i) ; //////////////////////////////////////////////////////////////////////////////////////////////////// c =0 ; i = s4.length()-1 ; for( ; i>=0 ;i-- ) { if(s4.charAt(i) =='a' || s4.charAt(i) =='e' || s4.charAt(i) =='i'||s4.charAt(i) =='o' ||s4.charAt(i) =='u') { c++ ; } if(c== k){ break ; } } if(i==-1) { ans = 0 ;break ; } s4 = s4.substring(i) ; if( s1.equals(s2) && s2.equals(s3) && s3.equals(s4))set.add(1) ; else if( s1.equals(s2) && s3.equals(s4))set.add(2) ; else if( s1.equals(s3) && s2.equals(s4))set.add(3) ; else if( s1.equals(s4) && s2.equals(s3))set.add(4) ; else{ ans = 0 ;break ; } } if(ans == 0)out.println("NO") ; else{ if(set.size() >2)out.println("NO") ; else if(set.size() == 2 ) { if(!set.contains(1))out.println("NO") ; else{ set.remove(1) ; if(set.contains(2))out.println("aabb") ; else if(set.contains(3))out.println("abab") ; else out.println("abba") ; } } else { if(set.contains(1))out.println("aaaa") ; else if(set.contains(2))out.println("aabb") ; else if(set.contains(3))out.println("abab") ; else out.println("abba") ; } } //out.println("Case #" + testcases + ": " + ans ) ; //out.println("@") ; set.clear() ; sb.delete(0 , sb.length()) ; list.clear() ;lista.clear() ;listb.clear() ; map.clear() ; mapx.clear() ; mapy.clear() ; setx.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { double first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class C{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-9; int n, k; String[] ss; void run(){ n=sc.nextInt(); k=sc.nextInt(); sc.nextLine(); ss=new String[4*n]; for(int i=0; i<4*n; i++){ ss[i]=sc.nextLine(); } solve(); } void solve(){ boolean[] isVowel=new boolean[256]; isVowel['a']=isVowel['e']=isVowel['i']=isVowel['o']=isVowel['u']=true; long c=(int)1e9+7; HashSet<Integer> set=new HashSet<Integer>(); for(int j=0; j<n; j++){ long[] hash=new long[4]; for(int i=0; i<4; i++){ int vowel=0; String s=ss[j*4+i]; int m=s.length(); debug(j, i, s); int index; for(index=m-1; index>=0; index--){ hash[i]=hash[i]*c+s.charAt(index); if(isVowel[s.charAt(index)]){ vowel++; if(vowel==k){ break; } } } if(index<0){ set.add(4); set.add(5); } } debug(j, hash); if(hash[0]==hash[1]&&hash[1]==hash[2]&&hash[2]==hash[3]){ set.add(3); }else{ if(hash[0]==hash[1]&&hash[2]==hash[3]){ set.add(0); }else if(hash[0]==hash[2]&&hash[1]==hash[3]){ set.add(1); }else if(hash[0]==hash[3]&&hash[1]==hash[2]){ set.add(2); }else{ set.add(4); set.add(5); } } } debug(set); Integer[] is=set.toArray(new Integer[0]); String[] as={"aabb", "abab", "abba", "aaaa"}; if(is.length==1){ println(as[is[0]]); }else if(is.length==2&&set.contains(3)){ println(as[min(is[0], is[1])]); }else{ println("NO"); } } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } void debug(Object... os){ // System.err.println(Arrays.deepToString(os)); } public static void main(String[] args){ new C().run(); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
from sys import stdin rints = lambda: [int(x) for x in stdin.readline().split()] str_inp = lambda n: [stdin.readline()[:-1] for x in range(n)] n, k = rints() a, vow, pat, flag = [str_inp(4) for _ in range(n)], 'aeiou', set(), 0 pats = {'aabb', 'abab', 'abba'} for i in range(n): ixs = [0] * (4) for j in range(4): k1 = k for s in a[i][j][::-1]: if not k1: break if s in vow: k1 -= 1 ixs[j] -= 1 if k1: print('NO') exit() dis = [] for s in range(4): if a[i][s][ixs[s]:] not in dis: dis.append(a[i][s][ixs[s]:]) if len(dis) > 2: print('NO') exit() elif len(dis) == 2: tem = '' for s in range(4): if a[i][s][ixs[s]:] == dis[0]: tem += 'a' else: tem += 'b' if tem in pats: pat.add(tem) else: print('NO') exit() else: flag = 1 if len(pat) == 1: print(pat.pop()) elif flag and not pat: print('aaaa') else: print('NO')
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int countbit(int n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } int lowbit(int n) { return (n ^ (n - 1)) & n; } const double pi = acos(-1.0); const double eps = 1e-11; char str[4][10010]; int N, K; char mode[4][5] = {"aaaa", "aabb", "abba", "abab"}; int getMode() { int x[4]; for (int i = 0; i < 4; ++i) { x[i] = -1; int len = strlen(str[i]); for (int j = len - 1, index = 0; j >= 0; --j) { if (str[i][j] == 'i' || str[i][j] == 'e' || str[i][j] == 'o' || str[i][j] == 'u' || str[i][j] == 'a') index++; if (index == K) { x[i] = j; break; } } if (x[i] == -1) return -1; } int mat[4][4] = {}; for (int i = 0; i < 4; ++i) for (int j = i + 1; j < 4; ++j) mat[i][j] = strcmp(str[i] + x[i], str[j] + x[j]); if (mat[0][2] == 0 && mat[0][1] == 0 && mat[0][3] == 0) return 0; if (mat[0][1] == 0 && mat[2][3] == 0) return 1; if (mat[0][3] == 0 && mat[1][2] == 0) return 2; if (mat[0][2] == 0 && mat[1][3] == 0) return 3; return -1; } int main() { int m = -1; scanf("%d%d", &N, &K); for (int i = 0; i < N; ++i) { for (int j = 0; j < 4; ++j) scanf("%s", str[j]); int tm = getMode(); if (tm == -1) { m = -1; break; } else if (m == -1) m = tm; else if (m == 0) m = tm; else if (tm == 0) m = m; else if (m != tm) { m = -1; break; } } if (m == -1) printf("NO\n"); else printf("%s\n", mode[m]); return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import sys n,k=map(int,sys.stdin.readline().split()) def func(s): if (s==''): return cur_gl=k pos=len(s)-1 while pos>=0 and cur_gl>0: if s[pos] in ('a','e','i','o','u'): cur_gl-=1 if cur_gl>0: pos-=1 if cur_gl>0: print 'NO' exit(0) return s[pos:] stikh=tuple(map(func,sys.stdin.read().split('\n'))) ans='aaaa' for i in range(n): cur='NO' if stikh[i*4]==stikh[i*4+1]==stikh[i*4+2]==stikh[i*4+3]: cur='aaaa' elif stikh[i*4]==stikh[i*4+3] and stikh[i*4+1]==stikh[i*4+2]: cur='abba' elif stikh[i*4]==stikh[i*4+2] and stikh[i*4+1]==stikh[i*4+3]: cur='abab' elif stikh[i*4]==stikh[i*4+1] and stikh[i*4+2]==stikh[i*4+3]: cur='aabb' if not (ans=='aaaa' or ans==cur or cur=='aaaa'): ans='NO' break else: if cur!='aaaa': ans=cur print ans
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.Scanner; // C public class Main { private static boolean isVowel(char c) { return c=='a' || c=='e' || c=='i' || c=='o' || c=='u'; } public static void main (String[] args) { Scanner c = new Scanner(System.in); int n = c.nextInt(); int k = c.nextInt(); c.nextLine(); boolean aabb = true; boolean abab = true; boolean abba = true; String line; StringBuffer[] quat = new StringBuffer[4]; for(int q=0; q<n; q++) { for(int l=0; l<4; l++) { line = c.nextLine(); int kcpt = k; quat[l] = new StringBuffer(""); for(int i=line.length()-1; i>=0 && kcpt>0; i--) { if(isVowel(line.charAt(i))) { kcpt--; } quat[l].append(line.charAt(i)); } if(kcpt != 0) { System.out.println("NO"); return; } } /*if(aabb && (!quat[0].equals(quat[1]) || !quat[2].equals(quat[3]))) { aabb = false; } if(abab && (!quat[0].equals(quat[2]) || !quat[1].equals(quat[3]))) { abab = false; } if(abba && (!quat[0].equals(quat[3]) || !quat[1].equals(quat[2]))) { abba = false; }*/ if(aabb && (!quat[0].toString().equals(quat[1].toString()) || !quat[2].toString().equals(quat[3].toString()))) { aabb = false; } if(abab && (!quat[0].toString().equals(quat[2].toString()) || !quat[1].toString().equals(quat[3].toString()))) { abab = false; } if(abba && (!quat[0].toString().equals(quat[3].toString()) || !quat[1].toString().equals(quat[2].toString()))) { abba = false; } if(!aabb && !abab && !abba) { System.out.println("NO"); return; } } if(aabb && abab && abba) { System.out.println("aaaa"); } else if(aabb) { System.out.println("aabb"); } else if(abab) { System.out.println("abab"); } else if(abba) { System.out.println("abba"); } } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Artem Gilmudinov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Reader in = new Reader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Reader in, PrintWriter out) { int n, k; n = in.ni(); k = in.ni(); String[] s = new String[4 * n]; for (int i = 0; i < 4 * n; i++) { s[i] = in.rl(); } String[] suff = new String[4 * n]; for (int i = 0; i < 4 * n; i++) { int pos = -1; String str = s[i]; for (int j = str.length() - 1, cnt = 0; j >= 0; j--) { if (isVowel(str.charAt(j))) { ++cnt; if (cnt == k) { pos = j; } } } if (pos == -1) { suff[i] = i + ""; continue; } StringBuilder sb = new StringBuilder(); for (int j = pos; j < str.length(); j++) { sb.append(str.charAt(j)); } suff[i] = sb.toString(); } boolean[][] vars = new boolean[n][3]; for (int i = 0; i < 4 * n; i += 4) { if (suff[i].equals(suff[i + 1])) { if (suff[i + 2].equals(suff[i + 3])) { if (suff[i + 1].equals(suff[i + 2])) { for (int j = 0; j < 3; j++) { vars[i / 4][j] = true; } } else { vars[i / 4][0] = true; } } } else { if (suff[i].equals(suff[i + 2])) { if (suff[i + 1].equals(suff[i + 3])) { vars[i / 4][1] = true; } } else { if (suff[i].equals(suff[i + 3])) { if (suff[i + 1].equals(suff[i + 2])) { vars[i / 4][2] = true; } } } } } boolean flag = true; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { flag &= vars[i][j]; } } if (flag) { out.println("aaaa"); return; } for (int j = 0; j < 3; j++) { boolean ok = true; for (int i = 0; i < n; i++) { ok &= vars[i][j]; } if (ok) { if (j == 0) { out.println("aabb"); } if (j == 1) { out.println("abab"); } if (j == 2) { out.println("abba"); } return; } } out.println("NO"); } public boolean isVowel(char ch) { return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } } static class Reader { private BufferedReader in; private StringTokenizer st = new StringTokenizer(""); private String delim = " "; public Reader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public String next() { if (!st.hasMoreTokens()) { st = new StringTokenizer(rl()); } return st.nextToken(delim); } public String rl() { try { return in.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int ni() { return Integer.parseInt(next()); } } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import static java.lang.Math.*; import java.util.*; import java.io.*; public class C { public void solve() throws Exception { int n = nextInt(), kk = nextInt(); boolean aabb=true, abab=true, abba=true; for (int i=0; i<n; ++i) { String[] q = new String[4]; int[] cnt = new int[4]; for (int j=0; j<4; ++j) { q[j] = nextLine(); int wcnt = 0; for (int k=q[j].length()-1; k>=0; --k) { if (charIn(q[j].charAt(k), "aeiou")) wcnt++; if (wcnt==kk) { cnt[j] = q[j].length()-k; break; } } if (cnt[j]==0) halt("NO"); } aabb &= fits(q, cnt, 0, 1, 2, 3); abba &= fits(q, cnt, 0, 3, 1, 2); abab &= fits(q, cnt, 0, 2, 1, 3); } int cnt = 0; cnt = (abab ? 1:0) + (abba ? 1:0) + (aabb ? 1:0); if (cnt>1) halt("aaaa"); if (cnt==0) halt("NO"); if (abab) halt("abab"); if (abba) halt("abba"); halt("aabb"); } boolean fits(String[] q, int[] cnt, int a1, int a2, int b1, int b2) { if (cnt[a1]!=cnt[a2] || cnt[b1]!=cnt[b2]) return false; for (int i=0; i<cnt[a1]; ++i) if (q[a1].charAt(q[a1].length()-i-1) != q[a2].charAt(q[a2].length()-i-1)) return false; for (int i=0; i<cnt[b1]; ++i) if (q[b1].charAt(q[b1].length()-i-1) != q[b2].charAt(q[b2].length()-i-1)) return false; return true; } boolean charIn(char ch, String s) { if (s==null) return false; for (int i=0; i<s.length(); ++i) if (ch==s.charAt(i)) return true; return false; } //////////////////////////////////////////////////////////////////////////// boolean showDebug = true; static boolean useFiles = false; static String inFile = "input.txt"; static String outFile = "output.txt"; double EPS = 1e-7; int INF = Integer.MAX_VALUE; long INFL = Long.MAX_VALUE; double INFD = Double.MAX_VALUE; int absPos(int num) { return num<0 ? 0:num; } long absPos(long num) { return num<0 ? 0:num; } double absPos(double num) { return num<0 ? 0:num; } int min(int... nums) { int r = INF; for (int i: nums) if (i<r) r=i; return r; } int max(int... nums) { int r = -INF; for (int i: nums) if (i>r) r=i; return r; } long minL(long... nums) { long r = INFL; for (long i: nums) if (i<r) r=i; return r; } long maxL(long... nums) { long r = -INFL; for (long i: nums) if (i>r) r=i; return r; } double minD(double... nums) { double r = INFD; for (double i: nums) if (i<r) r=i; return r; } double maxD(double... nums) { double r = -INFD; for (double i: nums) if (i>r) r=i; return r; } long sumArr(int[] arr) { long res = 0; for (int i: arr) res+=i; return res; } long sumArr(long[] arr) { long res = 0; for (long i: arr) res+=i; return res; } double sumArr(double[] arr) { double res = 0; for (double i: arr) res+=i; return res; } long partsFitCnt(long partSize, long wholeSize) { return (partSize+wholeSize-1)/partSize; } boolean odd(long num) { return (num&1)==1; } boolean hasBit(int num, int pos) { return (num&(1<<pos))>0; } long binpow(long a, int n) { long r = 1; while (n>0) { if ((n&1)!=0) r*=a; a*=a; n>>=1; } return r; } boolean isLetter(char c) { return (c>='a' && c<='z') || (c>='A' && c<='Z'); } boolean isLowercase(char c) { return (c>='a' && c<='z'); } boolean isUppercase(char c) { return (c>='A' && c<='Z'); } boolean isDigit(char c) { return (c>='0' && c<='9'); } String stringn(String s, int n) { if (n<1) return ""; StringBuilder sb = new StringBuilder(s.length()*n); for (int i=0; i<n; ++i) sb.append(s); return sb.toString(); } String str(Object o) { return o.toString(); } long timer = System.currentTimeMillis(); void startTimer() { timer = System.currentTimeMillis(); } void stopTimer() { System.err.println("time: "+(System.currentTimeMillis()-timer)/1000.0); } static class InputReader { private byte[] buf; private int bufPos = 0, bufLim = -1; private InputStream stream; public InputReader(InputStream stream, int size) { buf = new byte[size]; this.stream = stream; } private void fillBuf() throws IOException { bufLim = stream.read(buf); bufPos = 0; } char read() throws IOException { if (bufPos>=bufLim) fillBuf(); return (char)buf[bufPos++]; } boolean hasInput() throws IOException { if (bufPos>=bufLim) fillBuf(); return bufPos<bufLim; } } static InputReader inputReader; static BufferedWriter outputWriter; char nextChar() throws IOException { return inputReader.read(); } char nextNonWhitespaceChar() throws IOException { char c = inputReader.read(); while (c<=' ') c=inputReader.read(); return c; } String nextWord() throws IOException { StringBuilder sb = new StringBuilder(); char c = inputReader.read(); while (c<=' ') c=inputReader.read(); while (c>' ') { sb.append(c); c = inputReader.read(); } return new String(sb); } String nextLine() throws IOException { StringBuilder sb = new StringBuilder(); char c = inputReader.read(); while (c<=' ') c=inputReader.read(); while (c!='\n' && c!='\r') { sb.append(c); c = inputReader.read(); } return new String(sb); } int nextInt() throws IOException { int r = 0; char c = nextNonWhitespaceChar(); boolean neg = false; if (c=='-') neg=true; else r=c-48; c = nextChar(); while (c>='0' && c<='9') { r*=10; r+=c-48; c=nextChar(); } return neg ? -r:r; } long nextLong() throws IOException { long r = 0; char c = nextNonWhitespaceChar(); boolean neg = false; if (c=='-') neg=true; else r = c-48; c = nextChar(); while (c>='0' && c<='9') { r*=10L; r+=c-48L; c=nextChar(); } return neg ? -r:r; } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextWord()); } int[] nextArr(int size) throws NumberFormatException, IOException { int[] arr = new int[size]; for (int i=0; i<size; ++i) arr[i] = nextInt(); return arr; } long[] nextArrL(int size) throws NumberFormatException, IOException { long[] arr = new long[size]; for (int i=0; i<size; ++i) arr[i] = nextLong(); return arr; } double[] nextArrD(int size) throws NumberFormatException, IOException { double[] arr = new double[size]; for (int i=0; i<size; ++i) arr[i] = nextDouble(); return arr; } String[] nextArrS(int size) throws NumberFormatException, IOException { String[] arr = new String[size]; for (int i=0; i<size; ++i) arr[i] = nextWord(); return arr; } char[] nextArrCh(int size) throws IOException { char[] arr = new char[size]; for (int i=0; i<size; ++i) arr[i] = nextNonWhitespaceChar(); return arr; } char[][] nextArrCh(int rows, int columns) throws IOException { char[][] arr = new char[rows][columns]; for (int i=0; i<rows; ++i) for (int j=0; j<columns; ++j) arr[i][j] = nextNonWhitespaceChar(); return arr; } char[][] nextArrChBorders(int rows, int columns, char border) throws IOException { char[][] arr = new char[rows+2][columns+2]; for (int i=1; i<=rows; ++i) for (int j=1; j<=columns; ++j) arr[i][j] = nextNonWhitespaceChar(); for (int i=0; i<columns+2; ++i) { arr[0][i] = border; arr[rows+1][i] = border; } for (int i=0; i<rows+2; ++i) { arr[i][0] = border; arr[i][columns+1] = border; } return arr; } void printf(String format, Object... args) throws IOException { outputWriter.write(String.format(format, args)); } void print(Object o) throws IOException { outputWriter.write(o.toString()); } void println(Object o) throws IOException { outputWriter.write(o.toString()); outputWriter.newLine(); } void print(Object... o) throws IOException { for (int i=0; i<o.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(o[i].toString()); } } void println(Object... o) throws IOException { print(o); outputWriter.newLine(); } void printn(Object o, int n) throws IOException { String s = o.toString(); for (int i=0; i<n; ++i) { outputWriter.write(s); if (i!=n-1) outputWriter.write(' '); } } void printnln(Object o, int n) throws IOException { printn(o, n); outputWriter.newLine(); } void printArr(int[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Integer.toString(arr[i])); } } void printArr(long[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Long.toString(arr[i])); } } void printArr(double[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Double.toString(arr[i])); } } void printArr(String[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(arr[i]); } } void printArr(char[] arr) throws IOException { for (char c: arr) outputWriter.write(c); } void printlnArr(int[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(long[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(double[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(String[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(char[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void halt(Object... o) throws IOException { if (o.length!=0) println(o); outputWriter.flush(); outputWriter.close(); System.exit(0); } void debug(Object... o) { if (showDebug) System.err.println(Arrays.deepToString(o)); } public static void main(String[] args) throws Exception { Locale.setDefault(Locale.US); if (!useFiles) { inputReader = new InputReader(System.in, 1<<16); outputWriter = new BufferedWriter(new OutputStreamWriter(System.out), 1<<16); } else { inputReader = new InputReader(new FileInputStream(new File(inFile)), 1<<16); outputWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outFile))), 1<<16); } new C().solve(); outputWriter.flush(); outputWriter.close(); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.io.*; import java.util.*; public class Main implements Runnable{ private void solve()throws IOException{ HashSet<Character> vowels=new HashSet<>(); vowels.add('a'); vowels.add('e'); vowels.add('i'); vowels.add('o'); vowels.add('u'); int n=nextInt(); int k=nextInt(); String scheme[]=new String[n+1]; for(int i=1;i<=n;i++){ char line[]=new char[4]; for(int j=0;j<4;j++) line[j]=(char)('a'+j); HashMap<String,Integer> map=new HashMap<>(); for(int j=0;j<4;j++) { String s=nextLine(); int vowelcount=0; String suff=""; for(int l=s.length()-1;l>=0;l--){ if(vowels.contains(s.charAt(l))) vowelcount++; suff+=s.charAt(l); if(vowelcount==k) { if(map.containsKey(suff)) line[j]=line[map.get(suff)]; else map.put(suff,j); break; } } } String s=new String(line); scheme[i]=s.equals("aacc")?"aabb":s; } HashSet<String> schemes=new HashSet<>(); schemes.add("aabb"); schemes.add("abab"); schemes.add("abba"); schemes.add("aaaa"); boolean possible=true; String ans=null; for(int i=1;i<=n;i++){ if(!schemes.contains(scheme[i])) { possible=false; break; } else if(i!=1 && !scheme[i].equals("aaaa") && !scheme[i-1].equals("aaaa") && !scheme[i].equals(scheme[i-1])) { possible=false; break; } if(!scheme[i].equals("aaaa")) ans=scheme[i]; } if(possible) out.println(ans==null?"aaaa":ans); else out.println("NO"); } /////////////////////////////////////////////////////////// final long mod=(long)(1e9+7); final int inf=(int)(1e9+1); final int maxn=(int)(1e6); final long lim=(long)(1e18); public void run(){ try{ br=new BufferedReader(new InputStreamReader(System.in)); st=null; out=new PrintWriter(System.out); solve(); // int t=nextInt(); // for(int i=1;i<=t;i++){ // // out.print("Case #"+i+": "); // solve(); // } br.close(); out.close(); }catch(Exception e){ e.printStackTrace(); System.exit(1); } } public static void main(String args[])throws IOException{ new Main().run(); } int max(int ... a){ int ret=a[0]; for(int i=1;i<a.length;i++) ret=Math.max(ret,a[i]); return ret; } int min(int ... a){ int ret=a[0]; for(int i=1;i<a.length;i++) ret=Math.min(ret,a[i]); return ret; } void debug(Object ... a){ System.out.print("> "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } void debug(int a[]){debuga(Arrays.stream(a).boxed().toArray());} void debug(long a[]){debuga(Arrays.stream(a).boxed().toArray());} void debuga(Object a[]){ System.out.print("> "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } BufferedReader br; StringTokenizer st; PrintWriter out; String nextToken()throws IOException{ while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws IOException{ return br.readLine(); } int nextInt()throws IOException{ return Integer.parseInt(nextToken()); } long nextLong()throws IOException{ return Long.parseLong(nextToken()); } double nextDouble()throws IOException{ return Double.parseDouble(nextToken()); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() ACCEPTED={'aaaa','aabb','abba','abab'} vowels='aeiou' nul='abcd' nu=0 def operate(s): global nu c=0 for i in range(len(s)-1,-1,-1): if(s[i] in vowels): c+=1 if(c==k): return s[i:] nu=(nu+1)%4 return nul[nu] def rhymes(a): a=[operate(i) for i in a] # print(a) ID={} id=0 ans='' for i in a: if(i not in ID): ID[i]=nul[id] id+=1 ans+=ID[i] return ans n,k=value() scheme=set() for i in range(n): a=[] for j in range(4): a.append(input()) scheme.add(rhymes(a)) # print(scheme) for i in scheme: if(i not in ACCEPTED): print("NO") exit() if(len(scheme)>2): print("NO") elif(len(scheme)==2): if('aaaa' not in scheme): print("NO") else: for i in scheme: if(i!='aaaa'): print(i) else: print(*scheme)
PYTHON3
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.*; public class c { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int k = input.nextInt(); String a, b, c, d; boolean abab = true, abba = true, aabb = true, aaaa = true; for(int i = 0; i < n; i++) { a = input.next(); b = input.next(); c = input.next(); d = input.next(); int vowelsFound = 0; for(int j = a.length() - 1; j >=0; j--) { char ch = a.charAt(j); int v = ch - 'a'; if(v == 0 || v == 4 || v == 8 || v == 14 || v == 20) vowelsFound++; if(vowelsFound == k) { a = a.substring(j); break; } } if(vowelsFound < k) { aaaa = false; aabb = false; abab = false; abba = false; } vowelsFound = 0; for(int j = b.length() - 1; j >=0; j--) { char ch = b.charAt(j); int v = ch - 'a'; if(v == 0 || v == 4 || v == 8 || v == 14 || v == 20) vowelsFound++; if(vowelsFound == k) { b = b.substring(j); break; } } if(vowelsFound < k) { aaaa = false; aabb = false; abab = false; abba = false; } vowelsFound = 0; for(int j = c.length() - 1; j >=0; j--) { char ch = c.charAt(j); int v = ch - 'a'; if(v == 0 || v == 4 || v == 8 || v == 14 || v == 20) vowelsFound++; if(vowelsFound == k) { c = c.substring(j); break; } } if(vowelsFound < k) { aaaa = false; aabb = false; abab = false; abba = false; } vowelsFound= 0; for(int j = d.length() - 1; j >=0; j--) { char ch = d.charAt(j); int v = ch - 'a'; if(v == 0 || v == 4 || v == 8 || v == 14 || v == 20) vowelsFound++; if(vowelsFound == k) { d = d.substring(j); break; } } if(vowelsFound < k) { aaaa = false; aabb = false; abab = false; abba = false; } //System.out.println(d); if(!a.equals(b) || !c.equals(d)) aabb = false; if(!a.equals(c) || !b.equals(d)) abab = false; if(!a.equals(d) || !b.equals(c)) abba = false; } if(!aabb || !abab || !abba) aaaa = false; if(aaaa) System.out.println("aaaa"); else if(abba) System.out.println("abba"); else if(abab) System.out.println("abab"); else if(aabb) System.out.println("aabb"); else System.out.println("NO"); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const string tp[4] = {"aaaa", "aabb", "abab", "abba"}; int n, k; string w[10000]; int main() { cin >> n >> k; n *= 4; bool bad = false; for (int i = 0; i < n; i++) { string s; cin >> s; w[i] = ""; int l = 0; while (l < k && (int)s.size()) { char ch = s[(int)s.size() - 1]; w[i] += ch; s.erase(s.end() - 1); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++l; } } if (l < k) { bad = true; } } if (!bad) { for (int t = 0; t < 4; t++) { bool ok2 = true; for (int i = 0; i < n; i += 4) { bool ok = true; for (int p = 0; p < 4 && ok; p++) { for (int q = p + 1; q < 4 && ok; q++) { if (tp[t][q] == tp[t][p] && w[i + p] != w[i + q]) { ok = false; } } } if (!ok) { ok2 = false; break; } } if (ok2) { cout << tp[t] << endl; return 0; } } } cout << "NO" << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int k; bool Is(char a) { return (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u'); } bool Equal(string s, string t) { int n = (int)s.size(), m = (int)t.size(); int v1 = 0, v2 = 0; for (int i = 0; i < n; i++) v1 += Is(s[i]); for (int i = 0; i < m; i++) v2 += Is(t[i]); if (v1 < k || v2 < k) return false; string s1 = "", s2 = ""; int cnt = 0; for (int i = n - 1; i >= 0; i--) { cnt += Is(s[i]); s1 += s[i]; if (cnt == k) break; } cnt = 0; for (int i = m - 1; i >= 0; i--) { cnt += Is(t[i]); s2 += t[i]; if (cnt == k) break; } reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); return (s1 == s2); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n >> k; vector<string> a(4 * n); vector<int> cnt(4, 0); for (int i = 0; i < 4 * n; i++) cin >> a[i]; for (int p = 0; p <= 4 * n - 4; p += 4) { if (Equal(a[p], a[p + 1]) && Equal(a[p + 2], a[p + 3])) cnt[0] += 1; if (Equal(a[p], a[p + 2]) && Equal(a[p + 1], a[p + 3])) cnt[1] += 1; if (Equal(a[p], a[p + 3]) && Equal(a[p + 1], a[p + 2])) cnt[2] += 1; bool ok = true; for (int i = p; i < p + 4; i++) { for (int j = p; j < p + 4; j++) { if (!Equal(a[i], a[j])) { ok = false; break; } } if (!ok) break; } cnt[3] += ok; } if (*max_element(cnt.begin(), cnt.end()) != n) { cout << "NO\n"; return 0; } int mx = cnt[3]; int who = 3; for (int i = 0; i < 4; i++) { if (cnt[i] > mx) { mx = cnt[i]; who = i; } } if (who == 3) { cout << "aaaa\n"; return 0; } if (who == 0) { cout << "aabb"; } else if (who == 1) { cout << "abab"; } else { cout << "abba"; } cout << '\n'; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#!/usr/bin/env python """(c) gorlum0 [at] gmail.com""" import itertools as it from sys import stdin vowels = 'aeiou' types_rhymes = 'NO abba abab # aabb # # aaaa'.split() def suffix(k, w): for i in xrange(len(w)-1, -1, -1): if w[i] in vowels: k -= 1 if not k: break return w[i:] if not k else '' def rhyme(k, w1, w2, w3, w4): s1 = suffix(k, w1) s2 = suffix(k, w2) s3 = suffix(k, w3) s4 = suffix(k, w4) ## print s1, s2, s3, s4 if not (s1 and s2 and s3 and s4): return 0 if s1 == s2 == s3 == s4: return 7 # 111 elif s1 == s2 and s3 == s4: return 4 # 100 elif s1 == s3 and s2 == s4: return 2 # 010 elif s1 == s4 and s2 == s3: return 1 # 001 else: return 0 def solve(n, k, ws): res = rhyme(k, *ws[:4]) for i in xrange(4, n*4, 4): res &= rhyme(k, *ws[i:i+4]) return types_rhymes[res] def main(): for l in stdin: n, k = map(int, l.split()) ws = [next(stdin).strip() for _ in xrange(n*4)] ## print ws print solve(n, k, ws) if __name__ == '__main__': main()
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int k; bool eq(char &q) { return (q == 'a' || q == 'e' || q == 'o' || q == 'u' || q == 'i'); } bool rifm(string &a, string &b) { int n = a.length(), m = b.length(); int k1, k2; k1 = k2 = 0; int i = n; while (i > 0 && k1 < k) { i--; if (eq(a[i])) k1++; } if (k1 < k) return false; int j = m; while (j > 0 && k2 < k) { j--; if (eq(b[j])) k2++; } if (k2 < k) return false; if (n - i != m - j) return false; for (int e = 0; e < n - i; e++) if (a[e + i] != b[e + j]) return false; return true; } int main() { int n; string a, b, c, d; scanf("%d%d\n", &n, &k); bool aabb, abab, abba, aaaa; aabb = abab = abba = aaaa = true; for (int i = 1; i <= n; i++) { getline(cin, a); getline(cin, b); getline(cin, c); getline(cin, d); bool ab = rifm(a, b); bool cd = rifm(c, d); aabb = aabb && (ab && cd); bool ac = rifm(a, c); bool bd = rifm(b, d); abab = abab && (ac && bd); bool ad = rifm(a, d); bool bc = rifm(b, c); abba = abba && (ad && bc); aaaa = aaaa && (ab && bc && cd); } if (aaaa) cout << "aaaa\n"; else if (aabb) cout << "aabb\n"; else if (abab) cout << "abab\n"; else if (abba) cout << "abba\n"; else cout << "NO\n"; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
def bad(a,b,c,d): if a == 'bad' or b == 'bad' or c == 'bad' or d == 'bad': return True return False if __name__ == "__main__": p = map(eval,raw_input().split(" ")) n=p[0] K=p[1] poem = [] vowel = ['a','e','i','o','u'] for i in range(0,n): quat = [] for j in range(0,4): s = map(str,raw_input().split(" ")) s = s[0] s = s[::-1] cnt = 0 for p in range(0, len(s)): if s[p] in vowel: cnt = cnt + 1 if cnt == K: quat.append(s[0:p+1]) break if cnt < K: quat.append("bad") poem.append(quat) table = ["" for i in range(0,n)] for i in range(0,n): quat = poem[i] l1 = quat[0] l2 = quat[1] l3 = quat[2] l4 = quat[3] if bad(l1,l2,l3,l4): table[i] = "NO" continue if l1 == l2 and l1 == l3 and l1 == l4 and l2 == l3 and l2 == l4: table[i] = 'aaaa' elif l1 == l3 and l2 == l4 and l1 != l2: table[i] = 'abab' elif l1 == l2 and l3 == l4 and l1 != l3: table[i] = 'aabb' elif l1 == l4 and l2 == l3 and l1 != l3: table[i] = 'abba' else: table[i] = "NO" ans = 'aaaa' res = {} res['aaaa'] = 0 res['abba'] = 0 res['abab'] = 0 res['aabb'] = 0 res['NO'] = 0 for i in range(0,n): if table[i] == "bad": res["NO"] = res["NO"] + 1 else: res[table[i]] = res[table[i]] + 1 cnt = 0 last = 'aaaa' for key, value in res.iteritems(): if value > 0 and key != last and key != "NO": cnt = cnt + 1 last = key ans = key if res['NO'] > 0 or cnt > 1: ans = "NO" print ans
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, k, l, m; bool a, b, c; string s, t[4]; a = b = c = true; cin >> n >> k; for ((i) = 0; (i) < (int)(n); (i)++) { for ((j) = 0; (j) < (int)(4); (j)++) { cin >> s; l = 0; t[j] = ""; for (m = s.length() - 1; m >= 0; m--) { if (s[m] == 'a' || s[m] == 'i' || s[m] == 'u' || s[m] == 'e' || s[m] == 'o') l++; t[j] += s[m]; if (l == k) { reverse(t[j].begin(), t[j].end()); break; } } if (l != k) { cout << "NO" << endl; return 0; } } if (t[0] != t[1] || t[2] != t[3]) a = false; if (t[0] != t[2] || t[1] != t[3]) b = false; if (t[0] != t[3] || t[1] != t[2]) c = false; } if (a == true && b == true && c == true) cout << "aaaa" << endl; else if (a == true && b == false && c == false) cout << "aabb" << endl; else if (a == false && b == true && c == false) cout << "abab" << endl; else if (a == false && b == false && c == true) cout << "abba" << endl; else cout << "NO" << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int n, k; int rhymes[2550]; string arr[4]; int vowel(char a) { return (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u'); } int rhyme(string a, string b) { int cnt = 0; int i; for ((i) = 0; (i) < (int)(((int)(a).size())); (i)++) if (vowel(a[i])) cnt++; if (cnt < k) return 0; cnt = 0; for ((i) = 0; (i) < (int)(((int)(b).size())); (i)++) if (vowel(b[i])) cnt++; if (cnt < k) return 0; int aa = ((int)(a).size()) - 1; cnt = 0; while (1) { if (vowel(a[aa])) cnt++; if (cnt == k) break; aa--; } int bb = ((int)(b).size()) - 1; cnt = 0; while (1) { if (vowel(b[bb])) cnt++; if (cnt == k) break; bb--; } return a.substr(aa, ((int)(a).size()) - aa) == b.substr(bb, ((int)(b).size()) - bb); } int check[4][4]; int main() { memset(rhymes, -1, sizeof(rhymes)); int i, j, z; cin >> n >> k; for ((i) = 0; (i) < (int)(n); (i)++) { for ((j) = 0; (j) < (int)(4); (j)++) cin >> arr[j]; for ((j) = 0; (j) < (int)(4); (j)++) for ((z) = 0; (z) < (int)(4); (z)++) { if (rhyme(arr[j], arr[z])) check[j][z] = 1; else check[j][z] = 0; } if (check[0][1] && check[2][3]) rhymes[i] = 0; if (check[0][2] && check[1][3]) rhymes[i] = 1; if (check[0][3] && check[1][2]) rhymes[i] = 2; if (check[0][1] && check[1][2] && check[2][3]) rhymes[i] = 3; } int last = 1; for ((i) = 0; (i) < (int)(n); (i)++) { if (rhymes[i] != 3) last = 0; } if (last) { cout << "aaaa" << endl; cin.get(); cin.get(); return 0; } for ((i) = 0; (i) < (int)(3); (i)++) { int works = 1; for ((j) = 0; (j) < (int)(n); (j)++) { if (rhymes[j] != 3 && rhymes[j] != i) works = 0; } if (works) { if (i == 0) { cout << "aabb" << endl; cin.get(); cin.get(); return 0; } if (i == 1) { cout << "abab" << endl; cin.get(); cin.get(); return 0; } if (i == 2) { cout << "abba" << endl; cin.get(); cin.get(); return 0; } } } cout << "NO" << endl; cin.get(); cin.get(); return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k; int n, m; while (cin >> n >> m) { int f = 0; for (i = 0; i < n; ++i) { string t[4]; for (j = 0; j < 4; ++j) { string s; cin >> s; if (f < 0) continue; int ln = s.length(); int ct = 0; for (k = ln - 1; k >= 0; --k) { if (s[k] == 'a' || s[k] == 'i' || s[k] == 'u' || s[k] == 'e' || s[k] == 'o') { ++ct; if (ct == m) break; } } if (k >= 0) { t[j] = s.substr(k); } else { f = -1; } } if (f < 0) continue; bool a[4][4]; for (j = 0; j < 3; ++j) for (k = j + 1; k < 4; ++k) a[j][k] = t[j] == t[k]; if (a[0][1] && a[2][3] && !a[1][2]) { if (f == 2 || f == 3) f = -1; else f = 1; continue; } if (a[0][1] && a[2][3] && a[1][2]) { continue; } if (a[0][3] && a[1][2] && !a[0][1]) { if (f == 1 || f == 3) f = -1; else f = 2; continue; } if (a[0][2] && a[1][3] && !a[0][1]) { if (f == 1 || f == 2) f = -1; else f = 3; continue; } f = -1; } if (0) { } else if (f == 0) { cout << "aaaa" << endl; } else if (f == 1) { cout << "aabb" << endl; } else if (f == 2) { cout << "abba" << endl; } else if (f == 3) { cout << "abab" << endl; } else { cout << "NO" << endl; } } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static String[] list; static String type(String s[]){ if(s[0].equals(s[1]) && s[1].equals(s[2]) && s[2].equals(s[3])) return "aaaa"; if(s[0].equals(s[1]) && s[2].equals(s[3])) return "aabb"; if(s[0].equals(s[2]) && s[1].equals(s[3])) return "abab"; if(s[0].equals(s[3]) && s[1].equals(s[2])) return "abba"; return null; } static boolean isVolwer(char c){ return c == 'a' || c=='u'||c=='e'||c=='i' ||c =='o'; } public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer cin = new StringTokenizer(b.readLine()); int n = Integer.parseInt(cin.nextToken()), k = Integer.parseInt(cin.nextToken()); list = new String[n]; String [] s= new String[4]; for( int i = 0; i < n; i++){ for( int j = 0, K; j < 4; j++){ String tmp = b.readLine(); int can = 0; for( K = tmp.length() - 1; K >=0 && can != k; K--) if(isVolwer(tmp.charAt(K))) can++; if(can == k) s[j] = tmp.substring(K+1); else { System.out.println("NO"); System.exit(0); } } list[i] = type(s); if(list[i] == null){ System.out.println("NO"); System.exit(0); } } String res =list[0]; for(int i = 1; i < n; i++){ if(list[i].equals(list[i-1]) || list[i].equals("aaaa") || list[i-1].equals("aaaa")){ if(!list[i].equals("aaaa")) res = list[i]; continue; } System.out.println("NO"); System.exit(0); } System.out.println(res); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.*; import java.lang.*; public class Main { public static boolean rhymes(String[][] ap, int a, int b, int k) { int la = ap[a].length, lb = ap[b].length; //System.out.println(la); for(int i = la - k, j = lb - k; i < la && j < lb;) { if(i < 1 || j < 1) return false; //System.out.println(ap[a][i]+ " " + ap[b][j]); if(!ap[a][i].equals(ap[b][j])) return false; i++; j++; } //System.out.println("success"); return true; } public static boolean cle(String[][] ap, int k) { return rhymes(ap, 0, 1, k) && rhymes(ap, 2, 3, k); } public static boolean alt(String[][] ap, int k) { return rhymes(ap, 0, 2, k) && rhymes(ap, 1, 3, k); } public static boolean enc(String[][] ap, int k) { return rhymes(ap, 0, 3, k) && rhymes(ap, 1, 2, k); } public static boolean testAll(String[][] ap, int k) { for(int i = 0; i < 4; i++) for(int j = i + 1; j < 4; j++) if(!rhymes(ap, i, j, k)) return false; return true; } public static String getPattern(String[][] ap, int k) { if(testAll(ap, k)) return "aaaa"; if(cle(ap, k)) return "aabb"; if(alt(ap, k)) return "abab"; if(enc(ap, k)) return "abba"; return "NO"; } public static void main (String[] args) throws java.lang.Exception { java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); String s = r.readLine(); String[] sp = s.split(" "); int n = Integer.parseInt(sp[0]); int k = Integer.parseInt(sp[1]); String ret = "aaaa"; for(int i = 0; i < n; i++) { String[][] ap = new String[4][]; for(int j = 0; j < 4; j++) { String rl = "c" + r.readLine() + "c"; String vwl = rl.replaceAll("[bcdfghjklmnpqrstvwxyz]", ""); ap[j] = rl.split("[aeiou]"); for(int p = 1; p < ap[j].length; p++) ap[j][p] = vwl.charAt(p - 1) + ap[j][p]; } String pat = getPattern(ap, k); //System.out.println(pat); if(!ret.matches(pat) && ret.matches("aaaa") && !pat.matches("aaaa")) ret = pat; else if(!ret.matches(pat) && !ret.matches("aaaa") && !pat.matches("aaaa")) ret = "NO"; //System.out.println(ret); } System.out.println(ret); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; vector<int> aux; int n, k; char m[100][150]; bool vogal(char a) { return (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u'); } bool igual(string a, int ida, string b, int idb) { if (a.length() - ida != b.length() - idb) return false; for (int i = ida; i < a.length(); i++) if (a[i] != b[i - ida + idb]) return false; return true; } int f(string a, string b, string c, string d) { int aa = 0, bb = 0, cc = 0, dd = 0; int ia, ib, ic, id; for (ia = a.length() - 1; ia >= 0 && aa < k; ia--) if (vogal(a[ia])) aa++; for (ib = b.length() - 1; ib >= 0 && bb < k; ib--) if (vogal(b[ib])) bb++; for (ic = c.length() - 1; ic >= 0 && cc < k; ic--) if (vogal(c[ic])) cc++; for (id = d.length() - 1; id >= 0 && dd < k; id--) if (vogal(d[id])) dd++; if (aa != k || bb != k || cc != k || dd != k) return -1; ia++, ib++, ic++, id++; if (igual(a, ia, b, ib) && igual(a, ia, c, ic) && igual(a, ia, d, id)) return 4; if (igual(a, ia, b, ib) && igual(c, ic, d, id)) return 1; if (igual(a, ia, c, ic) && igual(b, ib, d, id)) return 2; if (igual(a, ia, d, id) && igual(b, ib, c, ic)) return 3; return -1; } int main() { string a, b, c, d; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { cin >> a >> b >> c >> d; aux.push_back(f(a, b, c, d)); } int q = 0, w = 0, e = 0, r = 0; bool ff = true; for (int i = 0; i < n; i++) { if (aux[i] == -1) { ff = false; break; } if (aux[i] == 1) q++; else if (aux[i] == 2) w++; else if (aux[i] == 3) e++; else r++; } if (ff == false) printf("NO\n"); else if (q == 0 && w == 0 && e == 0) printf("aaaa\n"); else if (w == 0 && e == 0) printf("aabb\n"); else if (q == 0 && w == 0) printf("abba\n"); else if (q == 0 && e == 0) printf("abab\n"); else printf("NO\n"); return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; string S1[10002], an[] = {"aabb", "abab", "abba", "aaaa", "NO"}, S; int ar[5], n, m; int main() { cin >> n >> m; for (int i = 0; i < 4 * n; i++) { int l = 0, k; cin >> S; for (k = S.size(); k >= 0 && l != m;) { k--; if (S[k] == 'a' || S[k] == 'e' || S[k] == 'i' || S[k] == 'o' || S[k] == 'u') l++; } if (l != m) ar[4]++; if (k == -1) k = 0; S1[i] = S.substr(k); } for (int i = 0; i < 4 * n; i += 4) { if (S1[i] == S1[i + 1] && S1[i + 1] == S1[i + 2] && S1[i + 2] == S1[i + 3]) ar[3]++; if (S1[i] == S1[i + 1] && S1[i + 2] == S1[i + 3]) ar[0]++; if (S1[i] == S1[i + 2] && S1[i + 1] == S1[i + 3]) ar[1]++; if (S1[i] == S1[i + 3] && S1[i + 1] == S1[i + 2]) ar[2]++; } if (ar[4]) cout << an[4] << endl; else { int t = 0, k1 = -1; for (int i = 0; i < 4; i++) if (ar[i] == n) k1 = i; if (k1 != -1) cout << an[k1] << endl; else cout << an[4] << endl; } }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const long double EPS = 1E-9; const int INF = (int)1E9; const long long INF64 = (long long)1E18; int n, k; bool check1(string a, string b, string c, string d) { string p, q, r, s; int t = 0; for (int i = a.size() - 1; i >= 0; i--) { if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') t++; p = p + a[i]; if (t == k) { break; } } if (t != k) return 0; t = 0; for (int i = b.size() - 1; i >= 0; i--) { if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') t++; q = q + b[i]; if (t == k) { break; } } if (t != k) return 0; t = 0; for (int i = c.size() - 1; i >= 0; i--) { if (c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o' || c[i] == 'u') t++; r = r + c[i]; if (t == k) { break; } } if (t != k) return 0; t = 0; for (int i = d.size() - 1; i >= 0; i--) { if (d[i] == 'a' || d[i] == 'e' || d[i] == 'i' || d[i] == 'o' || d[i] == 'u') t++; s = s + d[i]; if (t == k) { break; } } if (t != k) return 0; t = 0; if (p == q && q == r && r == s) return 1; return 0; } bool check(string a, string b) { string p, q; int t = 0; for (int i = a.size() - 1; i >= 0; i--) { if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') t++; p = p + a[i]; if (t == k) { break; } } if (t != k) return 0; t = 0; for (int i = b.size() - 1; i >= 0; i--) { if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') t++; q = q + b[i]; if (t == k) { break; } } if (t != k) return 0; t = 0; if (p != q) return 0; return 1; } int main() { cin >> n >> k; string rhy = "aaaa"; for (int i = 0; i < (int)(n); i++) { string ar[4]; for (int j = 0; j < (int)(4); j++) { cin >> ar[j]; } string temp; if (check1(ar[0], ar[1], ar[2], ar[3])) continue; else if (check(ar[0], ar[1]) && check(ar[2], ar[3])) temp = "aabb"; else if (check(ar[0], ar[2]) && check(ar[1], ar[3])) temp = "abab"; else if (check(ar[0], ar[3]) && check(ar[2], ar[1])) temp = "abba"; if (temp.size() == 0) { cout << "NO"; return 0; } if (rhy == "aaaa") rhy = temp; else if (rhy != temp) { cout << "NO"; return 0; } } cout << rhy; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; set<char> V = {'a', 'e', 'i', 'o', 'u'}; bool match(string& a, string& b, int k) { int i = a.size() - 1, j = b.size() - 1; while (i >= 0 && j >= 0) { if (a[i] != b[j]) return 0; if (V.count(a[i]) && --k == 0) return 1; i--, j--; } return 0; } int main() { ios::sync_with_stdio(0); int n, k; cin >> n >> k; bitset<4> b(0); while (n-- && b.count() != 4) { string a[4]; for (int i = 0; i < 4; i++) cin >> a[i]; if (match(a[0], a[1], k) && match(a[0], a[2], k) && match(a[0], a[3], k)) continue; b[0] = 1; if (match(a[0], a[1], k)) { if (!b[1] && !match(a[2], a[3], k)) b[1] = 1; b[2] = b[3] = 1; } else if (match(a[0], a[2], k)) { if (!b[2] && !match(a[1], a[3], k)) b[2] = 1; b[1] = b[3] = 1; } else if (match(a[0], a[3], k)) { if (!b[3] && !match(a[1], a[2], k)) b[3] = 1; b[2] = b[1] = 1; } else b[1] = b[2] = b[3] = 1; } if (!b[0]) cout << "aaaa\n"; else if (!b[1]) cout << "aabb\n"; else if (!b[2]) cout << "abab\n"; else if (!b[3]) cout << "abba\n"; else cout << "NO\n"; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const int maxi = 1e6 + 10; int a[maxi]; string s[maxi]; int n, k; vector<int> v[maxi]; int vowel(char x) { return x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'; } int rima(int x, int y) { int n1 = s[x].size(); int n2 = s[y].size(); int pozX, pozY; int vx = 0, vy = 0; for (int i = n1 - 1; i >= 0; i--) { vx += vowel(s[x][i]); if (vx == k) { pozX = i; break; } } for (int i = n2 - 1; i >= 0; i--) { vy += vowel(s[y][i]); if (vy == k) { pozY = i; break; } } if (n1 - pozX != n2 - pozY) return 0; for (int i = 0; i < n1 - pozX; i++) if (s[x][i + pozX] != s[y][i + pozY]) return 0; return 1; } int main() { cin >> n >> k; n *= 4; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) { int vowels = 0; for (int j = 0; j < s[i].size(); j++) vowels += vowel(s[i][j]); if (vowels < k) return !printf("NO\n"); } int ok = 1; for (int i = 1; i < n; i += 4) { if (!rima(i, i + 1) || !rima(i, i + 2) || !rima(i, i + 3) || !rima(i + 1, i + 2) || !rima(i + 1, i + 3) || !rima(i + 2, i + 3)) ok = 0; } if (ok) return !printf("aaaa\n"); ok = 1; for (int i = 1; i < n; i += 4) { if (!rima(i, i + 1) || !rima(i + 2, i + 3)) ok = 0; } if (ok) return !printf("aabb\n"); ok = 1; for (int i = 1; i < n; i += 4) { if (!rima(i, i + 2) || !rima(i + 1, i + 3)) ok = 0; } if (ok) return !printf("abab\n"); ok = 1; for (int i = 1; i < n; i += 4) { if (!rima(i, i + 3) || !rima(i + 1, i + 2)) ok = 0; } if (ok) return !printf("abba\n"); return !printf("NO\n"); }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; string lines[10001]; string pieces[10001]; int n, k; char v[] = {'a', 'e', 'i', 'o', 'u'}; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> lines[4 * i] >> lines[4 * i + 1] >> lines[4 * i + 2] >> lines[4 * i + 3]; } set<char> vowels(v, v + 5); for (int i = 0; i < 4 * n; i++) { int j = 0, l = lines[i].size() - 1; while (l >= 0 && j < k) { if ((vowels).find(lines[i][l]) != (vowels).end()) { j++; } if (j < k) l--; } if (l < 0) { pieces[i] = "."; } else { pieces[i] = lines[i].substr(l); } } bool clerihew = true, alternating = true, enclosed = true; for (int i = 0; i < n; i++) { bool ok = true; if (pieces[4 * i] == "." || pieces[4 * i + 1] == "." || pieces[4 * i + 2] == "." || pieces[4 * i + 3] == ".") ok = false; clerihew = clerihew && ok && pieces[4 * i] == pieces[4 * i + 1] && pieces[4 * i + 2] == pieces[4 * i + 3]; alternating = alternating && ok && pieces[4 * i] == pieces[4 * i + 2] && pieces[4 * i + 1] == pieces[4 * i + 3]; enclosed = enclosed && ok && pieces[4 * i] == pieces[4 * i + 3] && pieces[4 * i + 1] == pieces[4 * i + 2]; } if (clerihew && alternating && enclosed) { cout << "aaaa" << endl; } else { if (clerihew) { cout << "aabb" << endl; } else if (alternating) { cout << "abab" << endl; } else if (enclosed) { cout << "abba" << endl; } else { cout << "NO" << endl; } } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
/** * Jerry Ma * Program lit */ import java.io.*; import java.util.*; public class lit { static BufferedReader cin; static StringTokenizer tk; static char [] vowels = {'a','e','i','o','u'}; public static void main (String [] args) throws IOException { init(); int num = gInt(), vowelPos = gInt(); String rhymeScheme = "aaaa"; for (int count = 0; count < num; count ++) { String a = suffix(token(), vowelPos), b = suffix(token(), vowelPos), c = suffix(token(), vowelPos), d = suffix(token(), vowelPos); if (a == null || b == null || c == null || d == null) { rhymeScheme = "NO"; break; } String curScheme = null; if (a.equals(b) && b.equals(c) && c.equals(d)) curScheme = "aaaa"; else if (a.equals(b) && c.equals(d)) curScheme = "aabb"; else if (a.equals(c) && b.equals(d)) curScheme = "abab"; else if (a.equals(d) && b.equals(c)) curScheme = "abba"; else { rhymeScheme = "NO"; break; } if (rhymeScheme.equals("aaaa")) rhymeScheme = curScheme; else if (curScheme.equals("aaaa")); else if (!rhymeScheme.equals(curScheme)) { rhymeScheme = "NO"; break; } } System.out.println(rhymeScheme); exit(); } public static String suffix (String orig, int vPos) { int numVowels = 0; for (int count = orig.length() - 1; count > -1; count --) { char c = orig.charAt(count); boolean isVowel = false; for (char c2 : vowels) { if (c == c2) isVowel = true; } if (isVowel) numVowels ++; if (numVowels >= vPos) return orig.substring(count, orig.length()); } return null; } public static void init () { cin = new BufferedReader(new InputStreamReader(System.in)); tk = new StringTokenizer(""); } public static void exit () throws IOException { System.out.flush(); System.exit(0); } public static String token () throws IOException { while (!tk.hasMoreTokens()) tk = new StringTokenizer(cin.readLine()); return tk.nextToken(); } public static int gInt () throws IOException { return Integer.parseInt(token()); } public static long gLong () throws IOException { return Long.parseLong(token()); } public static double gDouble () throws IOException { return Double.parseDouble(token()); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.HashMap; import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Alex */ public class _139C { private static String getSuffix(String a, int k) { String res = ""; StringBuilder temp = new StringBuilder(); int count = k; b: for (int i = a.length() - 1; i >= 0; i--) { char aChar = a.charAt(i); res = aChar + res; if (aChar == 'a' || aChar == 'e' || aChar == 'i' || aChar == 'o' || aChar == 'u') { count--; if (count <= 0) { break b; } } } return res; } private static String getRifm(String a1, String a2, String a3, String a4, int k) { String res = "NO"; if (k > a1.length()) { return "NO"; } // String a1Suff = a1.substring(a1.length() - k); // String a2Suff = a2.substring(a2.length() - k); // String a3Suff = a3.substring(a3.length() - k); // String a4Suff = a4.substring(a4.length() - k); String a1Suff = getSuffix(a1, k); String a2Suff = getSuffix(a2, k); String a3Suff = getSuffix(a3, k); String a4Suff = getSuffix(a4, k); if (a1Suff.equals(a2Suff) && a3Suff.equals(a4Suff) && a1Suff.equals(a3Suff)) { res = "aaaa"; } else { if (a1Suff.equals(a2Suff) && a3Suff.equals(a4Suff)) { res = "aabb"; } if (a1Suff.equals(a3Suff) && a2Suff.equals(a4Suff)) { res = "abab"; } if (a1Suff.equals(a4Suff) && a2Suff.equals(a3Suff)) { res = "abba"; } } return res; } public static void main(String... args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); int count = 0; String type = ""; c: for (int i = 0; i < n; i++) { type = getRifm(s.next(), s.next(), s.next(), s.next(), k); count++; if (!type.equals("aaaa")) { break c; } } String tempType = ""; b: if (!type.equals("NO")) { for (int i = count; i < n; i++) { tempType = getRifm(s.next(), s.next(), s.next(), s.next(), k); if (!tempType.equals("aaaa") && !tempType.equals(type)) { System.out.print("NO"); break b; } } System.out.print(type); } else { System.out.print("NO"); } } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const int N = 100005; string s[N], suf[N]; string ans[4] = {"aaaa", "aabb", "abab", "abba"}; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { for (int j = (i - 1) * 4 + 1; j <= i * 4; ++j) { cin >> s[j]; } } int f = -1; for (int i = 1; i <= n && f; ++i) { for (int j = (i - 1) * 4 + 1; j <= i * 4 && f; ++j) { int sz = s[j].size(), t = k; for (int a = sz - 1; a >= 0; --a) { suf[j] += s[j][a]; if (s[j][a] == 'a' || s[j][a] == 'e' || s[j][a] == 'i' || s[j][a] == 'o' || s[j][a] == 'u') { --t; } if (t <= 0) break; } if (t > 0) f = 0; } int pos = (i - 1) * 4, ff = -1; if (suf[pos + 1] == suf[pos + 2] && suf[pos + 2] == suf[pos + 3] && suf[pos + 3] == suf[pos + 4]) { ff = 1; } else if (suf[pos + 1] == suf[pos + 2] && suf[pos + 2] != suf[pos + 3] && suf[pos + 3] == suf[pos + 4]) { ff = 2; } else if (suf[pos + 1] == suf[pos + 3] && suf[pos + 2] == suf[pos + 4] && suf[pos + 1] != suf[pos + 2]) { ff = 3; } else if (suf[pos + 1] == suf[pos + 4] && suf[pos + 2] == suf[pos + 3] && suf[pos + 1] != suf[pos + 2]) { ff = 4; } if (ff == -1) f = 0; else { if (f == -1) f = ff; else if (f == 1) f = ff; else if (ff == 1) f = f; else if (f != ff) f = 0; } } if (f) cout << ans[f - 1] << endl; else cout << "NO" << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; bool pro(char c) { return (c == 'a' || c == 'i' || c == 'o' || c == 'e' || c == 'u'); } long long n, k; bool ttt(string a, string b) { long long t = min(a.length(), b.length()), i = 1, u = 0; while (i <= t) { if (a[a.length() - i] == b[b.length() - i]) { if (pro(a[a.length() - i])) u++; i++; } else break; } if (u >= k) return 1; else return 0; } int main() { long long i, j, m, t, rec = 4; string a, b, c, d, e[4] = {"aabb", "abab", "abba", "aaaa"}; cin >> n >> k; for (i = 0; i < n; i++) { long long re; cin >> a; cin >> b; cin >> c; cin >> d; if (ttt(a, b)) if (ttt(c, d)) if (ttt(a, c)) re = 4; else re = 1; else re = 0; else if (ttt(b, c)) if (ttt(a, d)) re = 3; else re = 0; else if (ttt(b, d)) if (ttt(a, c)) re = 2; else re = 0; else re = 0; if (re != 4) { if (re == 0) rec = 0; else if (rec == 4) rec = re; else if (rec != re) rec = 0; } } if (rec == 0) printf("NO"); else cout << e[rec - 1]; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30, N = 1002; int ax[] = {0, 1, -1, 0, 0}; int ay[] = {0, 0, 0, -1, 1}; string a[10002]; bool rifma(int first, int second, int k) { int n = a[first].size() - 1; int m = a[second].size() - 1; while (true) { if (a[first][n] != a[second][m]) return false; if (a[first][n] == 'a' || a[first][n] == 'e' || a[first][n] == 'i' || a[first][n] == 'o' || a[first][n] == 'u') { k--; } if (k == 0) { return true; } n--; m--; if (n == -1 || m == -1) { return false; } } return true; } void upd(int z, int &now) { if (now == 0) { now = z; return; } if (now == -1) { now = z; return; } if (z == -1) { return; } if (now != z) { cout << "NO"; exit(0); } if (now == z) return; } int main() { int i, j, k, n; cin >> n >> k; n *= 4; for (i = 1; i <= n; i++) cin >> a[i]; int now = 0; for (i = 1; i <= n; i += 4) { if (rifma(i, i + 1, k) && rifma(i + 2, i + 3, k) && rifma(i, i + 3, k)) { upd(-1, now); continue; } if (rifma(i, i + 1, k) && rifma(i + 2, i + 3, k)) { upd(1, now); continue; } if (rifma(i, i + 2, k) && rifma(i + 1, i + 3, k)) { upd(2, now); continue; } if (rifma(i, i + 3, k) && rifma(i + 1, i + 2, k)) { upd(3, now); continue; } cout << "NO"; return 0; } if (now == 1) cout << "aabb"; if (now == 2) cout << "abab"; if (now == 3) cout << "abba"; if (now == -1) cout << "aaaa"; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); String cur = ""; String tmp; int cnt = 0; String s[] = new String[4]; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { tmp = in.next(); cnt = 0; int x = -1; for (x = tmp.length() - 1; x >= 0; x--) { if (vo(tmp.charAt(x))) cnt++; if (cnt == k) break; } if (cnt != k) { System.out.println("NO"); System.exit(0); } s[j] = tmp.substring(x); } tmp = getCur(s); if (tmp == null) { System.out.println("NO"); System.exit(0); } if (cur.equals("aaaa") || cur.equals("")) cur = tmp; else if (!cur.equals(tmp) && !tmp.equals("aaaa")) { System.out.println("NO"); System.exit(0); } } System.out.println(cur); } public static String getCur(String[] a) { if (a[0].equals(a[1]) && a[0].equals(a[2]) && a[0].equals(a[3])) return "aaaa"; if (a[0].equals(a[2]) && a[1].equals(a[3])) return "abab"; if (a[0].equals(a[3]) && a[1].equals(a[2])) return "abba"; if (a[0].equals(a[1]) && a[2].equals(a[3])) return "aabb"; return null; } public static boolean vo(char c) { return c == 'a' || c == 'e' || c == 'o' || c == 'u' || c == 'i'; } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3fLL; const double err = 1e-9; const int mod = 1e9 + 7; const int N = 1e5 + 10; ll n, k; bool isVowel(char c) { return c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k; int types[N]; map<string, int> mp; mp["aaaa"] = 1; mp["aabb"] = 2; mp["abab"] = 3; mp["abba"] = 4; for (int(i) = (0); (i) < (n); (i)++) { vector<string> str; string s; for (int(j) = (0); (j) < (4); (j)++) { cin >> s; int vwl = 0; for (int ki = (int)(s).size() - 1; ki >= 0; ki--) { if (isVowel(s[ki])) vwl++; if (vwl == k) { s = s.substr(ki); break; } if (ki == 0) { cout << "NO\n"; return 0; } } str.push_back(s); } if (str[0] == str[1] and str[1] == str[2] and str[2] == str[3]) types[i] = mp["aaaa"]; else if (str[0] == str[2] and str[1] == str[3]) types[i] = mp["abab"]; else if (str[0] == str[3] and str[1] == str[2]) types[i] = mp["abba"]; else if (str[0] == str[1] and str[2] == str[3]) types[i] = mp["aabb"]; } int gt = -1; for (int(i) = (0); (i) < (n); (i)++) { int t = types[i]; if (t == 0) { gt = 0; break; } if (t == 1 and gt == -1) gt = 1; if (t > 1 and (gt > 1 and t != gt)) { gt = 0; break; } if (t > 1 and (gt < 2 or gt == t)) gt = t; } if (!gt) cout << "NO\n"; if (gt == 1) cout << "aaaa\n"; if (gt == 2) cout << "aabb\n"; if (gt == 3) cout << "abab\n"; if (gt == 4) cout << "abba\n"; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, j, l; string s, t, r, z, u, p[5], q; char c; cin >> n >> k; getline(cin, z); for (i = 1; i <= n; i++) { t = " "; r = ""; c = 'a'; for (l = 1; l <= 4; l++) { getline(cin, z); j = z.length() - 1; q = s = ""; while (j >= 0 && s.length() < k) { q += z[j]; if (z[j] == 'a' || z[j] == 'e' || z[j] == 'i' || z[j] == 'o' || z[j] == 'u') s += z[j]; j--; } if (s.length() < k) { cout << "NO"; return 0; } p[l] = q; for (j = 1; j < l; j++) { if (p[j] == q) { t[l - 1] = t[j - 1]; break; } } if (j == l) t[l - 1] = c, c++; } if (i == 1 || u == "aaaa") u = t; if (u != t && t != "aaaa") { cout << "NO"; return 0; } } if (u != "aaaa" && u != "abab" && u != "aabb" && u != "abba") { cout << "NO"; return 0; } cout << u; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; string getBack(string s, int k) { int p = s.size(); for (int i = 0; i < k; i++) { int ap = s.substr(0, p).find_last_of("a"); int ep = s.substr(0, p).find_last_of("e"); int ip = s.substr(0, p).find_last_of("i"); int op = s.substr(0, p).find_last_of("o"); int up = s.substr(0, p).find_last_of("u"); p = max(ap, max(ep, max(ip, max(op, up)))); if (p == -1) { return ""; } } return s.substr(p, s.size()); } string s[10001]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < 4 * n; i++) { string ss; cin >> ss; s[i] = getBack(ss, k); } vector<int> code; for (int i = 0; i < n; i++) { int sp = 4 * i; bool ab[4] = {false}; for (int j = 0; j < 4; j++) { ab[j] = (s[sp] == s[sp + j] && s[sp + j] != "" && s[sp] != ""); } if (ab[0] == true && ab[1] == true && ab[2] == true && ab[3] == true) { code.push_back(0); } else { if (ab[0] == true && ab[1] == false && ab[2] == false && ab[3] == true || (ab[0] == false && ab[1] == true && ab[2] == false && ab[3] == true) && s[sp + 1] == s[sp + 2]) { code.push_back(1); } else { if (ab[0] == true && ab[1] == true && ab[2] == false && ab[3] == false || (ab[0] == false && ab[1] == false && ab[2] == true && ab[3] == true) && s[sp + 2] == s[sp + 3]) { code.push_back(2); } else { if ((ab[0] == true && ab[1] == false && ab[2] == true && ab[3] == false || (ab[0] == false && ab[1] == true && ab[2] == false && ab[3] == true)) && s[sp + 1] == s[sp + 3]) { code.push_back(3); } else { code.push_back(-1); } } } } } sort(code.begin(), code.end()); for (int i = 0; i < code.size() - 1; i++) { if (code[i] != 0 && code[i + 1] != code[i]) { cout << "NO"; return 0; } } if (code[code.size() - 1] == 0) { cout << "aaaa"; } else { if (code[code.size() - 1] == 1) { cout << "abba"; } else { if (code[code.size() - 1] == 2) { cout << "aabb"; } else { if (code[code.size() - 1] == 3) { cout << "abab"; } else { cout << "NO"; } } } } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.HashSet; import java.util.Scanner; /** * * @author Eslam Ashraf */ public class C { static HashSet<Integer> set = new HashSet<Integer>(); public static void main(String[] args) { set.add('a' + 0); set.add('e' + 0); set.add('i' + 0); set.add('o' + 0); set.add('u' + 0); Scanner in = new Scanner(System.in); String[] s = in.nextLine().trim().split(" +"); int n = Integer.parseInt(s[0]); int k = Integer.parseInt(s[1]); int[] pattern = new int[n]; // 0 no , 1 aaaa , 2 aabb , 3 abab , 4 abba int common =-1; int i =0; for (i = 0; i < n; i++) { String a = in.nextLine(); String b = in.nextLine(); String c = in.nextLine(); String d = in.nextLine(); //check string a int indexA = check(a, k); if (indexA == -1){pattern[i] = 0; break;} String sufixA = a.substring(indexA); int len1 = sufixA.length(); if (b.length() - len1 >=0 && sufixA.equals(b.substring(b.length()-len1))) { //aabb , aaaa , no if(c.length()-len1 >=0 && d.length()-len1>=0 && sufixA.equals(c.substring(c.length()-len1)) && sufixA.equals(d.substring(d.length()-len1)))pattern[i]=1; else { int indexC = check(c , k); if(indexC==-1){pattern[i]=0 ; break ;} String sufixC = c.substring(indexC); int len2 = sufixC.length(); if(d.length() - len2 >=0 && sufixC.equals(d.substring(d.length()-len2)))pattern [i]=2; else pattern [i]=0; } } else if (c.length()- len1 >=0 && sufixA.equals(c.substring(c.length()-len1))) { //abab , no int indexB = check(b , k); if(indexB==-1){pattern[i]=0 ; break ;} String sufixB = b.substring(indexB); int len3 = sufixB.length(); if(d.length() - len3 >=0 && sufixB.equals(d.substring(d.length()-len3)))pattern [i]=3; else pattern [i]=0; } else if (d.length() - len1 >=0 && sufixA.equals(d.substring(d.length()-len1))) { //abba , no int indexB = check(b , k); if(indexB==-1){pattern[i]=0 ; break;} String sufixB = b.substring(indexB); int len3 = sufixB.length(); if(c.length()- len3 >=0 && sufixB.equals(c.substring(c.length()-len3)))pattern [i]=4; else pattern [i]=0; } else { pattern [i]=0; } if(pattern[i]==0)break; if(pattern[i]==1 )continue; if(pattern[i]==common)continue; else{ if(common ==-1)common = pattern[i]; else { pattern [i]=0 ; break;} } } // 0 no , 1 aaaa , 2 aabb , 3 abab , 4 abba if(i<n && pattern[i]==0)System.out.println("NO"); else{ if(common == 2){System.out.println("aabb");} else if(common == 3){System.out.println("abab");} else if(common == 4){System.out.println("abba");} else System.out.println("aaaa"); } } private static int check(String a, int k) { int c = 0; for (int i = a.length() - 1; i >= 0; i--) { if (set.contains(a.charAt(i) + 0)) c++; if (c == k) return i; } return -1; } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.*; import java.math.*; public class main { public static void main(String args[]) { Scanner c=new Scanner(System.in); int N=c.nextInt(); int K=c.nextInt(); String P[][]=new String [N][4]; for(int i=0;i<N;i++) { for(int j=0;j<4;j++) { String s=c.next(); int l=s.length(); int vow=0; int k; for(k=l-1;k>=0;k--) { char c1=s.charAt(k); if(c1=='a'||c1=='e'||c1=='i'||c1=='o'||c1=='u') vow++; if(vow==K) break; } //System.out.println(vow+" "+k); if(vow<K) { System.out.println("NO"); return; } else { P[i][j]=s.substring(k,l); //System.out.println(P[i][j]); } } } //poem init ends //check aaaa //System.out.println("aa".compareTo("ab")); boolean ok=true; for(int i=0;i<N;i++) { for(int j=0;j<4;j++) { for(int k=j+1;k<4;k++) { //System.out.println("comparing "+P[i][j]+" and "+P[i][k]); if(P[i][j].compareTo(P[i][k])!=0) ok=false; } } if(!ok) break; } if(ok) { System.out.println("aaaa"); return; } //check abab //System.out.println("aa".compareTo("ab")); ok=true; for(int i=0;i<N;i++) { if(P[i][0].compareTo(P[i][2])!=0||P[i][1].compareTo(P[i][3])!=0) { ok=false; break; } } if(ok) { System.out.println("abab"); return; } //check aabb //System.out.println("aa".compareTo("ab")); ok=true; for(int i=0;i<N;i++) { if(P[i][0].compareTo(P[i][1])!=0||P[i][2].compareTo(P[i][3])!=0) { ok=false; break; } } if(ok) { System.out.println("aabb"); return; } //check abba //System.out.println("aa".compareTo("ab")); ok=true; for(int i=0;i<N;i++) { if(P[i][0].compareTo(P[i][3])!=0||P[i][2].compareTo(P[i][1])!=0) { ok=false; break; } } if(ok) { System.out.println("abba"); return; } System.out.println("NO"); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; vector<int> room; string str[4]; int check(char x) { if (x == 'a' || x == 'e' || x == 'i' || x == 'u' || x == 'o') return 1; return 0; } int main() { int n, m; scanf("%d%d", &n, &m); int ret = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { cin >> str[j]; string tmp = ""; bool f = 0; for (int k = ((str[j]).size()) - 1, s = 0; k >= 0; k--) { tmp += str[j][k]; if (check(str[j][k])) s++; if (s != m) continue; str[j] = tmp; f = 1; break; } if (f == 0) ret = -1; } int a = 0, b = 0; for (int k = 1; k < 4; k++) if (str[k] != str[0]) { if (a == 0) a = k; else if (b == 0) b = k; else ret = -1; } if (a == 0 && b == 0) continue; if (a && b == 0) ret = -1; if (str[a] != str[b]) ret = -1; if (ret == 0) ret = (1 << a) | (1 << b); else if (ret != ((1 << a) | (1 << b))) ret = -1; } if (ret == -1) cout << "NO"; else for (int i = 0; i < 4; i++) if (ret & (1 << i)) cout << "b"; else cout << "a"; cout << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; void nope() { cout << "NO"; exit(0); } int main() { set<string> res; int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { string sp[4]; for (int j = 0; j < 4; j++) { string s; cin >> s; int p = s.size(); int c = 0; while (p >= 0 && c < k) { char ch = s[--p]; if (ch == 'a' || ch == 'e' || ch == 'u' || ch == 'i' || ch == 'o') c++; } if (c < k) { nope(); } sp[j] = s.substr(p); } if (sp[0] == sp[1]) { if (sp[2] == sp[3]) { if (sp[1] == sp[2]) { res.insert("aaaa"); } else { res.insert("aabb"); } } else { nope(); } } else { if (sp[0] == sp[2]) { if (sp[1] == sp[3]) { res.insert("abab"); } else { nope(); } } else { if (sp[0] == sp[3]) { if (sp[2] == sp[1]) { res.insert("abba"); } else { nope(); } } else { nope(); } } } } if (res.size() > 1) { if (res.size() == 2 && *res.begin() == "aaaa") { res.erase(res.begin()); } else { nope(); } } cout << *res.begin() << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 10010; string s1[maxn]; string check(string t1, string t2, string t3, string t4) { if (t1 == t2 && t3 == t4 && t1 == t3) return "aaaa"; if (t1 == t3 && t2 == t4 && t1 != t2) return "abab"; if (t1 == t4 && t2 == t3 && t1 != t2) return "abba"; if (t1 == t2 && t3 == t4 && t1 != t3) return "aabb"; return "err"; } int main() { int n, l; cin >> n >> l; for (int i = 0; i < n * 4; i++) cin >> s1[i]; bool aaaa = false, aabb = false, abab = false, abba = false; for (int i = 0; i < n; i++) { string t1, t2, t3, t4; for (int j = 0; j < 4; j++) { int cnt = 0; for (int k = s1[4 * i + j].size() - 1; k >= 0; k--) { cnt += (s1[4 * i + j][k] == 'a' || s1[4 * i + j][k] == 'e' || s1[4 * i + j][k] == 'i' || s1[4 * i + j][k] == 'o' || s1[4 * i + j][k] == 'u'); if (cnt == l) { if (j == 0) t1 = s1[4 * i + j].substr(k, s1[4 * i + j].size() - k); if (j == 1) t2 = s1[4 * i + j].substr(k, s1[4 * i + j].size() - k); if (j == 2) t3 = s1[4 * i + j].substr(k, s1[4 * i + j].size() - k); if (j == 3) t4 = s1[4 * i + j].substr(k, s1[4 * i + j].size() - k); break; } } if (cnt < l) { cout << "NO" << endl; return 0; } } string tmp = check(t1, t2, t3, t4); if (tmp == "aaaa") aaaa = true; if (tmp == "aabb") aabb = true; if (tmp == "abab") abab = true; if (tmp == "abba") abba = true; if (tmp == "err") { cout << "NO" << endl; return 0; } } if ("aaaa") { if (!aabb && !abab && !abba) cout << "aaaa" << endl; else if (aabb && !abab && !abba) cout << "aabb" << endl; else if (!aabb && abab && !abba) cout << "abab" << endl; else if (!aabb && !abab && abba) cout << "abba" << endl; else cout << "NO" << endl; return 0; } else { if (aabb && !abab && !abba) cout << "aabb" << endl; else if (!aabb && abab && !abba) cout << "abab" << endl; else if (!aabb && !abab && abba) cout << "abba" << endl; else cout << "NO" << endl; return 0; } cout << "NO" << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int mode(char a[], char b[], char c[], char d[]) { if (strcmp(a, b) == 0 && strcmp(c, d) == 0 && strcmp(b, c) != 0) return 1; else if (strcmp(a, b) == 0 && strcmp(c, d) == 0 && strcmp(a, c) == 0) return 0; else if (strcmp(a, c) == 0 && strcmp(b, d) == 0 && strcmp(a, b) != 0) return 2; else if (strcmp(b, c) == 0 && strcmp(a, d) == 0 && strcmp(a, b) != 0) return 3; else return 10; } char a[10050], b[10050], c[10050], d[10050]; int modecur, modeori; int main() { int quanum, vov; cin >> quanum >> vov; char s[10050]; modecur = 0, modeori = 0; int flag = 1, goon = 1; for (int i = 1; i <= quanum; i++) { scanf("%s", s); int len = strlen(s); int sumv = 0, pnt = 0; for (int j = len - 1; j >= 0; j--) { a[pnt++] = s[j]; if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') { sumv++; if (sumv == vov) break; } } if (sumv < vov) { flag = 0; goon = 0; } memset(s, 0, sizeof(s)); scanf("%s", s); len = strlen(s); sumv = 0, pnt = 0; for (int j = len - 1; j >= 0; j--) { b[pnt++] = s[j]; if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') { sumv++; if (sumv == vov) break; } } if (sumv < vov) { flag = 0; goon = 0; } memset(s, 0, sizeof(s)); scanf("%s", s); len = strlen(s); sumv = 0, pnt = 0; for (int j = len - 1; j >= 0; j--) { c[pnt++] = s[j]; if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') { sumv++; if (sumv == vov) break; } } if (sumv < vov) { flag = 0; goon = 0; } memset(s, 0, sizeof(s)); scanf("%s", s); len = strlen(s); sumv = 0, pnt = 0; for (int j = len - 1; j >= 0; j--) { d[pnt++] = s[j]; if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') { sumv++; if (sumv == vov) break; } } if (sumv < vov) { flag = 0; goon = 0; } if (mode(a, b, c, d) == 10) { flag = 0; } if (i == 1) { modeori = mode(a, b, c, d); modecur = modeori; } else modecur = mode(a, b, c, d); if (modeori != 0) { if (modecur != modeori && modecur != 0) { flag = 0; break; } } else if (modeori == 0 && i != 1) { if (modecur != 0) { modeori = modecur; } } } if (!flag) cout << "NO" << endl; else { if (modeori == 0) cout << "aaaa" << endl; else if (modeori == 1) cout << "aabb" << endl; else if (modeori == 2) cout << "abab" << endl; else if (modeori == 3) cout << "abba" << endl; } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; char str[4][10005]; int k; bool isV(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return 1; return 0; } int get_type() { int len[4], i, j, st[4], x; for (i = 0; i < 4; i++) { len[i] = strlen(str[i]); if (len[i] < k) return 0; for (j = len[i] - 1, x = 0; j >= 0 && x < k; j--) if (isV(str[i][j])) x++; if (x < k) return 0; st[i] = j + 1; } bool r[4][4]; int y; for (i = 0; i < 4; i++) for (j = i + 1; j < 4; j++) { if (len[i] - st[i] == len[j] - st[j]) { for (x = st[i], y = st[j]; x < len[i]; x++, y++) if (str[i][x] != str[j][y]) break; if (x == len[i]) r[i][j] = 1; else r[i][j] = 0; } else r[i][j] = 0; } if (r[0][1] && r[1][2] && r[2][3]) return 1; if (r[0][1] && r[2][3]) return 2; if (r[0][2] && r[1][3]) return 3; if (r[0][3] && r[1][2]) return 4; return 0; } int main() { int n, i, j, t, type; char s[5][8] = {"NO", "aaaa", "aabb", "abab", "abba"}; while (2 == scanf("%d%d", &n, &k)) { type = 1; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) scanf("%s", str[j]); if (type) t = get_type(); if (i == 0) type = t; else if (type && t != 1) { if (type == 1) type = t; else if (type != t) type = 0; } } puts(s[type]); } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; char c[5] = {'a', 'e', 'i', 'o', 'u'}; int r[4] = {0}; int flag = 0; for (int i = 0; i < n; i++) { vector<string> v; for (int i = 0; i < 4; i++) { string str, p = ""; cin >> str; int ctr = 0; for (int l = str.size() - 1; l >= 0 && flag == 0; l--) { p = p + str[l]; if (str[l] == c[0] || str[l] == c[1] || str[l] == c[2] || str[l] == c[3] || str[l] == c[4]) { ctr++; if (ctr == k) { v.push_back(p); break; } } } if (ctr != k) { flag = 1; } } if (flag == 0 && v.size() == 4) { if (v[0] == v[1] && v[1] == v[2] && v[2] == v[3]) r[3]++; else if (v[0] == v[1] && v[2] == v[3]) r[0]++; else if (v[0] == v[2] && v[1] == v[3]) r[1]++; else if (v[0] == v[3] && v[1] == v[2]) r[2]++; else flag = 1; } } if (flag == 1) cout << "NO" << endl; else if (r[0] == 0 && r[1] == 0 && r[2] != 0) cout << "abba" << endl; else if (r[0] == 0 && r[1] != 0 && r[2] == 0) cout << "abab" << endl; else if (r[0] != 0 && r[1] == 0 && r[2] == 0) cout << "aabb" << endl; else if (r[0] == 0 && r[1] == 0 && r[2] == 0 && r[3] != 0) cout << "aaaa" << endl; else cout << "NO" << endl; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
""" Literature Lesson """ import random def split_str(string, k): index = len(string) for char in string[::-1]: index -= 1 if char in 'aeiou': k -= 1 if k == 0: return string[index:] return string * random.randint(0, 100) def rhyme_schemes(quatrain, k): schemes = set() lines = [split_str(x, k) for x in quatrain] if (lines[0] == lines[1]) and (lines[2] == lines[3]): schemes.add('aabb') if lines[1] == lines[2]: schemes.add('aaaa') if (lines[1] == lines[3]) and (lines[2] == lines[0]): schemes.add('abab') if (lines[1] == lines[2]) and (lines[0] == lines[3]): schemes.add('abba') return schemes def main(): n, k = map(int, raw_input().split()) quatrains = [[raw_input().strip() for i in range(4)] for j in range(n)] result = reduce(lambda acc, x: acc & x, [rhyme_schemes(quat, k) for quat in quatrains]) if len(result): print result.pop() else: print "NO" if __name__ == '__main__': main()
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; int n, k; char a[4][100000]; int c[4]; int b[3000]; bool is(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; return false; } int i, j, m; int main() { scanf("%d%d", &n, &k); bool flag = true; for (i = 1; i <= n; i++) { for (j = 0; j < 4; j++) { scanf("%s", a[j]); int len = strlen(a[j]); int num = 0; for (int r = len - 1; r >= 0; r--) { if (is(a[j][r])) { num++; if (num == k) { c[j] = r; break; } } } if (num < k) flag = false; } if (flag) { if (strcmp(a[0] + c[0], a[1] + c[1]) == 0 && strcmp(a[1] + c[1], a[2] + c[2]) == 0 && strcmp(a[2] + c[2], a[3] + c[3]) == 0) b[i] = 0; else if (strcmp(a[0] + c[0], a[1] + c[1]) == 0 && strcmp(a[3] + c[3], a[2] + c[2]) == 0) b[i] = 1; else if (strcmp(a[0] + c[0], a[2] + c[2]) == 0 && strcmp(a[1] + c[1], a[3] + c[3]) == 0) b[i] = 2; else if (strcmp(a[0] + c[0], a[3] + c[3]) == 0 && strcmp(a[1] + c[1], a[2] + c[2]) == 0) b[i] = 3; else flag = false; } } int sta = 0; for (i = 1; i <= n; i++) { if (b[i] != 0 && sta == 0) sta = b[i]; if (b[i] != sta && b[i] != 0) { flag = false; } } if (!flag) printf("NO\n"); else if (sta == 1) printf("aabb\n"); else if (sta == 2) printf("abab\n"); else if (sta == 0) printf("aaaa\n"); else printf("abba\n"); return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import sys def finish(result): print(result) sys.exit() n, k = map(int, raw_input().split()) scheme_count = { 'aabb': 0, 'abab': 0, 'abba': 0, 'aaaa': 0 } def make_scheme(suffixes): if None in suffixes: return None suffix_hash = {} symbol = 'a' scheme_parts = [] for suffix in suffixes: if suffix not in suffix_hash: suffix_hash[suffix] = symbol symbol = chr(ord(symbol) + 1) scheme_parts.append(suffix_hash[suffix]) return ''.join(scheme_parts) for i in range(0, 4 * n, 4): suffixes = 4 * [ None ] for j in range(i, i + 4): s = raw_input().strip() count = 0 for pos in range(len(s) - 1, -1, -1): if s[pos] in 'aeiou': count += 1 if count == k: suffixes[j - i] = s[pos:] break scheme = make_scheme(suffixes) if scheme not in scheme_count: finish('NO') scheme_count[scheme] += 1 m = n - scheme_count['aaaa'] if m == 0: finish('aaaa') del scheme_count['aaaa'] for scheme, count in scheme_count.items(): if count == m: finish(scheme) finish('NO')
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class C99P3 { private static BufferedReader in; private static PrintWriter out; private static StringTokenizer input; public static void main(String[] args) throws IOException { //------------------------------------------------------------------------- //Initializing... new C99P3(); //------------------------------------------------------------------------- //put your code here... :) int ver = nextInt(); int k = nextInt(); String [][] poem = new String[ver][4]; boolean cant = false; for (int i = 0; i < poem.length; i++) { for (int j = 0; j < poem[i].length; j++) { poem[i][j]=adj(nextString(),k); if(poem[i][j]==null){cant=true;break;} } } String shy = "aaaa"; if(!cant){ for (int i = 0; i < poem.length; i++) { String s= sch(poem[i]); if(s==null){shy=null;break;} shy=merge(shy,s); if(shy==null)break; } if(shy==null)writeln("NO"); else writeln(shy); }else{ writeln("NO"); } //------------------------------------------------------------------------- //closing up end(); //-------------------------------------------------------------------------- } private static String merge(String shy, String s) { // TODO Auto-generated method stub if(shy.equals("aaaa"))return s; if(s.equals("aaaa"))return shy; if(s.equals(shy))return s; return null; } private static String sch(String[] s) { // TODO Auto-generated method stub if(aaaa(s))return "aaaa"; if(aabb(s))return "aabb"; if(abab(s))return "abab"; if(abba(s))return "abba"; return null; } private static boolean abba(String[] s) { // TODO Auto-generated method stub return s[0].equals(s[3])&&s[1].equals(s[2]); } private static boolean abab(String[] s) { // TODO Auto-generated method stub return s[0].equals(s[2])&&s[1].equals(s[3]); } private static boolean aabb(String[] s) { // TODO Auto-generated method stub return s[0].equals(s[1])&&s[2].equals(s[3]); } private static boolean aaaa(String[] s) { // TODO Auto-generated method stub return s[0].equals(s[1])&&s[1].equals(s[2])&&s[2].equals(s[3]); } private static String adj(String nextString,int k) { // TODO Auto-generated method stub for (int i = nextString.length()-1; i >=0; i--) { if(isvaw(nextString.charAt(i)))k--; if(k==0)return nextString.substring(i); } return null; } private static boolean isvaw(char c) { // TODO Auto-generated method stub return c=='a'||c=='i'||c=='e'||c=='o'||c=='u'; } public C99P3() throws IOException { //Input Output for Console to be used for trying the test samples of the problem in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //------------------------------------------------------------------------- //Initalize Stringtokenizer input input = new StringTokenizer(in.readLine()); } private static int nextInt() throws IOException { if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine()); return Integer.parseInt(input.nextToken()); } private static long nextLong() throws IOException { if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine()); return Long.parseLong(input.nextToken()); } private static double nextDouble() throws IOException { if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine()); return Double.parseDouble(input.nextToken()); } private static String nextString() throws IOException { if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine()); return input.nextToken(); } private static void write(String output){ out.write(output); out.flush(); } private static void writeln(String output){ out.write(output+"\n"); out.flush(); } private static void end() throws IOException{ in.close(); out.close(); System.exit(0); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import re r = lambda: raw_input().strip() n,k = map(int,r().split(' ')) poem = [r().strip()[::-1] for i in xrange(4*n)] counter = 0 ptype = 'aaaa' s = [] for line in poem: i = k suffix = None match = re.search('([^aeiou]*[aeiou]){%d}' % k, line) if match: suffix = match.group(0) if suffix == None: print 'NO' exit() s.append(suffix) counter += 1 if counter % 4 == 0: if len(set(s)) == 1: new_ptype = 'aaaa' elif s[0] == s[1] and s[2] == s[3]: new_ptype = 'aabb' elif s[0] == s[2] and s[1] == s[3]: new_ptype = 'abab' elif s[0] == s[3] and s[1] == s[2]: new_ptype = 'abba' else: print 'NO' exit() if new_ptype == 'aaaa': pass elif ptype == 'aaaa': ptype = new_ptype elif new_ptype != ptype: print 'NO' exit() s = [] print ptype
PYTHON
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.*; import java.math.*; public class codeforces { public static int go(Scanner sc,int k) { String[] data=new String[4]; for(int i=0;i<4;i++) { data[i]=sc.next(); int t=0; for(int j=data[i].length()-1;j>=0;j--) { if(data[i].charAt(j)=='a') t++; if(data[i].charAt(j)=='e') t++; if(data[i].charAt(j)=='i') t++; if(data[i].charAt(j)=='o') t++; if(data[i].charAt(j)=='u') t++; if(t==k) { data[i]=data[i].substring(j); break; } } // System.out.println(data[i]); if(t<k) return -1; } if((data[0].equals(data[1]))&&(data[0].equals(data[2]))&&(data[0].equals(data[3]))) return -2; if((data[0].equals(data[1]))&&(data[3].equals(data[2]))) return 0; if((data[0].equals(data[2]))&&(data[1].equals(data[3]))) return 1; if((data[0].equals(data[3]))&&(data[1].equals(data[2]))) return 2; return -1; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int res=-2; for(int i=0;i<n;i++) { int temp=go(sc,k); //System.out.println(temp); if(temp==-1) { System.out.println("NO"); return; } if(temp==-2) continue; if(res==-2) res=temp; else { if(res!=temp) { System.out.println("NO"); return; } } } if(res==-2) { System.out.println("aaaa"); return; } if(res==0) { System.out.println("aabb"); return; } if(res==1) { System.out.println("abab"); return; } System.out.println("abba"); return; } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.io.*; import java.util.*; public class CF139C { static boolean vowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } static String suffix(String s, int k) { for (int i = s.length() - 1; i >= 0; i--) if (vowel(s.charAt(i))) { k--; if (k == 0) return s.substring(i); } return null; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); String[] ss = new String[n * 4]; for (int j = 0; j < n * 4; j++) { String s = br.readLine(); if ((ss[j] = suffix(s, k)) == null) { System.out.println("NO"); return; } } boolean aabb = true; boolean abab = true; boolean abba = true; for (int i = 0; i < n; i++) { String s1 = ss[i * 4]; String s2 = ss[i * 4 + 1]; String s3 = ss[i * 4 + 2]; String s4 = ss[i * 4 + 3]; boolean m12 = s1.equals(s2); boolean m13 = s1.equals(s3); boolean m14 = s1.equals(s4); boolean m23 = s2.equals(s3); boolean m24 = s2.equals(s4); boolean m34 = s3.equals(s4); aabb = aabb && m12 && m34; abab = abab && m13 && m24; abba = abba && m14 && m23; } if (aabb && abab && abba) System.out.println("aaaa"); else if (aabb) System.out.println("aabb"); else if (abab) System.out.println("abab"); else if (abba) System.out.println("abba"); else System.out.println("NO"); } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> int main(void) { int type = 0, i, j, k, l, m, n; char str[4][10001], last[4][10001]; scanf("%d %d%*c", &n, &k); while (n--) { for (i = 0; i < 4; i++) scanf("%s%*c", str[i]); for (m = 0; m < 4; m++) { l = strlen(str[m]); for (i = 0, j = 0; i < l && j < k; i++) { switch (str[m][l - i - 1]) { case 'a': case 'i': case 'u': case 'e': case 'o': j++; break; } } if (j != k) { puts("NO"); return 0; } strcpy(last[m], str[m] + (l - i)); } if (type == 0) { if (strcmp(last[0], last[1]) == 0 && strcmp(last[1], last[2]) == 0 && strcmp(last[2], last[3]) == 0) type = 0; else if (strcmp(last[0], last[1]) == 0 && strcmp(last[2], last[3]) == 0) type = 1; else if (strcmp(last[0], last[2]) == 0 && strcmp(last[1], last[3]) == 0) type = 2; else if (strcmp(last[0], last[3]) == 0 && strcmp(last[1], last[2]) == 0) type = 3; else type = -1; } else if (type == 1) { if (strcmp(last[0], last[1]) == 0 && strcmp(last[2], last[3]) == 0) type = 1; else type = -1; } else if (type == 2) { if (strcmp(last[0], last[2]) == 0 && strcmp(last[1], last[3]) == 0) type = 2; else type = -1; } else if (type == 3) { if (strcmp(last[0], last[3]) == 0 && strcmp(last[1], last[2]) == 0) type = 3; else type = -1; } else break; } switch (type) { case -1: puts("NO"); break; case 0: puts("aaaa"); break; case 1: puts("aabb"); break; case 2: puts("abab"); break; case 3: puts("abba"); break; } return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.util.Scanner; public class LiteratureLesson { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); int n = Integer.parseInt(s.substring(0, s.indexOf(" ")).trim()); int k = Integer.parseInt(s.substring(s.indexOf(" ")).trim()); String st[][] = new String[n][4]; int index[][] = new int[n][4]; for(int i=0; i <n;i++) { st[i][0] = in.nextLine(); index[i][0] = indexVal(st[i][0],k); st[i][1] = in.nextLine(); index[i][1] = indexVal(st[i][1],k); st[i][2] = in.nextLine(); index[i][2] = indexVal(st[i][2],k); st[i][3] = in.nextLine(); index[i][3] = indexVal(st[i][3],k); if(index[i][0] < 0 || index[i][1] < 0 || index[i][2] < 0 || index[i][3] < 0) { System.out.print("NO"); return; } } String v[] = new String[n]; for(int i=0; i <n;i++) { String val[] = new String[4]; val[0] = st[i][0].substring(index[i][0]); val[1] = st[i][1].substring(index[i][1]); val[2] = st[i][2].substring(index[i][2]); val[3] = st[i][3].substring(index[i][3]); v[i] = verify(val); // System.out.println(v); } for(int i=0; i <n;i++) { s = v[i]; // System.out.print("s : " + s); if(s.equals("aaaa")) continue; else { for(int j = i+1;j<n;j++) { if(v[j].equals("aaaa")) { continue; } if(!s.equals(v[j])) { System.out.print("NO"); return; } } System.out.print(s); return; } } System.out.print(v[0]); } private static int indexVal(String s, int k) { char ch[] = s.toCharArray(); int n = ch.length -1; int count = 0; while(n >= 0) { if(ch[n] == 'a' || ch[n] == 'e' || ch[n] == 'i' || ch[n] == 'o' || ch[n] == 'u') { count++; if(count == k) return n; } n--; } return -1; } private static String verify(String s[]) { if(s[0].equals(s[1]) && s[0].equals(s[2]) && s[0].equals(s[3])) return "aaaa"; else if(s[0].equals(s[2]) && s[1].equals(s[3])) return "abab"; else if(s[0].equals(s[3]) && s[1].equals(s[2])) return "abba"; else if(s[0].equals(s[1]) && s[2].equals(s[3])) return "aabb"; else return "NO"; } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; struct sd { int f, s, t; }; string ff(string a, int k) { string tt = ""; for (int i = a.size() - 1; i >= 0; i--) { if (k == 0) break; if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') k--; tt += a[i]; } if (k != 0) return "-1"; return tt; } int main() { string fir; string sec; string th; string four; int n, k; cin >> n >> k; sd temp; temp.f = 0; temp.s = 0; temp.t = 0; vector<sd> vec(n, temp); string t1, t2, t3, t4; for (int i = 0; i < n; i++) { cin >> fir >> sec >> th >> four; t1 = ff(fir, k); t2 = ff(sec, k); t3 = ff(th, k); t4 = ff(four, k); if (t1 == "-1") continue; if (t2 == "-1") continue; if (t3 == "-1") continue; if (t4 == "-1") continue; if (t1 == t2 && t3 == t4) vec[i].f = 1; if (t1 == t3 && t2 == t4) vec[i].s = 1; if (t1 == t4 && t2 == t3) vec[i].t = 1; } bool a1, a2, a3; a1 = true; a2 = true; a3 = true; for (int i = 0; i < n; i++) { if (vec[i].f == 0) a1 = false; if (vec[i].s == 0) a2 = false; if (vec[i].t == 0) a3 = false; } if (a1 && a2 && a3) { cout << "aaaa"; return 0; } if (a1) { cout << "aabb"; return 0; } if (a2) { cout << "abab"; return 0; } if (a3) { cout << "abba"; return 0; } cout << "NO"; return 0; }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class codefo { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); scan.useDelimiter("\\p{javaWhitespace}+|[\\s,]+"); /*int n = Integer.valueOf(scan.next()); int m = Integer.valueOf(scan.next()); int[] ck = new int[n]; int even = 0; int total = 0;*/ int n = Integer.valueOf(scan.next()); /*String str = scan.next(); int index = str.length(); for(int i = 0; i < index; i++) { } */ int k = Integer.valueOf(scan.next()); String[] str = new String[4*n]; String[] ret = new String[4*n]; boolean[] flg_a = new boolean[n]; boolean[] flg_b = new boolean[n]; boolean[] flg_c = new boolean[n]; boolean[] ans = new boolean[3]; for(int i = 0; i < n; i++) { flg_a[i] = false; flg_b[i] = false; flg_c[i] = false; } for(int i = 0;i < 4*n; i++) { str[i] = (new StringBuffer(scan.next())).reverse().toString(); ret[i] = ""; } for(int i = 0; i < 4*n; i++) { int index = 0; for(int j = 0; j < k; j++) { int hoge = chk(str[i]); if(hoge == 10001) { System.out.println("NO"); return; } ret[i] += str[i].substring(0,hoge+1); str[i] = str[i].substring(hoge+1,str[i].length()); //System.out.println(ret[i]); } } for(int i = 0; i < n; i++) { if(ret[4*i].equalsIgnoreCase(ret[4*i+1])) { if(ret[4*i+2].equalsIgnoreCase(ret[4*i+3])) { flg_a[i] = true; //System.out.println("a is true " + i); } } if(ret[4*i].equalsIgnoreCase(ret[4*i+2])) { if(ret[4*i+1].equalsIgnoreCase(ret[4*i+3])) { flg_b[i] = true; //System.out.println("b is true " + i); } } if(ret[4*i].equalsIgnoreCase(ret[4*i+3])) { if(ret[4*i+1].equalsIgnoreCase(ret[4*i+2])) { flg_c[i] = true; //System.out.println("c is true " + i); } } } ans[0] = flg_a[0]; ans[1] = flg_b[0]; ans[2] = flg_c[0]; for(int i = 1 ; i < n; i++) { if(ans[0] == true) { ans[0] = flg_a[i]; } if(ans[1] == true) { ans[1] = flg_b[i]; } if(ans[2] == true) { ans[2] = flg_c[i]; } } if(ans[0] == true && ans[1] == true && ans[2] == true) { System.out.println("aaaa"); } else if(ans[0] == true) { System.out.println("aabb"); } else if(ans[1] == true) { System.out.println("abab"); } else if(ans[2] == true) { System.out.println("abba"); } else { System.out.println("NO"); } /*int[] room_length = new int[n]; int[] room_width = new int[n]; int[] room_height = new int[n]; for(int i = 0; i < n; i++) { room_length[i] = Integer.valueOf(scan.next()); room_width[i] = Integer.valueOf(scan.next()); room_height[i] = Integer.valueOf(scan.next()); } int m = Integer.valueOf(scan.next()); int[] wall_length = new int[m]; int[] wall_width = new int[m]; int[] wall_price = new int[m]; for(int i = 0; i < m; i++) { wall_length[i] = Integer.valueOf(scan.next()); wall_width[i] = Integer.valueOf(scan.next()); wall_price[i] = Integer.valueOf(scan.next()); } int[] total = new total; */ } static int chk (String str) { int hoge = 10001; int index; index = str.indexOf("a"); if(index != -1 && hoge > index) { hoge = str.indexOf("a"); } index = str.indexOf("i"); if(index != -1 && hoge > index) { hoge = str.indexOf("i"); } index = str.indexOf("u"); if(index != -1 && hoge > index) { hoge = str.indexOf("u"); } index = str.indexOf("e"); if(index != -1 && hoge > index) { hoge = str.indexOf("e"); } index = str.indexOf("o"); if(index != -1 && hoge > index) { hoge = str.indexOf("o"); } return hoge; } }
JAVA
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; bool is[10020][5]; int n, k; bool Is(char x) { if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') return 1; else return 0; } char *solve(char *x) { int t = strlen(x), c = 0; for (int i = t; i >= 0; --i) { if (Is(x[i])) { c++; if (c == k) return x + i; } } return NULL; } int main() { int i, j; while (cin >> n >> k) { memset(is, 0, sizeof(is)); ; for (i = 1; i <= n; ++i) { char c1[10020], c2[10020], c3[10020], c4[10020]; scanf("%s%s%s%s", c1, c2, c3, c4); char *z1, *z2, *z3, *z4; z1 = solve(c1); z2 = solve(c2); z3 = solve(c3); z4 = solve(c4); if (z1 == NULL || z2 == NULL || z3 == NULL || z4 == NULL) continue; if (strcmp(z1, z2) == 0 && strcmp(z3, z4) == 0) is[i][1] = 1; if (strcmp(z1, z3) == 0 && strcmp(z2, z4) == 0) is[i][2] = 1; if (strcmp(z1, z4) == 0 && strcmp(z2, z3) == 0) is[i][3] = 1; if (strcmp(z1, z2) == 0 && strcmp(z2, z3) == 0 && strcmp(z3, z4) == 0) is[i][4] = 1; } bool flag = 0; for (i = 4; i >= 1; i--) { for (j = 1; j <= n; ++j) if (!is[j][i]) break; if (j > n) { if (i == 1) puts("aabb"); if (i == 2) puts("abab"); if (i == 3) puts("abba"); if (i == 4) puts("aaaa"); flag = 1; break; } } if (!flag) puts("NO"); } }
CPP
139_C. Literature Lesson
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β‰  ermit). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): * Clerihew (aabb); * Alternating (abab); * Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. Input The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. Output Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. Examples Input 1 1 day may sun fun Output aabb Input 1 1 day may gray way Output aaaa Input 2 1 a a a a a a e e Output aabb Input 2 1 day may sun fun test hill fest thrill Output NO Note In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
2
9
#include <bits/stdc++.h> using namespace std; bool f[2510][5]; int n, k; bool Is(char x) { if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') return 1; return 0; } bool NO; char *judge(char *c) { if (NO) return NULL; int l = strlen(c), t = 0; for (int i = l - 1; i >= 0; --i) { if (Is(c[i])) { t++; if (t == k) return c + i; } } NO = 1; return NULL; } int main() { int i, j; while (cin >> n >> k) { memset(f, 0, sizeof(f)); ; NO = 0; for (i = 1; i <= n; ++i) { char c1[10020], c2[10020], c3[10020], c4[10020]; scanf("%s%s%s%s", c1, c2, c3, c4); if (NO) continue; char *s1 = judge(c1); char *s2 = judge(c2); char *s3 = judge(c3); char *s4 = judge(c4); if (NO) continue; if (!strcmp(s1, s2) && !strcmp(s3, s4)) f[i][1] = 1; if (!strcmp(s1, s3) && !strcmp(s2, s4)) f[i][2] = 1; if (!strcmp(s1, s4) && !strcmp(s2, s3)) f[i][3] = 1; if (!strcmp(s1, s2) && !strcmp(s2, s3) && !strcmp(s3, s4)) f[i][4] = 1; } if (NO) { puts("NO"); continue; } NO = 1; for (i = 4; i > 0; --i) { for (j = 1; j <= n; ++j) if (!f[j][i]) break; if (j > n) { if (i == 1) puts("aabb"); if (i == 2) puts("abab"); if (i == 3) puts("abba"); if (i == 4) puts("aaaa"); NO = 0; break; } } if (NO) puts("NO"); } }
CPP