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
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
vowels = set('aeiou') def suf(s, k): global vowels i = 0 l_s = len(s) - 1 while l_s >= 0: if s[l_s] in vowels: i += 1 if i == k: return s[l_s:] l_s -= 1 print "NO" exit(0) r = raw_input n,k = map(int,r().strip().split(' ')) w_s = 7 p_s = [suf(r().strip(),k) for i in xrange(4 * n)] for base in xrange(0,4 * n,4): s = 0 if p_s[base] == p_s[base + 1] and p_s[base + 2] == p_s[base + 3]: s |= 1 if p_s[base] == p_s[base + 2] and p_s[base + 1] == p_s[base + 3]: s |= 2 if p_s[base] == p_s[base + 3] and p_s[base + 1] == p_s[base + 2]: s |= 4 w_s &= s if w_s == 0: print 'NO' exit(0) if w_s == 7: print 'aaaa' exit(0) if w_s == 1: print 'aabb' exit(0) if w_s == 2: print 'abab' exit(0) if w_s == 4: print 'abba' exit(0)
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 re import sys rgx = re.compile( '[aeiou]' ) rs = { 63:'aaaa', 33:'aabb', 18:'abab', 12: 'abba' } pairs = { 1: [0,1], 2:[0,2], 4:[0,3], 8:[1,2], 16:[1,3], 32:[2,3] } def c( qs, k): scheme = 63 for q in qs: splits = [] falls = [] for lin in q: fall= re.findall(rgx,lin) if len(fall) < k: return "NO" falls += [ fall ] splits += [ re.split(rgx,lin) ] sum = 0 for key in pairs: if falls[pairs[key][0]][-k:]!=falls[pairs[key][1]][-k:]: continue if splits[pairs[key][0]][-k:]!=splits[pairs[key][1]][-k:]: continue sum += key if not (sum in rs): return "NO" scheme &= sum if not (scheme in rs): return "NO" return rs[scheme] if __name__=="__main__": toks = sys.stdin.readline().split() nq = int(toks[0]) k = int(toks[1]) qs = [] for iq in range(nq): q = [] for i in range(4): q += [ sys.stdin.readline().strip('\r\n') ] qs += [q] print( c(qs,k) )
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.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pr = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); boolean gb = true; byte[] w = new byte[n]; sc.nextLine(); for (int i = 0; i < n; i++) { String[] s = new String[4]; for (int j = 0; j < 4; j++) { s[j] = sc.nextLine(); int a = 0; int v = 0; for (v = s[j].length() - 1; v >= 0 && a < k; v--) { char z = s[j].charAt(v); if (z == 'a' || z == 'e' || z == 'i' || z == 'u' || z == 'o') a++; } if (a != k) { gb = false; break; } s[j] = s[j].substring(v + 1); } if (!gb) break; if (s[0].equals(s[1]) && s[0].equals(s[2]) && s[0].equals(s[3])) w[i] = 1; else if (s[0].equals(s[1]) && s[2].equals(s[3])) w[i] = 2; else if (s[0].equals(s[2]) && s[1].equals(s[3])) w[i] = 3; else if (s[0].equals(s[3]) && s[1].equals(s[2])) w[i] = 4; else { gb = false; break; } } if (!gb) pr.print("NO"); else { TreeSet<Byte> t = new TreeSet<Byte>(); for (int i = 0; i < n; i++) { t.add(w[i]); } if (t.size() == 2) { if (t.first() == 1) { if (t.last() == 2) pr.print("aabb"); else if (t.last() == 3) pr.print("abab"); else pr.print("abba"); } else pr.print("NO"); } else if (t.size() == 1) { if (t.last() == 1) pr.print("aaaa"); else if (t.last() == 2) pr.print("aabb"); else if (t.last() == 3) pr.print("abab"); else pr.print("abba"); } else pr.print("NO"); } pr.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
#include <bits/stdc++.h> using namespace std; int n, k, cntk; string s[10009]; string sf, rh[2], rh1, common; bool check(string ss) { if (ss != "aabb" && ss != "abab" && ss != "abba" && ss != "aaaa") return false; return true; } bool vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } int main() { cin >> n >> k; for (int i = 0; i < 4 * n; i++) cin >> s[i]; for (int i = 0; i < 4 * n; i += 4) { rh1 = ""; rh[0] = ""; rh[1] = ""; for (int j = 0; j < 4; j++) { sf = ""; int ptr, n1; n1 = s[i + j].size(); ptr = n1 - 1; cntk = 0; while (ptr >= 0 && cntk < k) { sf = s[i + j][ptr] + sf; if (vowel(s[i + j][ptr])) cntk++; ptr--; } if (cntk < k) { cout << "NO"; return 0; } if (rh[0] == "") { rh[0] = sf; rh1 += 'a'; } else if (sf == rh[0]) rh1 += 'a'; else if (sf != rh[0] && rh[1] == "") { rh[1] = sf; rh1 += 'b'; } else if (sf == rh[1]) rh1 += 'b'; else { cout << "NO"; return 0; } } if (!check(rh1)) { cout << "NO"; return 0; } if (i == 0) common = rh1; else if (common == "aaaa") common = rh1; else if (common != rh1 && rh1 != "aaaa") { cout << "NO"; return 0; } } cout << common; 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, aaaa, abab, aabb, abba, flag1, v; string s[5], s1[5]; int main() { cin >> n >> k; aabb = 1; abab = 1; abba = 1; aaaa = 1; flag1 = 1; for (int q = 1; q <= n; q++) { for (int i = 1; i <= 4; i++) cin >> s[i]; if (flag1 == 0) continue; for (int i = 1; i <= 4; i++) { v = 0; for (int j = ((s[i]).length()) - 1; j >= 0; j--) { if ((s[i][j] == 'a') or (s[i][j] == 'e') or (s[i][j] == 'i') or (s[i][j] == 'o') or (s[i][j] == 'u')) v++; if (v == k) { s1[i] = s[i].substr(j, (s[i].length() - j)); break; } } if (v != k) { aabb = 0; abab = 0; abba = 0; aaaa = 0; flag1 = 0; break; } } if (flag1 == 0) continue; if (s1[1] != s1[2]) { aabb = 0; aaaa = 0; } if (s1[1] != s1[3]) { abab = 0; aaaa = 0; } if (s1[1] != s1[4]) { abba = 0; aaaa = 0; } if (s1[2] != s1[3]) { abba = 0; aaaa = 0; } if (s1[2] != s1[4]) { abab = 0; aaaa = 0; } if (s1[3] != s1[4]) { aabb = 0; aaaa = 0; } } if (aaaa == 1) cout << "aaaa" << endl; else if (aabb == 1) cout << "aabb" << endl; else if (abab == 1) cout << "abab" << endl; else if (abba == 1) 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
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class C { private static String ans (List<String> words, int k) { List<String> suffixes = new ArrayList<String>(); for (String word: words) { String suf = word.replace('a', '*').replace('e', '*'). replace('i', '*').replace('o', '*').replace('u', '*'); char chArr[] = suf.toCharArray(); int d = 0; for (int i=chArr.length-1; i>=0; i--) { if (chArr[i]=='*') { d++; } if (d==k) { suf = word.substring(i); break; } } if (d<k) return "NO"; //System.out.println(suf); suffixes.add(suf); } String answer[] = {"aabb", "abab", "abba", "aaaa"}; boolean values[] = {true, true, true, true}; for (int i=0; i<suffixes.size()/4; i++) { String a = suffixes.get(i*4); String b = suffixes.get(i*4+1); String c = suffixes.get(i*4+2); String d = suffixes.get(i*4+3); if (b.equals(c)==false || a.equals(d)==false) { values[2] = false; values[3] = false; } if (a.equals(b)==false || c.equals(d)==false) { values[0] = false; values[3] = false; } if (a.equals(c)==false || b.equals(d)==false) { values[1] = false; values[3] = false; } if (!values[0] && !values[1] && !values[2] && !values[3]) return "NO"; } for (int i=3; i>=0; i--) if (values[i]==true) return answer[i]; return "NO"; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); List<String> words = new ArrayList<String>(); for (int i=0; i<4*n; i++) { words.add(sc.next()); } System.out.println(ans(words, k)); } }
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 syll[256]; void nope() { cout << "NO" << endl; exit(0); } int main() { syll['a'] = syll['e'] = syll['i'] = syll['o'] = syll['u'] = true; int n, k; cin >> n >> k; set<string> res; for (int i = 0; i < n; i++) { string suf[4]; for (int j = 0; j < 4; j++) { string s; cin >> s; int p = (int)s.size(), c = 0; while (p > 0 && c < k) { --p; if (syll[s[p]]) ++c; } if (c < k) nope(); suf[j] = s.substr(p); } if (suf[0] == suf[1]) { if (suf[2] == suf[3]) { if (suf[1] == suf[2]) res.insert("aaaa"); else res.insert("aabb"); } else nope(); } else if (suf[0] == suf[2]) { if (suf[1] == suf[3]) { if (suf[0] == suf[1]) res.insert("aaaa"); else res.insert("abab"); } else nope(); } else if (suf[0] == suf[3]) { if (suf[1] == suf[2]) { if (suf[0] == suf[1]) res.insert("aaaa"); else 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
n,k = map(int,input().split()) lis=[] vov=['a','e','i','o','u'] d={} d['aabb']=d['abab']=d['abba']=d['aaaa']=0 for i in range(n*4): s = input() lis.append(s) if i%4==3: tmp=['']*4 for j in range(4): s=lis[j] c=0 cou=0 for ll in s[::-1]: cou+=1 if ll in vov: c+=1 if c==k: tmp[j]=s[-cou:] break # print(tmp) tt=1 if tmp[0]==tmp[1]==tmp[2]==tmp[3] and tmp[0]!='': d['aaaa']+=1 tt=0 if tmp[0]==tmp[1] and tmp[2]==tmp[3] and tmp[1]!=tmp[3] and tmp[1]!='' and tmp[3]!='': d['aabb']+=1 tt=0 elif tmp[0]==tmp[2] and tmp[1]==tmp[3] and tmp[1]!=tmp[0] and tmp[1]!='' and tmp[0]!='': d['abab']+=1 tt=0 elif tmp[0]==tmp[3] and tmp[1]==tmp[2] and tmp[2]!=tmp[0] and tmp[1]!='' and tmp[3]!='': d['abba']+=1 tt=0 if tt: print("NO") exit() lis=[] c=0 for i in d: if d[i]>0: c+=1 if c==1: for i in d: if d[i]>0: print(i) exit() elif c==2 and d['aaaa']>0: for i in d: if d[i]>0: print(i) exit() else: print("NO")
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.*; import java.io.*; import java.awt.Point; import static java.lang.Math.*; public class P139C { static int n; static int k; public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); n = in.nextInt(); k = in.nextInt(); int[] rhyme = new int[n]; for(int q=0; q<n; q++) { String A = in.next(); String B = in.next(); String C = in.next(); String D = in.next(); if(rhymes(A,B) && rhymes(A,C) && rhymes(A,D)) rhyme[q] = -1; else if(rhymes(A,B) && rhymes(C,D)) rhyme[q] = 1; else if(rhymes(A,C) && rhymes(B,D)) rhyme[q] = 2; else if(rhymes(A,D) && rhymes(B,C)) rhyme[q] = 3; else { System.out.println("NO"); return; } } int scheme = -1; for(int q=0; q<n; q++) { if(rhyme[q] > 0 && scheme>0 && scheme!=rhyme[q]) { System.out.println("NO"); return; } if(rhyme[q] > 0) scheme = rhyme[q]; } switch(scheme) { case -1: System.out.println("aaaa"); break; case 1: System.out.println("aabb"); break; case 2: System.out.println("abab"); break; case 3: System.out.println("abba"); break; } } public static boolean rhymes(String a, String b) { int vowels = 0; for(int i=0;; i++) { if(i >= a.length() || i>=b.length()) return false; if(a.charAt(a.length()-1-i) != b.charAt(b.length()-1-i)) return false; if(is_vowel(a.charAt(a.length()-1-i))) vowels++; if(vowels >= k) return true; } } public static boolean is_vowel(char c) { return c=='a' || c=='e' || c=='i' || c=='o' || c=='u'; } }
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.*; import java.lang.*; public class Rextester{ static boolean isVowel(char ch){ if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){ return true; } return false; } static String getRhymeScheme(String[] input,int k){ String[] suffixes = new String[4]; for(int i=0;i<4;i++){ int count = 0; for(int j=input[i].length()-1;j>=0;j--){ if(isVowel(input[i].charAt(j))){ count++; } if(count==k){ suffixes[i]=input[i].substring(j,input[i].length()); break; } } if(suffixes[i]==null){ suffixes[i]=""; } } if(suffixes[0].equals(suffixes[1])){ if(suffixes[0].equals(suffixes[2]) && suffixes[0].equals(suffixes[3])){ if(suffixes[0].equals("")){ return ""; } return "aaaa"; } else if(suffixes[2].equals(suffixes[3])){ return "aabb"; } else{ return ""; } } else if(suffixes[0].equals(suffixes[2])){ if(suffixes[1].equals(suffixes[3])){ return "abab"; } else{ return ""; } } else if(suffixes[0].equals(suffixes[3])){ if(suffixes[1].equals(suffixes[2])){ return "abba"; } else{ return ""; } } return ""; } 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[] rhymes = new String[n]; for(int i=0;i<n;i++){ String[] input = new String[4]; input[0]=br.readLine(); input[1]=br.readLine(); input[2]=br.readLine(); input[3]=br.readLine(); rhymes[i]=getRhymeScheme(input,k); } String commonPattern = "zz"; for(int i=0;i<n;i++){ if(commonPattern.equals("zz")){ commonPattern = rhymes[i]; } else if(commonPattern.equals(rhymes[i])){ continue; } else if(commonPattern.equals("aaaa") && !rhymes[i].equals(commonPattern)){ commonPattern = rhymes[i]; } else if(rhymes[i].equals("aaaa")){ continue; } else{ System.out.println("NO"); return; } } if(commonPattern.equals("")){ System.out.println("NO"); } else{ System.out.println(commonPattern); } } }
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 sys n, k = map(int, sys.stdin.readline().split()) vowels = 'aiueo' def a4(rh): return rh[0] == rh[1] == rh[2] == rh[3] def abab(rh): return rh[0] == rh[2] and rh[1] == rh[3] def aabb(rh): return rh[0] == rh[1] and rh[2] == rh[3] def abba(rh): return rh[0] == rh[3] and rh[1] == rh[2] ok = [True] * 4 for i in xrange(n): rh = [''] * 4 for j in range(4): line = sys.stdin.readline().strip() t, cnt = len(line) - 1, 0 while t >= 0: if line[t] in vowels: cnt += 1 if cnt == k: break t -= 1 if t >= 0: rh[j] = line[t:] else: rh[j] = str(j) if not a4(rh): ok[0] = False if not aabb(rh): ok[1] = False if not abab(rh): ok[2] = False if not abba(rh): ok[3] = False if ok[0]: print 'aaaa' elif ok[1]: print 'aabb' elif ok[2]: print 'abab' elif ok[3]: 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
def suffix(tpl): s, k = tpl for i in range(len(s)): if s[len(s)-i-1] == 'a' or \ s[len(s)-i-1] == 'e' or \ s[len(s)-i-1] == 'i' or \ s[len(s)-i-1] == 'o' or \ s[len(s)-i-1] == 'u' : k -= 1 if k==0: return s[len(s)-i-1:] inp = raw_input() n, k = map(int, inp.split(' ')) type_overall = 0 a = False for i in range(n): lines = [raw_input(), raw_input(), raw_input(), raw_input()] lines = map(lambda x: (x, k), lines) suffs = map(suffix, lines) if None in suffs: print 'NO' a = True break if suffs[0] == suffs[1] and suffs[1] == suffs[2] and suffs[2] == suffs[3]: type_current = 0 elif suffs[0] == suffs[1] and suffs[2] == suffs[3]: type_current = 1 elif suffs[0] == suffs[2] and suffs[1] == suffs[3]: type_current = 2 elif suffs[0] == suffs[3] and suffs[1] == suffs[2]: type_current = 3 else: print 'NO' a = True break if type_current == 0: pass elif type_overall == 0: type_overall = type_current elif type_overall != type_current: print 'NO' a = True break if not a: if type_overall == 0: print 'aaaa' if type_overall == 1: print 'aabb' if type_overall == 2: print 'abab' if type_overall == 3: print 'abba'
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; string line[10101]; vector<int> r; bool isvowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } string extract(string s, int k) { int position = -1; for (int i = s.length(); i > 0; i--) { if (isvowel(s[i - 1])) k--; if (k == 0) { position = i - 1; break; } } if (position == -1) return ""; s.erase(0, position); return s; } int rhyme(string s1, string s2, string s3, string s4) { if (s1.compare(s2) == 0) { if (s3.compare(s4) == 0) if (s2.compare(s3) == 0) return 0; else return 1; else return -1; } if (s1.compare(s3) == 0 && s2.compare(s4) == 0) return 2; if (s1.compare(s4) == 0 && s2.compare(s3) == 0) return 3; return -1; } int main() { cin >> n >> k; for (int i = 0; i < 4 * n; i++) { string s; cin >> s; line[i] = extract(s, k); if (line[i] == "") { cout << "NO"; return 0; } } for (int i = 0; i < 4 * n; i += 4) r.push_back(rhyme(line[i], line[i + 1], line[i + 2], line[i + 3])); sort(r.begin(), r.end()); r.erase(unique(r.begin(), r.end()), r.end()); int scheme; if (r[0] == -1) scheme = -1; else if (r[0] == 0) if (r.size() == 1) scheme = 0; else if (r.size() == 2) scheme = r[1]; else scheme = -1; else if (r.size() > 1) scheme = -1; else scheme = r[0]; if (scheme == -1) cout << "NO"; else if (scheme == 0) cout << "aaaa"; else if (scheme == 1) cout << "aabb"; else if (scheme == 2) cout << "abab"; else cout << "abba"; }
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; template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> T sqr(T x) { return x * x; } template <typename T> T cube(T x) { return x * x * x; } string out(long long x) { stringstream ss; ss << x; string res; ss >> res; return res; } long long out(string s) { stringstream ss; ss << s; long long res; ss >> res; return res; } template <typename T> void print(const vector<vector<T> > &v) { for (int i = 0; i < (int)(v.size()); i++) { cout << "i = " << i << endl; for (int j = 0; j < (int)(v[i].size()); j++) { cout << v[i][j] << ' '; } cout << endl; cout << endl; } } template <typename T> void print(const vector<T> &v) { for (int i = 0; i < (int)(v.size()); i++) { cout << v[i] << ' '; } cout << endl; } void no() { cout << "NO" << endl; exit(0); } string glas = "aeiou"; int k, n; vector<string> v; bool isGlas(char c) { return binary_search(glas.begin(), glas.end(), c); } void transform(string &s) { int i = s.size(); int cnt = 0; do { i--; cnt += isGlas(s[i]); } while (cnt < k); s = s.substr(i, s.size() - i); } void isgood(const string &s) { int cnt = 0; for (int i = 0; i < (int)(s.size()); i++) { cnt += binary_search(glas.begin(), glas.end(), s[i]); } if (cnt < k) no(); } bool case_0() { return v[0] == v[1] && v[1] == v[2] && v[2] == v[3]; } bool case_1() { return v[0] == v[1] && v[2] == v[3]; } bool case_2() { return v[0] == v[2] && v[1] == v[3]; } bool case_3() { return v[0] == v[3] && v[1] == v[2]; } string func() { v.clear(); v.resize(4); for (int i = 0; i < (int)(4); i++) { cin >> v[i]; isgood(v[i]); transform(v[i]); } if (case_0()) { return "aaaa"; } if (case_1()) return "aabb"; if (case_2()) return "abab"; if (case_3()) return "abba"; no(); return ""; } void init() { sort(glas.begin(), glas.end()); } int main() { init(); cin >> n >> k; string commonStyle; for (int j = 0; j < (int)(n); j++) { string style = func(); if (j && commonStyle != style && commonStyle != "aaaa" && style != "aaaa") { no(); } if (!j) { commonStyle = style; } else { commonStyle = style == "aaaa" ? commonStyle : style; } } cout << commonStyle << 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 vowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } int main() { int n, k; scanf("%d %d", &n, &k); bool aabb = 1, abab = 1, abba = 1; vector<string> s(4); while (n--) { for (int i = 0; i < 4; ++i) { cin >> s[i]; int t = k, j = (int)((s[i]).size()) - 1; for (; t > 0 && j >= 0; j--) if (vowel(s[i][j])) t--; if (t > 0) { puts("NO"); return 0; } s[i] = s[i].substr(j + 1); } if (s[0] != s[1] || s[2] != s[3]) aabb = 0; if (s[0] != s[2] || s[1] != s[3]) abab = 0; if (s[0] != s[3] || s[1] != s[2]) abba = 0; } puts((aabb && abab) ? "aaaa" : (aabb) ? "aabb" : (abab) ? "abab" : (abba) ? "abba" : "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 signed long long Infinity = 1000000001; const long double Pi = 2.0L * asinl(1.0L); const long double Epsilon = 0.000000001; template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << "(" << p.first << "," << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& V) { for (typeof(V.begin()) i = V.begin(); i != V.end(); ++i) os << *i << endl; return os; } template <class T> ostream& operator<<(ostream& os, const set<T>& S) { for (typeof(S.begin()) i = S.begin(); i != S.end(); ++i) os << *i << endl; return os; } template <class T, class U> ostream& operator<<(ostream& os, const map<T, U>& M) { for (typeof(M.begin()) i = M.begin(); i != M.end(); ++i) os << *i << endl; return os; } string vowel = "ouiea"; vector<string> A; int n, k; bool rhyme(string a, string b) { int i = a.size(), j = b.size(); int t = k; while (i > 0 and t > 0) { i--; if (find(vowel.begin(), vowel.end(), a[i]) != vowel.end()) t--; } if (i == 0 and t != 0) return false; t = k; while (j > 0 and t > 0) { j--; if (find(vowel.begin(), vowel.end(), b[j]) != vowel.end()) t--; } if (j == 0 and t != 0) return false; return a.substr(i) == b.substr(j); } void readData() { scanf("%d %d", &n, &k); for (int(i) = (1); (i) <= (4 * n); (i)++) { string s; cin >> s; A.push_back(s); } } bool aaaa(int a) { if (!rhyme(A[4 * a], A[4 * a + 1])) return false; if (!rhyme(A[4 * a], A[4 * a + 2])) return false; if (!rhyme(A[4 * a], A[4 * a + 3])) return false; return true; } bool abba(int a) { if (!rhyme(A[4 * a], A[4 * a + 3])) return false; if (!rhyme(A[4 * a + 1], A[4 * a + 2])) return false; return true; } bool abab(int a) { if (!rhyme(A[4 * a], A[4 * a + 2])) return false; if (!rhyme(A[4 * a + 1], A[4 * a + 3])) return false; return true; } bool aabb(int a) { if (!rhyme(A[4 * a], A[4 * a + 1])) return false; if (!rhyme(A[4 * a + 2], A[4 * a + 3])) return false; return true; } bool AAAA() { for (int(i) = (0); (i) < (n); (i)++) if (!aaaa(i)) return false; return true; } bool AABB() { for (int(i) = (0); (i) < (n); (i)++) if (!aabb(i)) return false; return true; } bool ABAB() { for (int(i) = (0); (i) < (n); (i)++) if (!abab(i)) return false; return true; } bool ABBA() { for (int(i) = (0); (i) < (n); (i)++) if (!abba(i)) return false; return true; } void solve() { if (AAAA()) printf("aaaa"); else if (AABB()) printf("aabb"); else if (ABAB()) printf("abab"); else if (ABBA()) printf("abba"); else printf("NO"); } int main() { readData(); 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
#!/usr/bin/env python def get_scheme(quarts): if quarts[0] == quarts[1] and quarts[1] == quarts[2] and quarts[2] == quarts[3]: return 'aaaa' if quarts[0] == quarts[1] and quarts[2] == quarts[3]: return 'aabb' if quarts[0] == quarts[2] and quarts[1] == quarts[3]: return 'abab' if quarts[0] == quarts[3] and quarts[1] == quarts[2]: return 'abba' def get_suffix(line, k): vowels = ['a', 'e', 'i', 'o', 'u'] suffix = '' for i, c in enumerate(line[::-1]): if c in vowels: k -= 1 if k == 0: return line[len(line) - i - 1 : ] if k != 0: return '' return line if __name__ == '__main__': q, k = map(int, raw_input().split()) scheme = [] invalid_k = False for i in range(q): quart = [] for i in range(4): suffix = get_suffix(raw_input(), k) if not suffix: invalid_k = True quart.append(suffix) scheme.append(get_scheme(quart)) scheme_set = set(scheme) if invalid_k or None in scheme: print 'NO' elif len(scheme_set) == 2: if 'aaaa' in scheme_set: for s in scheme: if s != 'aaaa': print s break else: print 'NO' elif len(scheme_set) > 2: print 'NO' else: print scheme[0]
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; const int maxn = 1e4 + 7; char str[maxn]; char ans[4]; int main() { int n, k; cin >> n >> k; int flag = 0; for (int i = 0; i < n; i++) { map<string, int> mp; string s[4]; char tans[4]; tans[0] = 1; int c = 0; for (int j = 0; j < 4; j++) { cin >> str; s[j].clear(); int len = strlen(str); int kk = k; for (int i = len - 1; kk && len >= 0;) { s[j] += str[len]; if (str[len] == 'a' || str[len] == 'e' || str[len] == 'i' || str[len] == 'o' || str[len] == 'u') kk--; len--; } if (kk > 0 && len < 0) flag = 1; mp[s[j]] = j; } if (mp.size() <= 2) { for (int i = 1; i < 4; i++) if (mp[s[0]] == mp[s[i]]) tans[i] = 0; else tans[i] = 1; for (int i = 1; i < 4; i++) { ans[i] = ans[i] | tans[i]; } } else { flag = 1; } } if (flag == 0) { int cnt = 0; for (int i = 0; i < 4; i++) cnt += ans[i]; if (cnt == 1 || cnt == 3) { cout << "NO" << endl; } else { for (int i = 0; i < 4; i++) { if (ans[i] == 0) cout << "a"; else cout << "b"; } } } 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
def IsOk(c): return c in ['a', 'e', 'o', 'i', 'u'] def f(k): a = raw_input() b = raw_input() c = raw_input() d = raw_input() ai = len(a) - 1 bi = len(b) - 1 ci = len(c) - 1 di = len(d) - 1 r = 0 while ai >= 0 and r < k: r += IsOk(a[ai]) ai -= 1 ai += 1 if r != k: return 'NO' r = 0 while bi >= 0 and r < k: r += IsOk(b[bi]) bi -= 1 bi += 1 if r != k: return 'NO' r = 0 while ci >= 0 and r < k: r += IsOk(c[ci]) ci -= 1 ci += 1 if r != k: return 'NO' r = 0 while di >= 0 and r < k: r += IsOk(d[di]) di -= 1 di += 1 if r != k: return 'NO' else: if (a[ai:] == b[bi:] == c[ci:] == d[di:]): return 'aaaa' elif (a[ai:] == b[bi:] and c[ci:] == d[di:]): return 'aabb' elif (a[ai:] == c[ci:] and b[bi:] == d[di:]): return 'abab' elif (a[ai:] == d[di:] and b[bi:] == c[ci:]): return 'abba' else: return 'NO' n, k = map(int, raw_input().split()) ans = f(k) now = '' for i in range(n - 1): now = f(k) if now != ans and ans != 'aaaa' and now != 'aaaa': ans = 'NO' elif ans == 'aaaa': ans = now 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 n, m; char poem[4][10005]; bool is[4][4]; int len[4]; char ans[5][10] = {"aaaa", "aabb", "abab", "abba", "NO"}; int vol(char x) { if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') return true; return false; } int type[2505]; int main(int argc, char** argv) { scanf("%d%d", &n, &m); for (int v = 0; v < n; ++v) { memset(is, false, sizeof(is)); for (int i = 0; i < 4; ++i) scanf("%s", poem[i]); for (int i = 0; i < 4; ++i) len[i] = strlen(poem[i]); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (j == i) continue; bool flag = true; int cnt = 0; for (int k = len[i], l = len[j]; k >= 0 && j >= 0; --k, --l) { if (poem[i][k] != poem[j][l]) { flag = false; break; } if (vol(poem[i][k])) cnt++; if (cnt == m) break; } is[i][j] = (flag && (cnt == m)); } } if (is[0][1] && is[0][2] && is[0][3]) { type[v] = 0; continue; } if (is[0][1] && is[2][3]) { type[v] = 1; continue; } if (is[0][2] && is[1][3]) { type[v] = 2; continue; } if (is[0][3] && is[1][2]) { type[v] = 3; continue; } type[v] = 4; } int con[5]; memset(con, 0, sizeof(con)); for (int i = 0; i < n; ++i) { con[type[i]]++; } if (con[0] > 0 && con[1] == 0 && con[2] == 0 && con[3] == 0 && con[4] == 0) puts(ans[0]); else if (con[1] > 0 && con[2] == 0 && con[3] == 0 && con[4] == 0) puts(ans[1]); else if (con[1] == 0 && con[2] > 0 && con[3] == 0 && con[4] == 0) puts(ans[2]); else if (con[1] == 0 && con[2] == 0 && con[3] > 0 && con[4] == 0) puts(ans[3]); else puts(ans[4]); 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 N = 25000; int n, m, maxn; char s[10][N], r[4][N]; int a[N]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 1; j <= 4; j++) { scanf("%s", s[j]); int len = strlen(s[j]), cur = m, k; for (k = len - 1; k >= 0;) { if (s[j][k] == 'a' || s[j][k] == 'e' || s[j][k] == 'i' || s[j][k] == 'o' || s[j][k] == 'u') cur--; if (cur == 0) break; k--; } if (k < 0) { printf("NO"); return 0; } for (int l = k; l < len; l++) { r[j][l - k] = s[j][l]; } } int bi[5][5]; memset(bi, 0, sizeof(bi)); for (int k = 1; k <= 4; k++) { for (int l = k + 1; l <= 4; l++) { if (strcmp(r[k], r[l]) == 0) bi[k][l] = 1; } } if (bi[1][2] == 1 && bi[2][3] == 1 && bi[3][4] == 1) { a[i] = 1; } else if (bi[1][4] == 1 && bi[2][3] == 1) { a[i] = 2; } else if (bi[1][3] == 1 && bi[2][4] == 1) { a[i] = 3; } else if (bi[1][2] == 1 && bi[3][4] == 1) { a[i] = 4; } else { a[i] = 5; } } int vis[6]; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { vis[a[i]]++; } if (vis[5] > 0) { printf("NO"); return 0; } if (vis[1] > 0 && vis[2] == 0 && vis[3] == 0 && vis[4] == 0) { printf("aaaa"); } else if (vis[2] > 0 && vis[3] == 0 && vis[4] == 0) { printf("abba"); } else if (vis[2] == 0 && vis[3] > 0 && vis[4] == 0) { printf("abab"); } else if (vis[2] == 0 && vis[3] == 0 && vis[4] > 0) { printf("aabb"); } else { printf("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; int main() { int n, k, sp = -1; scanf("%d%d\n", &n, &k); vector<string> v; for (int i = 0; i < n; i++) { v.clear(); int a; for (int j = 0; j < 4; j++) { string s; getline(cin, s); int b = k, pos = -1; for (int z = s.size() - 1; z >= 0; z--) { if (s[z] == 'a' || s[z] == 'e' || s[z] == 'i' || s[z] == 'o' || s[z] == 'u') b--; if (!b) { pos = z; break; } } if (pos == -1) { puts("NO"); return 0; } v.push_back(s.substr(pos)); } a = -1; if (v[0] == v[3] && v[1] == v[2]) a = 3; if (v[0] == v[1] && v[2] == v[3]) a = 1; if (v[0] == v[2] && v[1] == v[3]) a = 2; if (v[0] == v[2] && v[0] == v[3] && v[0] == v[1]) a = 4; if (a == -1) { puts("NO"); return 0; } if (sp == -1 || sp == 4) sp = a; else if (a != 4 && a != sp) { puts("NO"); return 0; } } if (sp == 1) cout << "aabb\n"; else if (sp == 2) cout << "abab\n"; else if (sp == 3) cout << "abba\n"; else cout << "aaaa\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
def rhyme(a,b,k): v=["a","e","i","o","u"] i_a=None i_b=None v_a=0 v_b=0 for i in range(len(a)-1,-1,-1): if a[i] in v: v_a+=1 if v_a==k: i_a=i break for i in range(len(b)-1,-1,-1): if b[i] in v: v_b+=1 if v_b==k: i_b=i break if i_a!=None and i_b != None and a[i_a:]==b[i_b:]: return True return False def main(arr,k): style={0:"aabb",1:"abab",2:"abba",3:'aaaa'} ans=[] for j in range(4): c=True for i in range(len(arr)): q=arr[i] if j==0: if rhyme(q[0],q[1],k) and rhyme(q[2],q[3],k): continue else: c=False break if j==1: if rhyme(q[0],q[2],k) and rhyme(q[1],q[3],k): continue else: c=False break if j==2: if rhyme(q[0],q[3],k) and rhyme(q[1],q[2],k): continue else: c=False break if j==3: if rhyme(q[0],q[1],k) and rhyme(q[1],q[2],k) and rhyme(q[2],q[3],k): continue else: c=False break if c: ans.append(j) if 3 in ans: return style[3] elif len(ans)>=1: return style[ans[0]] else: return "NO" n,k=list(map(int,input().split())) arr=[] for i in range(n): temp=[] for j in range(4): temp.append(input()) arr.append(temp) print(main(arr,k))
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
#include <bits/stdc++.h> using namespace std; int find(string str, char vowel[], int k) { int count = 0; for (int i = str.size() - 1; i >= 0; i--) { char found = '0'; for (int j = 0; j < 5; j++) if (str[i] == vowel[j]) { found = vowel[j]; break; } if (found != '0') { count++; if (count == k) return i; } } return -1; } int main() { int n, k; cin >> n >> k; char vowel[5] = {'a', 'e', 'i', 'o', 'u'}; int types[n]; for (int i = 0; i < n; i++) { string found[4]; for (int j = 0; j < 4; j++) { string str; cin >> str; int index = find(str, vowel, k); if (index == -1) { cout << "NO"; return 0; } found[j] = str.substr(index, str.size() - index); } if (found[0] == found[1] && found[0] == found[2] && found[0] == found[3]) { types[i] = 4; } else if (found[0] == found[1] && found[2] == found[3]) types[i] = 1; else if (found[0] == found[2] && found[1] == found[3]) types[i] = 2; else if (found[0] == found[3] && found[1] == found[2]) types[i] = 3; else { cout << "NO"; return 0; } } int compare = 4; for (int i = 0; i < n; i++) { if (types[i] != 4) { if (compare != 4) { if (compare != types[i]) { cout << "NO"; return 0; } } else compare = types[i]; } } if (compare == 1) cout << "aabb"; else if (compare == 2) cout << "abab"; else if (compare == 3) cout << "abba"; else if (compare == 4) cout << "aaaa"; }
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
//package codeforces; /** * * @author Siddharth */ import java.io.*; public class Lesson { static boolean vowel(char x) { if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')return true; return false; } public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s[]=br.readLine().split(" "); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); int method=7; for(int i=0;i<n;i++) { String i1=br.readLine(); String i2=br.readLine(); String i3=br.readLine(); String i4=br.readLine(); int count=0; int x; for(x=i1.length()-1;x>=0;x--) { if(vowel(i1.charAt(x)))count++; if(count==k)break; } if(x==-1){method=0;break;} count=0; String suf1=i1.substring(x); for(x=i2.length()-1;x>=0;x--) { if(vowel(i2.charAt(x)))count++; if(count==k)break; } if(x==-1){method=0;break;} String suf2=i2.substring(x); count=0; for(x=i3.length()-1;x>=0;x--) { if(vowel(i3.charAt(x)))count++; if(count==k)break; } if(x==-1){method=0;break;} String suf3=i3.substring(x); count=0; for(x=i4.length()-1;x>=0;x--) { if(vowel(i4.charAt(x)))count++; if(count==k)break; } if(x==-1){method=0;break;} String suf4=i4.substring(x); if(suf1.equals(suf2)&&suf3.equals(suf4)&&suf2.equals(suf3)){continue;} else if(suf1.equals(suf2)&&suf3.equals(suf4)){method=method&1;} else if(suf1.equals(suf3)&&suf2.equals(suf4)){method=method&2;} else if(suf1.equals(suf4)&&suf2.equals(suf3)){method=method&4;} else method=0; if(method==0)break; } if(method==1)System.out.println("aabb"); else if(method==2)System.out.println("abab"); else if(method==4)System.out.println("abba"); else if(method==7)System.out.println("aaaa"); 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
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class C { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new C().run(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } boolean isVovel(char c){ if(c == 'a' || c =='e' || c == 'o' || c == 'i' || c == 'u') return true; return false; } boolean compar(char[] a, char[] b, int st1, int st2){ if(a.length - st1 != b.length - st2) return false; for(int i = st1; i < a.length; i++){ if(a[i] != b[st2 + i - st1]) return false; } return true; } void solve() throws IOException{ int n = readInt(); int k = readInt(); int[] r = new int[n]; for(int i = 0; i < n; i++){ int[] start = new int[4]; char[][] s = new char[4][]; for(int j = 0; j < 4; j++){ char[] cur = readString().toCharArray(); int curK = k; int in = cur.length - 1; while(curK > 0 && in >= 0){ if(isVovel(cur[in])) curK--; in--; } in++; if(curK > 0){ out.println("NO"); return; } start[j] = in; s[j] = cur; } int[] q = new int[3]; for(int j = 1; j < 4; j++){ if(compar(s[0],s[j],start[0],start[j])){ if(j == 1 && compar(s[3],s[2],start[3],start[2])){ q[0] = 1; } if(j == 2 && compar(s[3],s[1],start[3],start[1])){ q[1] = 1; } if(j == 3 && compar(s[2],s[1],start[2],start[1])){ q[2] = 1; } } } if(q[1] == 0 && q[2] == 0 && q[0] == 0){ out.println("NO"); return; } if(q[1] != 0 && q[2] != 0 && q[0] != 0){ r[i] = 4; continue; } for(int j = 0; j < 3; j++){ if(q[j] != 0){ r[i] = j+1; } } } boolean check4 = true; int b = 0; for(int i = 0; i < n; i++){ if(r[i] != 4) { check4 = false; b = i; } } if(check4){ out.println("aaaa"); return; } for(int i = 0; i < n; i++){ if(r[i] != r[b] && r[i] != 4){ out.println("NO"); return; } } if(r[b] == 1) out.println("aabb"); if(r[b] == 2) out.println("abab"); if(r[b] == 3) out.println("abba"); } int[] zFunction(char[] s){ int[] z = new int[s.length]; z[0] = 0; for (int i=1, l=0, r=0; i<s.length; ++i) { if (i <= r) z[i] = min (r-i+1, z[i-l]); while (i+z[i] < s.length && s[z[i]] == s[i+z[i]]) ++z[i]; if (i+z[i]-1 > r){ l = i; r = i+z[i]-1; } } return z; } int[] prefixFunction(char[] s){ int[] pr = new int[s.length]; for (int i = 1; i< s.length; ++i) { int j = pr[i-1]; while (j > 0 && s[i] != s[j]) j = pr[j-1]; if (s[i] == s[j]) ++j; pr[i] = j; } return pr; } int ModExp(int a, int n, int mod){ int res = 1; while (n!=0) if ((n & 1) != 0) { res = (res*a)%mod; --n; } else { a = (a*a)%mod; n >>= 1; } return res; } static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } boolean isPrime(int a){ for(int i = 2; i <= sqrt(a); i++) if(a % i == 0) return false; return true; } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ if(min(a,b) == 0) return max(a,b); return gcd(max(a, b) % min(a,b), min(a,b)); } static long lcm(long a, long b){ return a * b /gcd(a, b); } }
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
def vowel(x): if x in list("aeiou"): return 1 return 0 aaaa,aabb,abab,abba=1,1,1,1 n,k=map(int,raw_input().split()) for i in range(n): temp=[] for j in range(4): vowels=0 x = raw_input() x=x[::-1] for it in range(len(x)): if vowel(x[it]): vowels+=1 if vowels==k: break if vowels<k: print "NO" exit() temp.append(x[:it+1][::-1]) a,b,c,d = temp[0],temp[1],temp[2],temp[3] # print a,b,c,d if "" in [a,b,c,d]: print "NO" aaaa,aabb,abab,abba=0,0,0,0 # exit() if a!=b or b!=c or c!=d: aaaa=0 if a!=b or c!=d: aabb=0 if a!=c or b!=d: abab=0 if a!=d or b!=c: abba=0 if aaaa: print "aaaa" elif aabb: print "aabb" elif abab: print "abab" elif 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
import sun.reflect.generics.tree.Tree; import java.io.*; import java.util.*; public class A2 { static Scanner in; static int next() throws Exception {return in.nextInt();}; // static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;} // static BufferedReader in; static PrintWriter out; static int count(String s, int k){ int c = 0, i; for (i = s.length()-1;i >= 0&&c!=k;i--) { if (s.charAt(i) == 'a'||s.charAt(i) == 'e'||s.charAt(i) == 'u'||s.charAt(i) == 'i'||s.charAt(i) == 'o') c++; } if ((s.charAt(i+1) == 'a'||s.charAt(i+1) == 'e'|| s.charAt(i+1) == 'u'||s.charAt(i+1) == 'i'||s.charAt(i+1) == 'o')&&c==k) i++; return(i); } public static void main(String[] args) throws Exception { in = new Scanner(System.in); // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); // in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = next(), k = next(); int res = 15; int t = 0; String s1, s2, s3, s4, u1, u2, u3, u4; int g1, g2, g3, g4; int h[] = new int[4]; Arrays.fill(h,1); for (int i = 0;i < n;i++) { s1 = in.next(); s2 = in.next(); s3 = in.next(); s4 = in.next(); g1 = count(s1,k); g2 = count(s2,k); g3 = count(s3,k); g4 = count(s4,k); u1 = g1 > -1?s1.substring(g1):""; u2 = g2 > -1?s2.substring(g2):""; u3 = g3 > -1?s3.substring(g3):""; u4 = g4 > -1?s4.substring(g4):""; if (g1 == -1 ||g2 == -1 ||g3 == -1 ||g4 == -1)t++; if (!(!u1.equals("")&&u1.equals(u2)&&!u3.equals("")&&u3.equals(u4))) h[0] = 0; if (!(!u1.equals("")&&u1.equals(u3)&&!u2.equals("")&&u2.equals(u4))) h[1] = 0; if (!(!u1.equals("")&&u1.equals(u4)&&!u2.equals("")&&u2.equals(u3))) h[2] = 0; if (!(!u1.equals("")&&u1.equals(u2)&&u2.equals(u3)&&u3.equals(u4))) h[3] = 0; } if (t > 0) out.println("NO"); else { if (h[3] == 1)out.println("aaaa"); else if (h[2] == 1) out.println("abba"); else if (h[1] == 1) out.println("abab"); else if (h[0] == 1) out.println("aabb"); else out.println("NO"); } out.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
n, k = map(int, input().split()) ve = 'aeiou' abba, aabb, abab = 1, 1, 1 for j in range(n): a = ['']*4 for i in range(4): a[i] = input() l = len(a[i]) curr = 0 while l > 0 and curr < k: l -= 1 if a[i][l] in ve: curr += 1 if curr == k: a[i] = a[i][l:] else: a[i] = str(i) if a[0] == a[3] and a[1] == a[2]: abba = abba and 1 else: abba = 0 if a[0] == a[1] and a[2] == a[3]: aabb = aabb and 1 else: aabb = 0 if a[0] == a[2] and a[1] == a[3]: abab = abab and 1 else: abab = 0 if abba and aabb and abab: print('aaaa') elif not(abba or aabb or abab): print('NO') elif abba: print('abba') elif abab: print('abab') elif aabb: print('aabb') # Made By Mostafa_Khaled
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] S = in.readLine().split(" "); int n = Integer.parseInt(S[0]); int k = Integer.parseInt(S[1]); String[] A = new String[4 * n]; for (int i = 0; i < 4 * n; i++) A[i] = in.readLine(); for (int i = 0; i < 4 * n; i++) { int v = 0; boolean done = false; for (int j = A[i].length() - 1; j >= 0; j--) { if ("aeiou".contains("" + A[i].charAt(j))) v++; if (v == k) { A[i] = A[i].substring(j); done = true; break; } } if (!done) { System.out.println("NO"); return; } } int[] M = new int[3]; for (int i = 0; i < n; i++) { if (A[4 * i].equals(A[4 * i + 1]) && A[4 * i + 2].equals(A[4 * i + 3])) M[0]++; if (A[4 * i].equals(A[4 * i + 2]) && A[4 * i + 1].equals(A[4 * i + 3])) M[1]++; if (A[4 * i].equals(A[4 * i + 3]) && A[4 * i + 2].equals(A[4 * i + 1])) M[2]++; } if (M[0] == n && M[1] == n && M[2] == n) System.out.println("aaaa"); else if (M[0] == n) System.out.println("aabb"); else if (M[1] == n) System.out.println("abab"); else if (M[2] == n) 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; public class problemC implements Runnable{ boolean[] vowels=new boolean[256]; { vowels['a']=true; vowels['e']=true; vowels['i']=true; vowels['o']=true; vowels['u']=true; } public void solve() throws IOException{ int[] inp=intArr(); int n=inp[0]; int k=inp[1]; String prevType=""; for(int i=0;i<n;i++){ String s[]=new String[4]; s[0]=nextLine(); s[1]=nextLine(); s[2]=nextLine(); s[3]=nextLine(); String t=type(s,k); if(i==0){ prevType=t; } else { if(t.equals("NO")){ prevType="NO"; } else if(t.equals("aaaa")){ // } else if(prevType.equals("aaaa")){ prevType=t; } else if(prevType.equals(t)){ // } else { prevType="NO"; } } if(prevType.equals("NO")){ break; } } out.println(prevType); } public String type(String[] s,int k){ boolean[][] equals=new boolean[4][4]; int[] len=new int[4]; int maxLen=-1; for(int i=0;i<4;i++){ len[i]=s[i].length(); if(len[i]>maxLen){ maxLen=len[i]; } for(int j=i+1;j<4;j++){ equals[i][j]=true; equals[j][i]=true; } } int[] vCount=new int[4]; int sumK=0; mainLoop: for(int l=0;l<maxLen;l++){ for(int i=0;i<4;i++){ if(sumK==k*4){ break mainLoop; } if(vCount[i]==k){ continue; } if(l==len[i]){ return "NO"; } char c=s[i].charAt(len[i]-1-l); if(vowels[c]){ vCount[i]++; sumK++; } for(int j=i+1;j<4;j++){ if(len[j]<=l || vCount[j]>=k){ equals[i][j]=false; equals[j][i]=false; continue; } char cc=s[j].charAt(len[j]-1-l); if(c==cc){ equals[i][j]&=true; equals[j][i]&=true; } else { equals[i][j]=false; equals[j][i]=false; } } } } if(sumK<k*4){ return "NO"; } if(equals[0][1] && equals[0][2] && equals[0][3]){ return "aaaa"; } else if(equals[0][1] && equals[2][3]){ return "aabb"; } else if(equals[0][2] && equals[1][3]){ return "abab"; } else if(equals[0][3] && equals[1][2]){ return "abba"; } else { return "NO"; } } public static void main(String[] args){ new problemC().run(); } @Override public void run(){ // System.out.println("trying to solve"); try{ in=new BufferedReader(new InputStreamReader(stdin)); out=new PrintWriter(stdout); solve(); in.close(); out.close(); } catch (Exception ex){ ex.printStackTrace(); System.exit(1); } } BufferedReader in; PrintWriter out; int[] intArr() throws IOException { String[] arr=nextLine().split("\\s"); int[] res=new int[arr.length]; for(int i=0;i<arr.length;i++) res[i]=Integer.parseInt(arr[i]); return res; } long[] longArr() throws IOException { String[] arr=nextLine().split("\\s"); long[] res=new long[arr.length]; for(int i=0;i<arr.length;i++) res[i]=Long.parseLong(arr[i]); return res; } double[] doubleArr() throws IOException { String[] arr=nextLine().split("\\s"); double[] res=new double[arr.length]; for(int i=0;i<arr.length;i++) res[i]=Double.parseDouble(arr[i]); return res; } String nextLine() throws IOException { return in.readLine(); } InputStream stdin; OutputStream stdout; public problemC(){ stdin=System.in; stdout=System.out; } public problemC(InputStream stdin, OutputStream stdout){ this.stdin=stdin; this.stdout=stdout; } }
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
#!/usr/bin/python VOWELS = ['a', 'e', 'o', 'u', 'i'] def solve(): n, k = map(int,raw_input().split()) poem = [] for i in xrange(4*n): s = raw_input().strip() i = len(s) for j in xrange(k): i -= 1 while i >= 0 and s[i] not in VOWELS: i -= 1 if i < 0: return "NO" poem.append(s[i:]) #print poem schemes = (((1, 2), (3, 4)), ((1, 3), (2, 4)), ((1, 4), (2, 3))) mask = 7 for i in xrange(n): cmask = 0 a, b, c, d = poem[i * 4 : (i + 1) * 4] if a == b and c == d: cmask |= 1 if a == c and b == d: cmask |= 2 if a == d and b == c: cmask |= 4 mask &= cmask ansmap = {7: 'aaaa', 1: 'aabb', 2: 'abab', 4: 'abba', 0: 'NO'} assert mask in ansmap return ansmap[mask] print solve()
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; string s[10010]; bool rifm(string s, string t) { int j = s.length() - 1, l = t.length() - 1, kol = 0; while (s[j] == t[l]) { if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') kol++; if (kol == k) return true; j--; l--; if (j < 0 || l < 0) break; } return false; } string prov(int i) { if (rifm(s[i], s[i + 1]) && rifm(s[i + 2], s[i + 3]) && rifm(s[i + 1], s[i + 2])) return "aaaa"; if (rifm(s[i], s[i + 1]) && rifm(s[i + 2], s[i + 3])) return "aabb"; if (rifm(s[i], s[i + 2]) && rifm(s[i + 1], s[i + 3])) return "abab"; if (rifm(s[i], s[i + 3]) && rifm(s[i + 1], s[i + 2])) return "abba"; return "NO"; } string ans = "", tmp; int main() { cin >> n >> k; for (int i = 1; i <= 4 * n; i++) cin >> s[i]; for (int i = 1; i <= 4 * n; i += 4) { tmp = prov(i); if (tmp == "NO") { cout << tmp; return 0; } if (ans == "") ans = tmp; if ((tmp != "aaaa" && ans != "aaaa") && ans != tmp) { cout << "NO"; return 0; } if (tmp != "aaaa") ans = tmp; } cout << ans; }
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.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.TreeSet; public class Main { private static char[] gl = {'a', 'e', 'i', 'o', 'u'}; private static int Type(String[] s) { if(s[0].equals(s[1]) && s[1].equals(s[2]) && s[2].equals(s[3]))return 0; if(s[0].equals(s[1]) && s[2].equals(s[3]))return 1; if(s[0].equals(s[2]) && s[1].equals(s[3]))return 2; if(s[0].equals(s[3]) && s[1].equals(s[2]))return 3; return -1; } public static void main(String[] args) throws Exception { TreeSet<Character> ts = new TreeSet<Character>(); for(int i = 0; i<gl.length; i++)ts.add(gl[i]); String[] ss = inB.readLine().split(" "); int n = Integer.parseInt(ss[0]); int k = Integer.parseInt(ss[1]); String[] mas = new String[4*n]; boolean end = false; for(int i = 0; i<4*n; i++) { String s = inB.readLine(); char[] c = s.toCharArray(); int count = 0; int j = 0; for(j = c.length-1; j>=0; j--) { if(ts.contains(c[j])) { count++; if(count == k) { break; } } } if(count < k) { end = true; } else { mas[i] = s.substring(j, c.length); } } if(end){ exit("NO"); } int type = 0; for(int i = 0; i<n; i++) { String[] s = new String[4]; for(int j = 0; j<4; j++)s[j] = mas[i*4 + j]; int curType = Type(s); if(curType == -1)exit("NO"); if(type == 0)type = curType; else { if(curType != 0 && curType != type) { exit("NO"); } } } println(type == 0 ? "aaaa" : (type == 1 ? "aabb" : (type == 2 ? "abab" : "abba"))); } ///////////////////////////////////////////////////////////////// // IO ///////////////////////////////////////////////////////////////// private static StreamTokenizer in; private static PrintWriter out; private static BufferedReader inB; private static boolean FILE=false; private static int nextInt() throws Exception{ in.nextToken(); return (int)in.nval; } private static String nextString() throws Exception{ in.nextToken(); return in.sval; } static{ try { out = new PrintWriter(FILE ? (new FileOutputStream("output.txt")) : System.out); inB = new BufferedReader(new InputStreamReader(FILE ? new FileInputStream("input.txt") : System.in)); } catch(Exception e) {e.printStackTrace();} in = new StreamTokenizer(inB); } ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// // pre - written ///////////////////////////////////////////////////////////////// private static void println(Object o) throws Exception { out.println(o); out.flush(); } private static void exit(Object o) throws Exception { println(o); exit(); } private static void exit() { System.exit(0); } private static final int INF = Integer.MAX_VALUE; private static final int MINF = Integer.MIN_VALUE; ////////////////////////////////////////////////////////////////// }
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; int n, k, res[4]; char s[10001]; vector<string> v, p; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) for (int j = 0; j < 4; ++j) { scanf("%s", s); v.push_back(s); } bool can = true; for (int i = 0; i < n; ++i) { p.clear(); for (int j = 0; j < 4; ++j) { string &s = v[i * 4 + j]; int at = s.size(), need = k; while (need && at > 0) { --at; if (s[at] == 'a' || s[at] == 'e' || s[at] == 'i' || s[at] == 'o' || s[at] == 'u') --need; } if (need != 0) { can = false; break; } p.push_back(s.substr(at)); } if (!can) break; if (p[0] == p[1] && p[0] == p[2] && p[0] == p[3]) ++res[0]; else if (p[0] == p[1] && p[2] == p[3]) ++res[1]; else if (p[0] == p[2] && p[1] == p[3]) ++res[2]; else if (p[0] == p[3] && p[1] == p[2]) ++res[3]; else { can = false; break; } } if (!can) { puts("NO"); return 0; } if (res[0] == n) puts("aaaa"); else if (res[0] + res[1] == n) puts("aabb"); else if (res[0] + res[2] == n) puts("abab"); else if (res[0] + res[3] == n) puts("abba"); else puts("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
vowel = ['a','e','i','o','u'] def aabb(pv): if pv[0]==pv[1] and pv[2]==pv[3]: return True else: return False def abab(pv): if pv[0]==pv[2] and pv[1]==pv[3]: return True else: return False def abba(pv): if pv[0]==pv[3] and pv[2]==pv[1]: return True else: return False line = raw_input().split() n = int(line[0]) k = int(line[1]) flag = 4 for i in range(n): p = [] pv = [] for ii in range(4): p.append(raw_input()) for item in p: rev = item[::-1] count = 0 pos = 0 l = len(rev) for j in range(l): ch = rev[j] if ch in vowel: count += 1 if count == k: pos = l-j-1 break if count != k: flag = 0 item = item[pos:l] pv.append(item) if (flag == 4 and aabb(pv) and abab(pv) and abba(pv)): flag = 4 elif (flag == 4 or flag == 1) and aabb(pv): flag = 1 elif (flag == 4 or flag == 2) and abab(pv): flag = 2 elif (flag == 4 or flag == 3) and abba(pv): flag = 3 else: flag = 0 break if (flag == 4): print "aaaa" elif (flag == 1): print "aabb" elif (flag == 2): print "abab" elif (flag == 3): 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
vowels = ['a', 'e', 'i', 'o', 'u'] def rhyme(a, b, k): ai = -1 c = 0 if (a[ai] in vowels): c += 1 while (c < k) and (abs(ai) < len(a)): ai -= 1 if (a[ai] in vowels): c += 1 if (c < k): return False bi = -1 c = 0 if (b[bi] in vowels): c += 1 while (c < k) and (abs(bi) < len(b)): bi -= 1 if (b[bi] in vowels): c += 1 if (c < k): return False return (a[ai:] == b[bi:]) n, k = [int(i) for i in raw_input().split()] a = [] for i in xrange(4 * n): a.append(raw_input()) globalRes = 15 for i in xrange(0, 4 * n, 4): res = 0 if (rhyme(a[i], a[i + 1], k) and rhyme(a[i + 1], a[i + 2], k) and rhyme(a[i + 2], a[i + 3], k)): res += 1 if (rhyme(a[i], a[i + 1], k) and rhyme(a[i + 2], a[i + 3], k)): res += 2 if (rhyme(a[i], a[i + 2], k) and rhyme(a[i + 1], a[i + 3], k)): res += 4 if (rhyme(a[i], a[i + 3], k) and rhyme(a[i + 1], a[i + 2], k)): res += 8 globalRes = globalRes & res if (globalRes & 1): print 'aaaa' elif (globalRes & 2): print 'aabb' elif (globalRes & 4): print 'abab' elif (globalRes & 8): 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 main() { int q, k, now, p; cin >> q >> k; string vowels[4], line; int scheme = 0; bool possible = true; for (int i = 0; i < q; i++) { now = 0; for (int j = 0; j < 4; j++) { cin >> line; vowels[j] = ""; for (p = line.length() - 1; p >= 0 && vowels[j].length() < k; p--) { char v = line[p]; if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u') vowels[j] += v; } if (vowels[j].length() < k) { possible = false; break; } vowels[j] = line.substr(p + 1); } if (vowels[0] == vowels[1] && vowels[2] == vowels[3]) { if (vowels[0] == vowels[2]) now = 0; else now = 1; } else if (vowels[0] == vowels[2] && vowels[1] == vowels[3]) { now = 2; } else if (vowels[0] == vowels[3] && vowels[1] == vowels[2]) { now = 3; } else possible = false; if (scheme == 0 || scheme == now) scheme = now; else if (now) possible = false; } string op; if (!possible) op = "NO"; else switch (scheme) { case 0: op = "aaaa"; break; case 1: op = "aabb"; break; case 2: op = "abab"; break; case 3: op = "abba"; break; } cout << op << 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.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class cf138a { public static void main(String[] args) { FastIO in = new FastIO(), out = in; int n = 4*in.nextInt(); int k = in.nextInt(); String[] v = new String[n]; for(int i=0; i<n; i++) { v[i] = trim(in.next().trim(), k); if(v[i] == null) { out.println("NO"); out.close(); return; } } boolean aaaa = true, aabb = true, abab = true, abba = true; for(int i=0; i<n; i+=4) { if(!v[i].equals(v[i+1])) { aaaa = false; aabb = false; } if(!v[i].equals(v[i+2])) { aaaa = false; abab = false; } if(!v[i].equals(v[i+3])) { aaaa = false; abba = false; } if(!v[i+1].equals(v[i+2])) { aaaa = false; abba = false; } if(!v[i+1].equals(v[i+3])) { aaaa = false; abab = false; } if(!v[i+2].equals(v[i+3])) { aaaa = false; aabb = false; } } if(aaaa) out.println("aaaa"); else if(abab) out.println("abab"); else if(aabb) out.println("aabb"); else if(abba) out.println("abba"); else out.println("NO"); out.close(); } static String vowels = "aeiou"; static String trim(String x, int k) { int count = 0; for(int i=x.length()-1; i>=0; i--) { if(vowels.indexOf(x.charAt(i)) >= 0) count++; if(count == k) return x.substring(i); } return null; } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { this(System.in,System.out); } public FastIO(InputStream in, OutputStream out) { super(new BufferedWriter(new OutputStreamWriter(out))); br = new BufferedReader(new InputStreamReader(in)); scanLine(); } public void scanLine() { try { st = new StringTokenizer(br.readLine().trim()); } catch(Exception e) { throw new RuntimeException(e.getMessage()); } } public int numTokens() { if(!st.hasMoreTokens()) { scanLine(); return numTokens(); } return st.countTokens(); } public String next() { if(!st.hasMoreTokens()) { scanLine(); return next(); } return st.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { 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 java.io.*; import java.math.*; import java.util.*; public class Solution { public static final String INPUT_FILE = "a.in"; public static final String OUTPUT_FILE = "a.out"; BufferedReader bf; StringTokenizer st; public String next() throws IOException { if (st == null || !st.hasMoreTokens()) { String line = bf.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String nextLine() throws IOException { String s = bf.readLine(); st = null; return s; } public boolean eq(char[] s1, char[] s2, int k) { int v = 0; int n = s1.length, m = s2.length; for(int i = 0, b = Math.min(n, m); i < b; i++) { if (s1[n - i - 1] != s2[m - i - 1]) return false; char c = s1[n - i - 1]; if (c == 'a' || c == 'u' || c == 'i' || c == 'e' || c == 'o') ++v; if (v == k) return true; } return false; } public Solution() throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int n = new Integer(st.nextToken()), k = new Integer(st.nextToken()); char[][][] s = new char[n][4][]; for(int i = 0; i < n; i++) { for(int j = 0; j < 4; j++) { s[i][j] = bf.readLine().toCharArray(); } } boolean[][] rythm = new boolean[n][3]; for(int i = 0; i < n; i++) { boolean mc[][] = new boolean[4][4]; for(int j = 0; j < 4; j++) { for(int l = j + 1; l < 4; l++) { mc[j][l] = eq(s[i][j], s[i][l], k); mc[l][j] = mc[j][l]; } } rythm[i][0] = mc[0][1] && mc[2][3]; rythm[i][1] = mc[0][2] && mc[1][3]; rythm[i][2] = mc[0][3] && mc[1][2]; } boolean[] t = new boolean[]{true, true, true}; for(int i = 0; i < n; i++) { for(int j = 0; j < 3; j++) { t[j] = t[j] && rythm[i][j]; } } if (t[0] && t[1] && t[2]) { System.out.println("aaaa"); return; } if (t[0]) { System.out.println("aabb"); return; } if (t[1]) { System.out.println("abab"); return; } if (t[2]) { System.out.println("abba"); return; } System.out.println("NO"); } public static void main(String args[]) throws IOException{ new Solution(); } }
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.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line = scan.nextLine(); int n = Integer.parseInt(line.substring(0, line.indexOf(' '))); int k = Integer.parseInt(line.substring(line.indexOf(' ') + 1)); int[] type = new int[5];// "aabb", "abab", "abba", "aaaa" - index 3 for (int i = 0; i < n; i++) { String[] q = new String[4]; Arrays.fill(q, ""); String[] endings = new String[4]; Arrays.fill(endings, ""); for (int j = 0; j < 4; j++) { q[j] = scan.nextLine(); int count = 0; for (int l = q[j].length() - 1; l >= 0; l--) { if (q[j].charAt(l) == 'a' || q[j].charAt(l) == 'e' || q[j].charAt(l) == 'i' || q[j].charAt(l) == 'o' || q[j].charAt(l) == 'u') count++; if (count == k) { endings[j] = q[j].substring(l); l = -1; } } if (count != k) type[4]++; } if (endings[0].equals(endings[1]) && endings[1].equals(endings[2]) && endings[2].equals(endings[3])) type[3]++; // aaaa else if (endings[0].equals(endings[1]) && endings[2].equals(endings[3])) type[0]++; // aabb else if (endings[0].equals(endings[2]) && endings[1].equals(endings[3])) type[1]++; // abab else if (endings[0].equals(endings[3]) && endings[1].equals(endings[2])) type[2]++; // abba else type[4]++; } if (type[4] > 0) System.out.println("NO"); else if (type[0] == 0 && type[1] == 0 && type[2] == 0) System.out.println("aaaa"); else if (type[2] > 0 && type[0] == 0 && type[1] == 0) System.out.println("abba"); else if (type[1] > 0 && type[0] == 0 && type[2] == 0) System.out.println("abab"); else if (type[0] > 0 && type[1] == 0 && type[2] == 0) 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
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; // Literature Lesson // 2011/12/24 public class P138A{ Scanner sc=new Scanner(System.in); 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(); 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); } } 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); } } } 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); } public static void main(String[] args){ new P138A().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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class C139 { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; String nextToken() throws IOException { if (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private void solution(String arg[]) throws IOException { int n = nextInt(); int k = nextInt(); int t = 0; int z = 0; int x = 0; int v = 1; int vv = 0; String[] mas = new String[4 * n]; ArrayList<Integer> l = new ArrayList(4 * n); for (int i = 0; i < n; i++) { String[] s = new String[4]; for (int j = 0; j < 4; j++) { s[j] = nextToken(); if (k > s[j].length()) { System.out.println("NO"); System.exit(0); } if (s[j].length() > 1) { while (x != k) { if (s[j].charAt(s[j].length() - v) == 'a' || s[j].charAt(s[j].length() - v) == 'e' || s[j].charAt(s[j].length() - v) == 'y' || s[j].charAt(s[j].length() - v) == 'i' || s[j].charAt(s[j].length() - v) == 'o' || s[j].charAt(s[j].length() - v) == 'u') { x++; vv = s[j].length() - v; } if (x != k) v++; if(v>s[j].length()){ System.out.println("NO"); System.exit(0); } } v = 1; x = 0; mas[t] = s[j].substring(vv, s[j].length()); t++; } else { mas[t] = s[j]; t++; } } if (mas[t - 4].equals(mas[t - 3]) && mas[t - 2].equals(mas[t - 1]) && mas[t - 4].equals(mas[t - 2])) { l.add(z, 0); z++; } else { if (mas[t - 4].equals(mas[t - 3]) && mas[t - 2].equals(mas[t - 1]) && !mas[t - 4].equals(mas[t - 2])) { l.add(z, 1); z++; } else { if (mas[t - 4].equals(mas[t - 2]) && mas[t - 3].equals(mas[t - 1]) && !mas[t - 4].equals(mas[t - 3])) { l.add(z, 2); z++; } else { if (mas[t - 4].equals(mas[t - 1]) && mas[t - 3].equals(mas[t - 2]) && !mas[t - 4].equals(mas[t - 3])) { l.add(z, 3); z++; } else { System.out.println("NO"); System.exit(0); } } } } } for (int c = 0; c < l.size(); c++) { int d = l.get(c); if (d == 0) { l.remove(c); c--; } } if (l.size() == 0) { System.out.println("aaaa"); } else { if (l.size() > 1) { for (int c = 0; c < l.size() - 1; c++) { int dd = l.get(c); int ddd = l.get(c + 1); if (dd != ddd) { System.out.println("NO"); System.exit(0); } } } if (l.contains(1)) { System.out.println("aabb"); } else { if (l.contains(2)) { System.out.println("abab"); } else { if (l.contains(3)) { System.out.println("abba"); } } } } } public static void main(String arg[]) throws IOException { new C139().solution(arg); } }
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; 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) == b.substr(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
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class c99 { public static void debug(Object... obs) { System.out.println(Arrays.deepToString(obs)); } final static String[]vow={"a", "e", "i", "o", "u" }; final static Set<String> vowS = new HashSet<String>(); public static void main(String[] args) { vowS.add("a"); vowS.add("e"); vowS.add("i"); vowS.add("o"); vowS.add("u"); Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int[]types=new int[n]; for(int i=0;i<n;i++) { String l1=sc.next(); String l2=sc.next(); String l3=sc.next(); String l4=sc.next(); types[i]=rhyme(k,l1,l2,l3,l4); } int tys=0; boolean set=false; boolean seen4=false; for(int i=0;i<types.length;i++) { if(set && tys!=types[i] && types[i]!=4) { System.out.println("NO"); return; } if(types[i]!=4){set=true; tys=types[i];} else seen4=true; } if(seen4 && tys==0) { tys=4; } switch(tys) { case 1:System.out.println("aabb");return; case 2:System.out.println("abab");return; case 3:System.out.println("abba");return; case 4:System.out.println("aaaa");return; case 5:System.out.println("NO");return; } } private static int rhyme(int k, String l1, String l2, String l3, String l4) { String su1=subvow(l1,k); String su2=subvow(l2,k); String su3=subvow(l3,k); String su4=subvow(l4,k); if(su1.equals("")||su2.equals("")||su3.equals("")||su4.equals("")) { return 5; } //debug(su1,su2,su3,su4); if(su1.equals(su2)) { if(su3.equals(su4) && !su3.equals(su1)) { return 1; } if(su3.equals(su4) && su3.equals(su1)) { return 4; } return 5; } if(su1.equals(su3)) { if(su2.equals(su4)) { return 2; } return 5; } if(su1.equals(su4)) { if(su2.equals(su3)) { return 3; } return 5; } return 5; } private static String subvow(String l1, int k) { for(int j=l1.length()-1;j>=0;j--) { if(vowS.contains(l1.substring(j, j+1))) { k--; } if(k==0) { return l1.substring(j); } } 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { boolean isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } String suffix(String s, int k) { for (int i = s.length() - 1; i >= 0; i--) if (isVowel(s.charAt(i))) { k--; if (k == 0) return s.substring(i); } return null; } private void solve() throws IOException { int n = ni(); int k = ni(); String[] a = new String[n * 4]; for (int j = 0; j < n * 4; j++) { if ((a[j] = suffix(ns(), k)) == null) { out.println("NO"); return; } } boolean aabb = true; boolean abab = true; boolean abba = true; for (int i = 0; i < n; i++) { boolean m12 = a[i * 4].equals(a[i * 4 + 1]); boolean m13 = a[i * 4].equals(a[i * 4 + 2]); boolean m14 = a[i * 4].equals(a[i * 4 + 3]); boolean m23 = a[i * 4 + 1].equals(a[i * 4 + 2]); boolean m24 = a[i * 4 + 1].equals(a[i * 4 + 3]); boolean m34 = a[i * 4 + 2].equals(a[i * 4 + 3]); aabb = aabb && m12 && m34; abab = abab && m13 && m24; abba = abba && m14 && m23; } if (aabb && abab && abba) out.println("aaaa"); else if (aabb) out.println("aabb"); else if (abab) out.println("abab"); else if (abba) out.println("abba"); else out.println("NO"); out.close(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; private int[][] na(int n) throws IOException { int[][] a = new int[n][2]; for (int i = 0; i < n; i++) { a[i][0] = ni(); a[i][1] = i; } return a; } String ns() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine(), " "); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(ns()); } long nl() throws IOException { return Long.parseLong(ns()); } double nd() throws IOException { return Double.parseDouble(ns()); } String[] nsa(int n) throws IOException { String[] res = new String[n]; for (int i = 0; i < n; i++) { res[i] = ns(); } return res; } int[] nia(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = ni(); } return res; } long[] nla(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nl(); } return res; } public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); tok = new StringTokenizer(""); Main main = new Main(); main.solve(); out.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.*; import java.math.*; public class c { String _token() throws IOException { while (in.ready() && !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } if (st.hasMoreTokens()) return st.nextToken(); return ""; } int k; int H = 239; String[]a; int get(int n, int p) { int m = 4*n+p; n = a[m].length(); char[] b = a[m].toCharArray(); int K = k; while(K>0 && n>0) { n--; if (b[n]=='a' || b[n]=='e' || b[n]=='i' || b[n]=='o' || b[n]=='u') K--; } if (K==0) { int ans=0; for(int i=n;i<b.length;i++) ans = ans*239 + b[i]; return ans; } return -1; } void do_solve() throws NumberFormatException, IOException { int n = _int(); k = _int(); a = new String[4*n]; for(int i=0;i<4*n;i++) { a[i] = in.readLine(); } int t[] = new int[n]; int ans=-1; for(int i=0;i<n;i++) { for(int j=0;j<4;j++) { for(int w=0;w<j;w++) { if (get(i,j) == get(i,w) && get(i,j)!=-1) { t[i] |= 1<<(4*w+j); } } } ans&=t[i]; } int aaaa=0; for(int i=0;i<4;i++) for(int j=0;j<i;j++) aaaa|=1<<(4*j+i); int abab = 0; abab |= 1<<(4*0+2); abab |= 1<<(4*1+3); int aabb = 0; aabb |= 1<<(4*0+1); aabb |= 1<<(4*2+3); int abba = 0; abba |= 1<<(4*0+3); abba |= 1<<(4*1+2); if (ans == aaaa) { out.println("aaaa"); } else { if (ans == abab) { out.println("abab"); } else { if (ans == abba) { out.println("abba"); } else { if (ans == aabb) { out.println("aabb"); } else { out.println("NO"); } } } } } void mainProgram() throws NumberFormatException, IOException { /* test("c.in"); */ init(false, "c"); boolean multy = false; if (!multy) { while (in.ready() || st.hasMoreTokens()) { do_solve(); } } else { for (int T = _int(); T > 0; --T) { do_solve(); } } out.close(); } int rand(int x) { return (int) (Math.random() * x); } void test(String name) throws IOException { out = new PrintWriter(new File(name)); out.close(); } void init(boolean FILE, String name) throws NumberFormatException, IOException { Locale.setDefault(Locale.US); if (FILE) { in = new BufferedReader(new FileReader(name + ".in")); out = new PrintWriter(name + ".out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } st = new StringTokenizer(""); } public static void main(String[] args) throws NumberFormatException, IOException { long T = System.currentTimeMillis(); new c().mainProgram(); System.err.println("Time of working: " + (System.currentTimeMillis() - T) + " ms"); } void deb(String s) { System.err.println(s); } void deb(Object a) { System.err.println(a.toString()); } BigInteger add(BigInteger A, BigInteger B) { return A.add(B); } BigInteger mult(BigInteger A, BigInteger B) { return A.multiply(B); } BigInteger sub(BigInteger A, BigInteger B) { return A.subtract(B); } BigInteger div(BigInteger A, BigInteger B) { return A.divide(B); } BigInteger mi(BigInteger A) { return (BigInteger.ZERO).subtract(A); } BigInteger sq(BigInteger A) { return A.multiply(A); } BigInteger val(long a) { return BigInteger.valueOf(a); } int cmp(BigInteger a, BigInteger b) { return a.compareTo(b); } BufferedReader in; StringTokenizer st; PrintWriter out; int _int() throws NumberFormatException, IOException { return Integer.parseInt(_token()); } long _long() throws NumberFormatException, IOException { return Long.parseLong(_token()); } double _double() throws NumberFormatException, IOException { return Double.parseDouble(_token()); } int gcd(int a, int b) { if (a < 0) a = -a; if (b < 0) b = -b; if (b == 0) return a; return gcd(b, a % b); } long gcd(long a, long b) { if (a < 0) a = -a; if (b < 0) b = -b; if (b == 0) return a; return gcd(b, a % b); } BigInteger gcd(BigInteger a, BigInteger b) { if (cmp(a, val(0)) < 0) a = mi(a); if (cmp(b, val(0)) < 0) b = mi(b); return a.gcd(b); } }
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.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class TAskC { public static Set<Character> vowels; public static int n; public static int k; public static final int aabb=1; public static final int abab=2; public static final int abba=3; public static final int aaaa=4; public static HashMap<Integer, String> hash = new HashMap<Integer, String>(){ { put(1, "aabb"); put(2, "abab"); put(3, "abba"); put(4, "aaaa"); } }; public static String getSuffix(String s1, int k) { int count = 0; int index = -1; for (int i = s1.length()-1; i >= 0; --i) { if (vowels.contains(s1.charAt(i))) { count++; if(count==k) { index = i; break; } } } if (index == -1) return "-1"; else return s1.substring(index); } public static int fintRythm(String... a) { String suff0 = getSuffix(a[0], k); String suff1 = getSuffix(a[1], k); String suff2 = getSuffix(a[2], k); String suff3 = getSuffix(a[3], k); if (suff0.equals("-1") || suff1.equals("-1")||suff2.equals("-1")||suff3.equals("-1")) { return -1; } if (suff0.equals(suff1) && suff0.equals(suff2) && suff0.equals(suff3)){ return aaaa; } if (suff0.equals(suff1) && suff2.equals(suff3)){ return aabb; } if (suff0.equals(suff2) && suff1.equals(suff3)){ return abab; } if (suff0.equals(suff3) && suff1.equals(suff2)){ return abba; } return -1; } public static void main(String[] args) throws IOException { PrintWriter jout = new PrintWriter(System.out); Scanner jin = new Scanner(new InputStreamReader(System.in)); n = jin.nextInt(); k = jin.nextInt(); vowels=new HashSet<Character>(){ { add('a'); add('e'); add('i'); add('o'); add('u'); } }; String[][] poem = new String[n][4]; for (int i = 0; i < n; ++i) { for (int j = 0; j < 4; ++j) { poem[i][j] = jin.next(); } } Set<Integer> ryphmType = new HashSet(); for (int i = 0; i < n; ++i) { if (fintRythm(poem[i])==-1) { System.out.println("NO"); return; } ryphmType.add(fintRythm(poem[i])); } if (ryphmType.contains(-1)) { System.out.println("NO)"); return; } if (ryphmType.size()>=3) { System.out.println("NO"); return; } if (ryphmType.size()==1) { ArrayList<Integer> list = new ArrayList<Integer>(); for (Integer i:ryphmType) { list.add(i); } System.out.println(hash.get(list.get(0))); return; } if (ryphmType.size()==2) { ArrayList<Integer> list = new ArrayList<Integer>(); for (Integer i:ryphmType) { list.add(i); } if (list.get(0)==aaaa) { System.out.println(hash.get(list.get(1))); return; } if (list.get(1)==aaaa) { System.out.println(hash.get(list.get(0))); 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
import java.util.*; public class c { public static void main(String[] args) { HashMap<String,Integer> HM = new HashMap<String,Integer>(); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); Scanner temp = new Scanner(input); int Q = temp.nextInt(); int K = temp.nextInt(); int totz = 1; String[] book = new String[Q<<2]; int[] result = new int[Q<<2]; for(int a=0;a<Q<<2;a++){ book[a]=sc.nextLine(); } for(int a=0;a<Q<<2;a++){ int tk = K; for(int b=book[a].length()-1;b>=0;b--){ if(book[a].charAt(b)=='a'|| book[a].charAt(b)=='e'|| book[a].charAt(b)=='i'|| book[a].charAt(b)=='o'|| book[a].charAt(b)=='u')tk--; if(tk == 0){ String x =book[a].substring(b); if(HM.containsKey(x))result[a]=HM.get(x); else { HM.put(x,totz++); result[a]=HM.get(x); } break; } } if(result[a]==0){ System.out.println("NO"); System.exit(0); } } boolean aabb = true; boolean abab = true; boolean abba = true; boolean aaaa = true; for(int a=0;a<((Q<<2));a+=4){ if(!((result[a]==result[a+1])&&(result[a+2]==result[a+3]))){ aabb=false; } if(!((result[a]==result[a+3])&&(result[a+2]==result[a+1]))){ abba=false; } if(!((result[a]==result[a+2])&&(result[a+1]==result[a+3]))){ abab=false; } if(!((result[a]==result[a+1])&&(result[a+1]==result[a+3])&&(result[a+2]==result[a+3]))){ aaaa=false; } } if(aaaa)System.out.println("aaaa"); else if(aabb) System.out.println("aabb"); else if(abba) System.out.println("abba"); else if(abab) System.out.println("abab"); 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 char vow[5] = {'a', 'e', 'i', 'o', 'u'}; string S[4]; int C[3]; void doMagic() { if (S[0] == S[1] && S[2] == S[3]) C[0]++; if (S[0] == S[2] && S[1] == S[3]) C[1]++; if (S[0] == S[3] && S[1] == S[2]) C[2]++; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { string tmp; cin >> tmp; int cnt = 0; for (int i = tmp.length() - 1; i >= 0 && cnt < k; i--) { for (int j = 0; j < 5; j++) if (tmp[i] == vow[j]) { cnt++; break; } S[j].push_back(tmp[i]); } if (cnt < k) { cout << "NO"; goto exit; } } doMagic(); } if (C[0] == n && C[1] == n && C[2] == n) { cout << "aaaa"; goto exit; } if (C[0] == n) { cout << "aabb"; goto exit; } if (C[1] == n) { cout << "abab"; goto exit; } if (C[2] == n) { cout << "abba"; goto exit; } cout << "NO"; exit: 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.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] parts = br.readLine().split("\\s+"); int n = Integer.parseInt(parts[0]); int k = Integer.parseInt(parts[1]); char[][][] poem = new char[n][4][]; for (int i = 0; i < 4 * n; i++) { poem[i / 4][i % 4] = br.readLine().toCharArray(); } Map<Integer, String> rhythm = new HashMap<Integer, String>(); for (int i = 0; i < n; i++) { boolean[][] isRhythm = new boolean[4][4]; int[]lastVocalIdx = new int[4]; lastVocalIdx[0] = getVocalIdx(poem[i][0], k); lastVocalIdx[1] = getVocalIdx(poem[i][1], k); lastVocalIdx[2] = getVocalIdx(poem[i][2], k); lastVocalIdx[3] = getVocalIdx(poem[i][3], k); for (int j = 0; j < 4; j++) { if(lastVocalIdx[j] == -1)continue; for (int m = j+1; m < 4; m++) { if(lastVocalIdx[m] == -1)continue; int lenJ = poem[i][j].length - lastVocalIdx[j]; int lenM = poem[i][m].length - lastVocalIdx[m]; if(lenJ != lenM)continue; boolean cmpRes = true; for(int len = 0; len < lenJ; len++){ if(poem[i][j][lastVocalIdx[j]+len]!=poem[i][m][lastVocalIdx[m]+len]){ cmpRes = false; break; } } isRhythm[j][m] = cmpRes; isRhythm[m][j] = cmpRes; } } if (isRhythm[0][1] && isRhythm[1][2] && isRhythm[2][3]) { rhythm.put(i, "aaaa"); } else if (isRhythm[0][1] && isRhythm[2][3]) { rhythm.put(i, "aabb"); } else if (isRhythm[0][3] && isRhythm[1][2]) { rhythm.put(i, "abba"); } else if (isRhythm[0][2] && isRhythm[1][3]) { rhythm.put(i, "abab"); } else { rhythm.put(i, "NO"); } } Map<String, Integer> resMap = new HashMap<String, Integer>(); Iterator<Integer> iter = rhythm.keySet().iterator(); while (iter.hasNext()) { String rhythmStr = rhythm.get(iter.next()); Integer num = resMap.get(rhythmStr); if(num == null){ num = 0; } num++; resMap.put(rhythmStr, num); } int size = resMap.keySet().size(); if(size>2||size==0){ System.out.println("NO"); } else if(size==2){ Iterator<String> iter2 = resMap.keySet().iterator(); String a = iter2.next(); String b = iter2.next(); if("NO".equals(a)||"NO".equals(b)){ System.out.println("NO"); } else if("aaaa".equals(a)||"aaaa".equals(b)){ if(!"aaaa".equals(a)){ System.out.println(a); } else { System.out.println(b); } } else { System.out.println("NO"); } } else { Iterator<String> iter1 = resMap.keySet().iterator(); System.out.println(iter1.next()); } } public static int getVocalIdx(char[] a, int k) { for (int i = a.length - 1; i >= 0; i--) { if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') { --k; if (k == 0) 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.Arrays; import java.util.Scanner; import java.util.Vector; public class CF136B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); String[] poem = new String[4 * n]; int glob = 0; for (int i = 0; i < 4 * n; ++i) { String tmp = in.next(); int cnt = 0; StringBuilder z = new StringBuilder(); for (int j = tmp.length() - 1; j >= 0 && cnt < k; --j) { if (tmp.charAt(j) == 'a' || tmp.charAt(j) == 'e' || tmp.charAt(j) == 'i' || tmp.charAt(j) == 'o' || tmp.charAt(j) == 'u') cnt++; z.append(tmp.charAt(j)); } z.reverse(); if (cnt >= k) poem[i] = z.toString(); else { ++glob; poem[i] = Integer.toString(glob); } } // System.out.println(Arrays.toString(poem)); boolean aaaa = true, aabb = true, abab = true, abba = true; for (int i = 0; i < n; ++i) { if (!(poem[4 * i].equals(poem[4 * i + 1]) && poem[4 * i + 1].equals(poem[4 * i + 2]) && poem[4 * i + 2] .equals(poem[4 * i + 3]))) aaaa = false; if (!(poem[4 * i].equals(poem[4 * i + 1]) && poem[4 * i + 2].equals(poem[4 * i + 3]))) aabb = false; if (!(poem[4 * i].equals(poem[4 * i + 2]) && poem[4 * i + 1].equals(poem[4 * i + 3]))) abab = false; if (!(poem[4 * i].equals(poem[4 * i + 3]) && poem[4 * i + 1].equals(poem[4 * i + 2]))) abba = false; } if (aaaa) 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> using namespace std; int N, K; string pm[2510][4]; string vol = "aeiou"; int tp[2510]; int bi[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256}; int getSufId(string &s) { int cnt = 0; for (int i = s.size() - 1; i >= 0; --i) { if (vol.find(s[i]) != -1) { ++cnt; if (cnt == K) return i; } } return -1; } int getType(int id) { string pos[4]; for (int i = 0; i < 4; ++i) { int p = getSufId(pm[id][i]); if (p != -1) { pos[i] = pm[id][i].substr(p); } else return -1; } int res = 0; if (pos[0] == pos[1] && pos[1] == pos[2] && pos[2] == pos[3]) { res = 7; } if (pos[0] == pos[1] && pos[2] == pos[3]) { res |= 4; } if (pos[0] == pos[2] && pos[1] == pos[3]) { res |= 2; } if (pos[0] == pos[3] && pos[1] == pos[2]) { res |= 1; } return res; } int main() { while (cin >> N >> K) { for (int i = 0; i < N; ++i) { for (int j = 0; j < 4; ++j) { cin >> pm[i][j]; } } int f = 1; int id[N]; int h[20] = {0}; for (int i = 0; i < N; ++i) { id[i] = getType(i); if (id[i] == -1) { f = 0; puts("NO"); break; } else { h[id[i]]++; } } if (f) { if (h[7] == N) puts("aaaa"); else if (h[7] + h[4] == N) puts("aabb"); else if (h[7] + h[2] == N) puts("abab"); else if (h[7] + h[1] == N) puts("abba"); else puts("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 string vow = "aeiou"; string line, suf[4], last; int n, k; bool ok; inline int get_suf() { int i, j; for (i = line.size() - 1, j = 0; i >= 0 && j < k; --i) { if (vow.find(line[i]) != string::npos) ++j; } if (j != k) ok = false; return i + 1; } inline string get_rhyme() { string rhyme = "abcd"; if (!ok) return rhyme; else if (suf[0] == suf[1] && suf[1] == suf[2] && suf[2] == suf[3]) rhyme = "aaaa"; else if (suf[0] == suf[1] && suf[2] == suf[3]) rhyme = "aabb"; else if (suf[0] == suf[2] && suf[1] == suf[3]) rhyme = "abab"; else if (suf[0] == suf[3] && suf[1] == suf[2]) rhyme = "abba"; return rhyme; } int main() { cin >> n >> k; ok = true; for (int i = 0; i < 4; ++i) { cin >> line; suf[i] = line.substr(get_suf()); } last = get_rhyme(); if (last == "abcd") { cout << "NO"; return 0; } while (--n > 0) { ok = true; for (int i = 0; i < 4; ++i) { cin >> line; suf[i] = line.substr(get_suf()); } string tmp = get_rhyme(); if (last != tmp) { if (tmp == "aaaa") ; else if (last == "aaaa" && tmp != "abcd") last = tmp; else { cout << "NO"; return 0; } } } cout << last; 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
from sys import stdin read = stdin.readline n,k = map(int,read().split()) r = 0 vowel = {'a','e','i','o','u'} def cmp(a,b,k,vowel): l = 0 for x,y in zip(reversed(a),reversed(b)): if x != y: return False l += (x in vowel) if l == k: return True return False im = True for s in range(n): a,b,c,d = read(),read(),read(),read() l = 0 for i,x in enumerate(reversed(a),start=1): l += (x in vowel) if l == k: break else: print('NO') break b2,b3,b4 = (a[-i:] == b[-i:]),(a[-i:] == c[-i:]),(a[-i:] == d[-i:]) if b2 + b3 + b4 == 3: continue elif b2 + b3 + b4 == 1: if b2 and (r == 1 or r == 0) and cmp(c,d,k,vowel): r = 1 continue elif b3 and (r==2 or r == 0) and cmp(b,d,k,vowel): r = 2 continue elif b4 and (r == 3 or r == 0) and cmp(b,c,k,vowel): r = 3 continue print('NO') break else: print(['aaaa','aabb','abab','abba'][r])
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
vowels = set('aeiou') def suf(s, k): global vowels i = 0 l_s = len(s) - 1 while l_s >= 0: if s[l_s] in vowels: i += 1 if i == k: return s[l_s:] l_s -= 1 print "NO" exit(0) r = raw_input n,k = map(int,r().strip().split(' ')) w_s = 7 p_s = [suf(r().strip(),k) for i in xrange(4 * n)] for base in xrange(0,4 * n,4): s = 0 if p_s[base] == p_s[base + 1] and p_s[base + 2] == p_s[base + 3]: s |= 1 if p_s[base] == p_s[base + 2] and p_s[base + 1] == p_s[base + 3]: s |= 2 if p_s[base] == p_s[base + 3] and p_s[base + 1] == p_s[base + 2]: s |= 4 w_s &= s if w_s == 0: print 'NO' exit(0) ans = {1:'aabb', 2:'abab', 4:'abba', 7:'aaaa'} print ans[w_s]
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; string getsuf(const string &s) { int it = 0; for (int i = s.size() - 1; i > -1; i--) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') it++; if (it == k) return s.substr(i); } return "-1"; } int getType(vector<string> &s) { string p = getsuf(s[0]); string p1 = getsuf(s[1]); string p2 = getsuf(s[2]); string p3 = getsuf(s[3]); if (p == "-1" || p1 == "-1" || p2 == "-1" || p3 == "-1") return -1; if (p == p1 && p == p2 && p == p3) return 1; if (p == p1 && p2 == p3) return 2; if (p == p2 && p1 == p3) return 3; if (p == p3 && p1 == p2) return 4; return -1; } int main() { cin >> n >> k; set<int> ty; while (n--) { vector<string> s(4); for (int i = 0; i < 4; i++) { cin >> s[i]; } int r = getType(s); if (r != -1) ty.insert(r); else { cout << "NO" << endl; return 0; } } string str[] = {"aaaa", "aabb", "abab", "abba"}; if (ty.size() == 1) { cout << str[*ty.begin() - 1]; } else if (ty.size() == 2 && (*ty.begin()) == 1) { cout << str[*(++ty.begin()) - 1]; } 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.Arrays; public class CF139C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String fstl = br.readLine(); int spacePos = fstl.indexOf(' '); int n = Integer.parseInt(fstl.substring(0, spacePos)); int k = Integer.parseInt(fstl.substring(spacePos+1)); boolean isAABB = true; boolean isABAB = true; boolean isABBA = true; boolean[][] isRhyme = new boolean[4][4]; boolean[] isVowel = new boolean[256]; isVowel['a'] = isVowel['e'] = isVowel['i'] = isVowel['o'] = isVowel['u'] = true; for (int i = 0; i < n; i++) { String[] suf = new String[4]; for (int k2 = 0; k2 < 4; k2++) { String line = br.readLine(); for (int j = line.length() - 1, count = 0; j >= 0; j--) { if (isVowel[line.charAt(j)]) { count++; if (count == k) { suf[k2] = line.substring(j); } } } } for (int j = 0; j < 4; j++) { Arrays.fill(isRhyme[j], false); } for (int j = 0; j < 3; j++) { for (int j2 = j+1; j2 < 4; j2++) { if (suf[j] != null && suf[j].equals(suf[j2])) { isRhyme[j][j2] = isRhyme[j2][j] = true; } } } if (isAABB) isAABB = isRhyme[0][1] && isRhyme[2][3]; if (isABAB) isABAB = isRhyme[0][2] && isRhyme[1][3]; if (isABBA) isABBA = isRhyme[0][3] && isRhyme[1][2]; } System.out.println(isAABB && isABAB && isABBA ? "aaaa" : isAABB ? "aabb" : isABAB ? "abab" : isABBA ? "abba" : "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; string S[4] = {"aabb", "abab", "abba", "aaaa"}; string ans[5]; int pipei[2505]; string s[2505][4]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, K; while (cin >> n >> K) { int flag = 0; for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) cin >> s[i][j]; int Flag = 0; for (int i = 0; i < n; i++) { int flag = 0; for (int j = 0; j < 4; j++) { int len = s[i][j].size(); int cont = 0; int ok = 0; int id = -1; for (int k = len - 1; k >= 0; k--) { if (s[i][j][k] == 'a' || s[i][j][k] == 'e' || s[i][j][k] == 'i' || s[i][j][k] == 'o' || s[i][j][k] == 'u') cont++; if (cont == K) { id = k; ok = 1; break; } } if (!ok) break; if (j == 3) flag = 1; ans[j] = s[i][j].substr(id, len); } if (!flag) break; if (i == n - 1) Flag = 1; if (ans[0] == ans[1] && ans[1] == ans[2] && ans[2] == ans[3]) pipei[i] = 3; else if (ans[0] == ans[1] && ans[2] == ans[3]) pipei[i] = 0; else if (ans[0] == ans[2] && ans[1] == ans[3]) pipei[i] = 1; else if (ans[0] == ans[3] && ans[1] == ans[2]) pipei[i] = 2; else { Flag = 0; break; } if (i == 0) continue; if (pipei[i] != pipei[i - 1] && pipei[i] != 3 && pipei[i - 1] != 3) { Flag = 0; break; } if (pipei[i] == 3) pipei[i] = pipei[i - 1]; } if (!Flag) { cout << "NO" << endl; continue; } cout << S[pipei[n - 1]] << 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.*; /** * Created by IntelliJ IDEA. * User: piyushd * Date: 3/26/11 * Time: 10:53 PM * To change this template use File | Settings | File Templates. */ public class TaskC { String vowels = "aeiou"; void run(){ int n = nextInt(), k = nextInt(); String[] suffixes = new String[4 * n]; Arrays.fill(suffixes, ""); for(int i = 0; i < 4 * n; i++) { String x = next(); int cnt = 0; for(int j = x.length() - 1; j >= 0; j--) { if(vowels.indexOf(x.charAt(j)) != -1) cnt++; if(cnt == k) { suffixes[i] = x.substring(j); break; } } } String[] type = new String[n]; for(int i = 0; i < n; i++) { boolean[][] match = new boolean[4][4]; for(int a = 0; a < 4; a++) { if(suffixes[4 * i + a].isEmpty()) continue; for(int b = a + 1; b < 4; b++) { if(suffixes[4 * i + a].equals(suffixes[4 * i + b])) match[a][b] = true; } } if(match[0][1] && match[0][2] && match[0][3] && match[1][2] && match[1][3] && match[2][3]) type[i] = "aaaa"; else if(match[0][1] && match[2][3]) type[i] = "aabb"; else if(match[0][2] && match[1][3]) type[i] = "abab"; else if(match[0][3] && match[1][2]) type[i] = "abba"; else type[i] = "NO"; } HashSet<String> unique = new HashSet<String>(); for(String x : type) unique.add(x); if(unique.size() > 2) { System.out.println("NO"); } else if(unique.size() == 2) { Iterator<String> it = unique.iterator(); String x1 = it.next(), x2 = it.next(); if(x1.equals("aaaa")) { System.out.println(x2); } else if(x2.equals("aaaa")) { System.out.println(x1); } else { System.out.println("NO"); } } else { System.out.println(unique.iterator().next()); } } int nextInt(){ try{ int c = System.in.read(); if(c == -1) return c; while(c != '-' && (c < '0' || '9' < c)){ c = System.in.read(); if(c == -1) return c; } if(c == '-') return -nextInt(); int res = 0; do{ res *= 10; res += c - '0'; c = System.in.read(); }while('0' <= c && c <= '9'); return res; }catch(Exception e){ return -1; } } long nextLong(){ try{ int c = System.in.read(); if(c == -1) return -1; while(c != '-' && (c < '0' || '9' < c)){ c = System.in.read(); if(c == -1) return -1; } if(c == '-') return -nextLong(); long res = 0; do{ res *= 10; res += c-'0'; c = System.in.read(); }while('0' <= c && c <= '9'); return res; }catch(Exception e){ return -1; } } double nextDouble(){ return Double.parseDouble(next()); } String next(){ try{ StringBuilder res = new StringBuilder(""); int c = System.in.read(); while(Character.isWhitespace(c)) c = System.in.read(); do{ res.append((char)c); }while(!Character.isWhitespace(c=System.in.read())); return res.toString(); }catch(Exception e){ return null; } } String nextLine(){ try{ StringBuilder res = new StringBuilder(""); int c = System.in.read(); while(c == '\r' || c == '\n') c = System.in.read(); do{ res.append((char)c); c = System.in.read(); }while(c != '\r' && c != '\n'); return res.toString(); }catch(Exception e){ return null; } } public static void main(String[] args){ new TaskC().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
#include <bits/stdc++.h> using namespace std; int oo = (int)1e9; string vw = "aeiou"; int main() { int n, k; scanf("%d%d", &n, &k); n; int flg = 1; int aabb = 1, abab = 1, aaaa = 1, abba = 1; for (int i = 0; i < n; ++i) { string str; vector<string> vs; for (int j = 0; j < 4; ++j) { cin >> str; vs.push_back(str); } vector<string> suf; for (int j = 0; j < 4; ++j) { string tmp = ""; int t = k; for (int l = vs[j].size() - 1; l >= 0 && t; --l) { if (find(vw.begin(), vw.end(), vs[j][l]) != vw.end()) --t; tmp += vs[j][l]; } if (t == 0) suf.push_back(tmp); } if (suf.size() != 4) { flg = 0; break; } if (suf[0] != suf[1] || suf[2] != suf[3]) aabb = 0; if (suf[0] != suf[2] || suf[1] != suf[3]) abab = 0; if (suf[0] != suf[3] || suf[1] != suf[2]) abba = 0; set<string> st(suf.begin(), suf.end()); if (st.size() > 1) aaaa = 0; } if (flg == 0 || !(aabb || abab || abba || aaaa)) cout << "NO\n"; else { if (aaaa) cout << "aaaa\n"; else if (abba) cout << "abba\n"; else if (abab) cout << "abab\n"; else if (aabb) cout << "aabb\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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; public class C { public static void main( final String[] args ) throws IOException { final BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); final String[] parts = br.readLine().split( " " ); final int n = Integer.parseInt( parts[ 0 ] ); final int k = Integer.parseInt( parts[ 1 ] ); final String[] poem = new String[4 * n]; for ( int i = 0; i < 4 * n; ++i ) { poem[ i ] = transform( br.readLine(), k ); if ( poem[ i ] == null ) { System.out.println( "NO" ); return; } } final Set<String>[] set = new HashSet[n]; for ( int i = 0; i < n; ++i ) { set[ i ] = getSets( poem, 4 * i ); } boolean allAAAA = set[ 0 ].contains( "aaaa" ); for ( int i = 1; i < n; ++i ) { set[ 0 ].retainAll( set[ i ] ); allAAAA = allAAAA && set[ i ].contains( "aaaa" ); } if ( allAAAA ) System.out.println( "aaaa" ); else if ( set[ 0 ].isEmpty() ) System.out.println( "NO" ); else System.out.println( set[ 0 ].iterator().next() ); } private static Set<String> getSets( final String[] poem, final int i ) { final Set<String> set = new HashSet<String>(); if ( poem[ i ].equals( poem[ i + 1 ] ) && poem[ i ].equals( poem[ i + 2 ] ) && poem[ i ].equals( poem[ i + 3 ] ) ) { set.add( "aaaa" ); } if ( poem[ i ].equals( poem[ i + 2 ] ) && poem[ i + 1 ].equals( poem[ i + 3 ] ) ) { set.add( "abab" ); } if ( poem[ i ].equals( poem[ i + 1 ] ) && poem[ i + 2 ].equals( poem[ i + 3 ] ) ) { set.add( "aabb" ); } if ( poem[ i ].equals( poem[ i + 3 ] ) && poem[ i + 2 ].equals( poem[ i + 1 ] ) ) { set.add( "abba" ); } return set; } private static String transform( final String line, final int k ) { final String r = line.replaceAll( "([aeiou])", "|$1" ); final String[] parts = r.split( "[|]" ); if ( parts.length <= k ) return null; final StringBuilder res = new StringBuilder(); for ( int i = 0; i < k; ++i ) { res.insert( 0, parts[ parts.length - 1 - i ] ); } return res.toString(); } }
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> T; int n, k; long long p = 31; bool glas(char c) { if (c == 'a' || c == 'o' || c == 'u' || c == 'e' || c == 'i') return true; else return false; } int type_of(string &s1, string &s2, string &s3, string &s4) { long long hash1 = 0, hash2 = 0, hash3 = 0, hash4 = 0; long long cur_p = 1; int dt = k; for (int i = int(s1.size()) - 1; i >= 0, dt > 0; i--) { if (glas(s1[i])) dt--; hash1 += (s1[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; cur_p = 1; dt = k; for (int i = int(s2.size()) - 1; i >= 0, dt > 0; i--) { if (glas(s2[i])) dt--; hash2 += (s2[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; cur_p = 1; dt = k; for (int i = int(s3.size()) - 1; i >= 0, dt > 0; i--) { if (glas(s3[i])) dt--; hash3 += (s3[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; cur_p = 1; dt = k; for (int i = int(s4.size()) - 1; i >= 0, dt > 0; i--) { if (glas(s4[i])) dt--; hash4 += (s4[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; if (hash1 == hash2 && hash2 == hash3 && hash3 == hash4 && hash4 == hash1) return 4; if (hash1 == hash2 && hash3 == hash4) return 3; if (hash1 == hash3 && hash2 == hash4) return 2; if (hash1 == hash4 && hash2 == hash3) return 1; return 0; } int main() { cin >> n >> k; string s1, s2, s3, s4; for (int i = 0; i < n; i++) { cin >> s1 >> s2 >> s3 >> s4; T.push_back(type_of(s1, s2, s3, s4)); if (T.back() == 0) { cout << "NO" << endl; return 0; } } int frst = -1; for (int i = 0; i < T.size(); i++) { if (T[i] == 4) continue; if (frst == -1) { frst = T[i]; continue; } if (T[i] != frst) { cout << "NO" << endl; return 0; } } if (frst == -1) cout << "aaaa" << endl; if (frst == 3) cout << "aabb" << endl; if (frst == 2) cout << "abab" << endl; if (frst == 1) 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
import java.util.*; import java.io.*; import java.lang.ref.Reference; import java.math.BigDecimal; import java.math.BigInteger; public class Main { Scanner in; PrintWriter out; public static void main(String[] s) { new Main().run(); } void run() { in = new Scanner(System.in); out = new PrintWriter(System.out); try { solve(); } finally { out.close(); } } int k; class FourVerse { String s[]; FourVerse() { s = new String[4]; for (int i = 0; i < 4; i++) { s[i] = in.next(); } } String getSuff(int index) { String s = this.s[index]; int countSuf = k; for (int i = s.length() - 1; i >= 0; i--) { if (countSuf == 0) { return s.substring(i + 1, s.length()); } char ch = s.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { countSuf--; } } if (countSuf > 0) { return "Badly"; } return s; } byte[] getType() { String[] a = new String[4]; byte[] ans = new byte[4]; for (int i = 0; i < 4; i++) { a[i] = getSuff(i); if (a[i].equals("Badly")) return ans; } String[] b = a.clone(); Arrays.sort(b); if (b[0].equals(b[3])) { ans[3] = 1; } if (a[0].equals(a[1]) && a[2].equals(a[3])) { ans[0] = 1; } if (a[0].equals(a[2]) && a[1].equals(a[3])) { ans[1] = 1; } if (a[0].equals(a[3]) && a[1].equals(a[2])) { ans[2] = 1; } return ans; } } void solve() { final String[] type = { "aabb", "abab", "abba", "aaaa" }; int n = in.nextInt(); k = in.nextInt(); FourVerse[] a = new FourVerse[n]; for (int i = 0; i < n; i++) { a[i] = new FourVerse(); } byte[] mask = new byte[4]; for (int i = 0; i < 4; i++) { mask[i] = 1; } for (int i = 0; i < n; i++) { byte cur[] = a[i].getType(); for (int j = 0; j < 4; j++) { if (cur[j] + mask[j] == 2) { mask[j] = 1; } else { mask[j] = 0; } } } for (int i = 3; i >= 0; i--) { if (mask[i] == 1) { out.println(type[i]); return; } } 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> typedef struct node { char str[10001]; } node; node poem[4]; long n, k; int q[3]; int find_v(char *str, int t) { int i, tmp; tmp = strlen(str); for (i = tmp - 1; i >= 0; i--) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') t--; if (!t) return i; } return i; } int check(int classify) { int v0, v1, v2, v3, t1, t2; if (classify == 0) { v0 = find_v(poem[0].str, k); if (v0 < 0) return 0; v1 = find_v(poem[1].str, k); if (v1 < 0) return 0; v2 = find_v(poem[2].str, k); if (v2 < 0) return 0; v3 = find_v(poem[3].str, k); if (v3 < 0) return 0; t1 = strlen(poem[0].str + v0); t2 = strlen(poem[2].str + v2); if (memcmp(poem[0].str + v0, poem[1].str + v1, t1) != 0) return 0; if (memcmp(poem[2].str + v2, poem[3].str + v3, t2) != 0) return 0; return 1; } if (classify == 1) { v0 = find_v(poem[0].str, k); if (v0 < 0) return 0; v1 = find_v(poem[1].str, k); if (v1 < 0) return 0; v2 = find_v(poem[2].str, k); if (v2 < 0) return 0; v3 = find_v(poem[3].str, k); if (memcmp(poem[0].str + v0, poem[2].str + v2, strlen(poem[0].str + v0)) != 0) return 0; if (memcmp(poem[1].str + v1, poem[3].str + v3, strlen(poem[1].str + v1)) != 0) return 0; return 1; } if (classify == 2) { v0 = find_v(poem[0].str, k); if (v0 < 0) return 0; v1 = find_v(poem[1].str, k); if (v1 < 0) return 0; v2 = find_v(poem[2].str, k); if (v2 < 0) return 0; v3 = find_v(poem[3].str, k); if (memcmp(poem[0].str + v0, poem[3].str + v3, strlen(poem[0].str + v0)) != 0) return 0; if (memcmp(poem[1].str + v1, poem[2].str + v2, strlen(poem[1].str + v1)) != 0) return 0; return 1; } return 0; } int main() { int i, j, s, flag = 0; scanf("%ld%ld", &n, &k); q[0] = q[1] = q[2] = 1; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) scanf("%s", poem[j].str); for (s = 0; s < 3; s++) if (q[s]) if (!check(s)) q[s] = 0; if (q[0] == 0 && q[1] == 0 && q[2] == 0) break; } if (q[0] && !q[1] && !q[2] && !flag) { printf("aabb\n"); flag = 1; } if (!q[0] && q[1] && !q[2] && !flag) { printf("abab\n"); flag = 1; } if (!q[0] && !q[1] && q[2] && !flag) { printf("abba\n"); flag = 1; } if (!q[0] && !q[1] && !q[2] && !flag) { printf("NO\n"); flag = 1; } if (!flag) printf("aaaa\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; inline double sqr(double x) { return x * x; } #pragma comment(linker, "/STACK:16777216") int k; string lst(string s, int n) { int i = s.size(), ctr = 0; string r, v = "aeiou"; while (i--) { r = s[i] + r; if (v.find(s[i]) != -1) ++ctr; if (ctr == k) return r; } return "" + char('0' + n); } int main() { char _s[10001]; string s[4]; int v = 7, n; cin >> n >> k; gets(_s); while (n-- && v) { for (int i = int(0); i < int(4); ++i) { gets(_s); s[i] = lst(_s, i); } if (s[0] != s[1] || s[2] != s[3]) v &= ~1; if (s[0] != s[2] || s[1] != s[3]) v &= ~2; if (s[0] != s[3] || s[1] != s[2]) v &= ~4; } if (v == 7) cout << "aaaa"; else if (v == 1) cout << "aabb"; else if (v == 2) cout << "abab"; else if (v == 4) cout << "abba"; else if (v == 0) cout << "NO"; else return 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
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.*; public class Main { public static boolean vowel(char ch) { return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } public static String doIt(String s, int k) { int c = 0; for (int i = s.length() - 1; i >= 0; --i) if (vowel(s.charAt(i))) { if (++c == k) return s.substring(i); } return ""; } public static boolean rhyme(String s1, String s2, int k) { int c1 = 0, c2 = 0; for (int i = 0; i < s1.length(); ++i) if (vowel(s1.charAt(i))) ++c1; for (int i = 0; i < s2.length(); ++i) if (vowel(s2.charAt(i))) ++c2; if (c1 < k || c2 < k) return false; return doIt(s1, k).equals(doIt(s2, k)); } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int quatrains = cin.nextInt(); int k = cin.nextInt(); boolean[] vis = new boolean[4]; Arrays.fill(vis, false); for (int i = 0; i < quatrains; ++i) { String s1 = cin.next(); String s2 = cin.next(); String s3 = cin.next(); String s4 = cin.next(); if (rhyme(s1, s2, k) && rhyme(s2, s3, k) && rhyme(s3, s4, k)) vis[0] = true; else if (rhyme(s1, s2, k) && rhyme(s3, s4, k)) vis[1] = true; else if (rhyme(s1, s3, k) && rhyme(s2, s4, k)) vis[2] = true; else if (rhyme(s1, s4, k) && rhyme(s2, s3, k)) vis[3] = true; else { System.out.println("NO"); System.exit(0); } } int cnt = 0; for (boolean ok : vis) if (ok) ++cnt; if (cnt >= 3 || cnt == 0) System.out.println("NO"); else if (cnt == 2) { if (vis[0] && vis[1]) System.out.println("aabb"); else if (vis[0] && vis[2]) System.out.println("abab"); else if (vis[0] && vis[3]) System.out.println("abba"); else System.out.println("NO"); } else if (cnt == 1) { if (vis[0]) System.out.println("aaaa"); else if (vis[1]) System.out.println("aabb"); else if (vis[2]) System.out.println("abab"); else if (vis[3]) 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> using namespace std; int main() { int n, ct, pos, k; string s1, s2, s3, s4, resp = "aaaa"; bool enc = 1; scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { cin >> s1 >> s2 >> s3 >> s4; ct = 0, pos; for (int i = s1.length() - 1; i >= 0; i--) { if (s1[i] == 'a' || s1[i] == 'e' || s1[i] == 'i' || s1[i] == 'o' || s1[i] == 'u') ct++; if (ct == k) { pos = i; break; } } if (ct < k) { cout << "NO" << endl; enc = 0; break; } string sub1 = s1.substr(pos); ct = 0, pos; for (int i = s2.length() - 1; i >= 0; i--) { if (s2[i] == 'a' || s2[i] == 'e' || s2[i] == 'i' || s2[i] == 'o' || s2[i] == 'u') ct++; if (ct == k) { pos = i; break; } } if (ct < k) { cout << "NO" << endl; enc = 0; break; } string sub2 = s2.substr(pos); ct = 0, pos; for (int i = s3.length() - 1; i >= 0; i--) { if (s3[i] == 'a' || s3[i] == 'e' || s3[i] == 'i' || s3[i] == 'o' || s3[i] == 'u') ct++; if (ct == k) { pos = i; break; } } if (ct < k) { cout << "NO" << endl; enc = 0; break; } string sub3 = s3.substr(pos); ct = 0, pos; for (int i = s4.length() - 1; i >= 0; i--) { if (s4[i] == 'a' || s4[i] == 'e' || s4[i] == 'i' || s4[i] == 'o' || s4[i] == 'u') ct++; if (ct == k) { pos = i; break; } } if (ct < k) { cout << "NO" << endl; enc = 0; break; } string sub4 = s4.substr(pos); string tipo = "nada"; if (sub1 == sub2 && sub2 == sub3 && sub3 == sub4) { tipo = "aaaa"; } else { if (sub1 == sub2 && sub3 == sub4) { tipo = "aabb"; } else { if (sub1 == sub3 && sub2 == sub4) { tipo = "abab"; } else { if (sub1 == sub4 && sub2 == sub3) { tipo = "abba"; } } } } if (tipo == "nada") { cout << "NO" << endl; enc = 0; break; } if (tipo == "aaaa") continue; if (resp == "aaaa") { resp = tipo; continue; } if (tipo != resp) { cout << "NO" << endl; enc = 0; break; } } if (enc) cout << resp << 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, ans_res = -2; bool glas(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return 1; else return 0; } int main() { scanf("%d%d\n", &n, &k); for (int i = 0; i < n; i++) { int res = 0; vector<string> list; for (int j = 0; j < 4; j++) { string str; getline(cin, str); int h = str.length() - 1; int cnt = 0; while (1) { if (h < 0 || cnt == k) break; if (glas(str[h])) cnt++; h--; } if (cnt < k) { res = -1; break; } list.push_back(str.substr(h + 1, str.length() - h - 1)); } if (res == -1) { puts("NO"); return 0; } if (list[0] == list[1] && list[1] == list[2] && list[2] == list[3]) res = 4; else if (list[0] == list[1] && list[2] == list[3]) res = 1; else if (list[0] == list[2] && list[1] == list[3]) res = 2; else if (list[0] == list[3] && list[1] == list[2]) res = 3; else res = -1; if (ans_res == -2 || ans_res == res || ans_res == 4) ans_res = res; else if (res != 4) ans_res = -1; } if (ans_res == -1) puts("NO"); else if (ans_res == 1) puts("aabb"); else if (ans_res == 2) puts("abab"); else if (ans_res == 3) puts("abba"); else if (ans_res == 4) puts("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 int M = 10000 + 10; int main() { int n, k; cin >> n >> k; set<int> res; bool flag = true; for (int i = 0; i < n; i++) { string s[4]; for (int j = 0; j < 4; j++) { cin >> s[j]; } string t[4]; for (int j = 0; j < 4; j++) { int len = s[j].length(); string tmp = ""; int num_vowel = 0; int index = -1; for (int ii = len - 1; ii >= 0; ii--) { if (s[j][ii] == 'a' || s[j][ii] == 'e' || s[j][ii] == 'i' || s[j][ii] == 'o' || s[j][ii] == 'u') { num_vowel++; if (num_vowel == k) { index = ii; } } } if (num_vowel >= k) { for (int ii = index; ii < len; ii++) { tmp = tmp + s[j][ii]; } } t[j] = tmp; } if (t[0] != "" && t[1] != "" && t[2] != "" && t[3] != "") { if (t[0] == t[1] && t[1] == t[2] && t[2] == t[3]) { res.insert(4); } else { if (t[0] == t[1] && t[2] == t[3]) { res.insert(1); } else { if (t[0] == t[2] && t[1] == t[3]) { res.insert(2); } else { if (t[0] == t[3] && t[1] == t[2]) { res.insert(3); } else { flag = false; } } } } } else { flag = false; } } if (res.size() > 2 || (res.size() == 2 && res.find(4) == res.end()) || res.size() == 0 || flag == false) { cout << "NO" << endl; } else { set<int>::iterator it = res.begin(); int t = (*it); if (t == 1) { cout << "aabb" << endl; } if (t == 2) { cout << "abab" << endl; } if (t == 3) { cout << "abba" << endl; } if (t == 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
import java.util.*; import java.io.*; public class C99C{ static BufferedReader br; public static void main(String args[])throws Exception{ br=new BufferedReader(new InputStreamReader(System.in)); int nums[]=toIntArray(); int n=nums[0]; int k=nums[1]; char vow[]={'a','e','i','o','u'}; ArrayList<Character> ar=new ArrayList<Character>(); for(int i=0;i<5;i++){ ar.add(vow[i]); } String st[]=new String[4]; int type=4; for(int i=0;i<n;i++){ String s1=toStr(); String s2=toStr(); String s3=toStr(); String s4=toStr(); int count=0; int ind1=0; int ind2=0; int ind3=0; int ind4=0; for(int j=s1.length()-1;j>=0;j--){ if(ar.contains(s1.charAt(j))) count++; if(count==k){ ind1=j; break; } } if(count<k){ prln("NO"); return; } count=0; for(int j=s2.length()-1;j>=0;j--){ if(ar.contains(s2.charAt(j))) count++; if(count==k){ ind2=j; break; } } if(count<k){ prln("NO"); return; } count=0; for(int j=s3.length()-1;j>=0;j--){ if(ar.contains(s3.charAt(j))) count++; if(count==k){ ind3=j; break; } } if(count<k){ prln("NO"); return; } count=0; for(int j=s4.length()-1;j>=0;j--){ if(ar.contains(s4.charAt(j))) count++; if(count==k){ ind4=j; break; } } if(count<k){ prln("NO"); return; } //prln(ind1+" "+ind2+" "+ind3+" "+ind4); int curType=0; if(s1.substring(ind1).equals(s2.substring(ind2)) && s2.substring(ind2).equals(s3.substring(ind3)) && s3.substring(ind3).equals(s4.substring(ind4) )){ curType=4; } else if(s1.substring(ind1).equals(s2.substring(ind2)) && s3.substring(ind3).equals(s4.substring(ind4))){ curType=1; } else if(s1.substring(ind1).equals(s3.substring(ind3)) && s2.substring(ind2).equals(s4.substring(ind4))){ curType=2; } else if(s1.substring(ind1).equals(s4.substring(ind4)) && s2.substring(ind2).equals(s3.substring(ind3))){ curType=3; } if(curType==0){ prln("NO"); return; } if(curType<4){ if(type<4){ if(type!=curType){ prln("NO"); return; } } else type=curType; } } if(type==4){ prln("aaaa"); } if(type==1){ prln("aabb"); } if(type==2){ prln("abab"); } if(type==3){ prln("abba"); } } /****************************************************************/ public static int[] toIntArray()throws Exception{ String str[]=br.readLine().split(" "); int k[]=new int[str.length]; for(int i=0;i<str.length;i++){ k[i]=Integer.parseInt(str[i]); } return k; } public static int toInt()throws Exception{ return Integer.parseInt(br.readLine()); } public static long[] toLongArray()throws Exception{ String str[]=br.readLine().split(" "); long k[]=new long[str.length]; for(int i=0;i<str.length;i++){ k[i]=Long.parseLong(str[i]); } return k; } public static long toLong()throws Exception{ return Long.parseLong(br.readLine()); } public static double[] toDoubleArray()throws Exception{ String str[]=br.readLine().split(" "); double k[]=new double[str.length]; for(int i=0;i<str.length;i++){ k[i]=Double.parseDouble(str[i]); } return k; } public static double toDouble()throws Exception{ return Double.parseDouble(br.readLine()); } public static String toStr()throws Exception{ return br.readLine(); } public static String[] toStrArray()throws Exception{ String str[]=br.readLine().split(" "); return str; } public static void pr(Object st){ System.out.print(st); } public static void prln(Object st){ System.out.println(st); } /****************************************************************/ }
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; string a[10000]; string sep(string s, int k) { string al = "aeiou"; int kol = 0; for (int i = 0; i < s.size(); i++) if (al.find(s[i]) < al.size() && al.find(s[i]) >= 0) kol++; if (kol < k) { cout << "NO"; exit(0); } kol = 0; string res; int j = s.size() - 1; while (kol < k) { res += s[j]; int p = al.find(s[j]); if (al.find(s[j]) < al.size() && al.find(s[j]) >= 0) kol++; j--; } return res; } int n, k; void solve(string t) { for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) for (int k = 0; k < 4; k++) if ((t[j] == t[k]) && !(a[i * 4 + j] == a[i * 4 + k])) return; cout << t; exit(0); } int main() { cin >> n >> k; for (int i = 0; i < 4 * n; i++) { string s; cin >> s; s = sep(s, k); a[i] = s; } solve("aaaa"); solve("aabb"); solve("abab"); solve("abba"); cout << "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 is_vowel(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { return true; } return false; } bool is_rhyme(string s1, string s2, int k) { int i, p1 = 0, p2 = 0, l1, l2; for (i = s1.size() - 1, p1 = 0; i >= 0 && p1 < k; i--) { if (is_vowel(s1[i])) { p1++; } } l1 = i + 1; if (p1 != k) { return false; } for (i = s2.size() - 1, p2 = 0; i >= 0 && p2 < k; i--) { if (is_vowel(s2[i])) { p2++; } } if (p2 != k) { return false; } l2 = i + 1; if (s1.substr(l1, s1.size() - l1 + 1) == s2.substr(l2, s2.size() - l2 + 1)) { return true; } return false; } int main() { long long int i, j, k, m, n, t, x, y, z; string rhyme[3000]; string str[3000][5]; char ch; string s1, s2, s3; vector<int> v; cin >> n >> k; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { cin >> str[i][j]; } } for (i = 0; i < n; i++) { bool fir = is_rhyme(str[i][0], str[i][1], k) && is_rhyme(str[i][2], str[i][3], k); bool sec = is_rhyme(str[i][0], str[i][2], k) && is_rhyme(str[i][1], str[i][3], k); bool thr = is_rhyme(str[i][0], str[i][3], k) && is_rhyme(str[i][1], str[i][2], k); if (fir && sec && thr) { rhyme[i] = "4"; } else if (fir && sec) { rhyme[i] = "12"; } else if (fir && thr) { rhyme[i] = "13"; } else if (thr && sec) { rhyme[i] = "23"; } else if (fir) { rhyme[i] = "1"; } else if (sec) { rhyme[i] = "2"; } else if (thr) { rhyme[i] = "3"; } else { rhyme[i] = "0"; } } int ct = 1; char ans = '0'; for (char k = '4'; k >= '1'; k--) { ct = 1; for (i = 0; i < n; i++) { int tmp = 1; for (j = 0; j < rhyme[i].size(); j++) { if (rhyme[i][j] == k || rhyme[i] == "4") { tmp = 0; } } if (tmp == 1) { ct = 0; break; } } if (ct == 1) { ans = k; break; } } if (ans == '4') { cout << "aaaa"; } else if (ans == '1') { cout << "aabb"; } else if (ans == '2') { cout << "abab"; } else if (ans == '3') { cout << "abba"; } 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
#include <bits/stdc++.h> using namespace std; int n, k; string ans = ""; vector<string> v; string func(string s) { int cnt = 0; for (int i = (int)s.size() - 1; i >= 0; i--) if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') { ++cnt; if (cnt == k) return s.substr(i); } return "ERROR"; } void die() { cout << "NO" << endl; exit(0); } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { v.clear(); for (int j = 0; j < 4; j++) { string s; cin >> s; string x = func(s); if (x == "ERROR") die(); v.push_back(x); } if (v[0] == v[1] && v[0] == v[2] && v[0] == v[3] && v[1] == v[2] && v[1] == v[3] && v[2] == v[3]) { if (ans == "") ans = "aaaa"; } else if (v[0] == v[1] && v[2] == v[3]) { if (ans != "" && ans != "aaaa" && ans != "aabb") die(); ans = "aabb"; } else if (v[0] == v[2] && v[1] == v[3]) { if (ans != "" && ans != "aaaa" && ans != "abab") die(); ans = "abab"; } else if (v[0] == v[3] && v[1] == v[2]) { if (ans != "" && ans != "aaaa" && ans != "abba") die(); ans = "abba"; } else die(); } cout << ans << 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
n, k = map(int, input().split()) vowels = ['a', 'e', 'i', 'o', 'u'] def find_nth(tab, S, n): amount, pos = (1 if tab[0] in S else 0), 0 while pos < len(tab) and amount < n: pos += 1 if tab[pos] in S: amount += 1 return pos def rhyme_type(lines, k): suffixes = [] for line in lines: amount = 0 for i in line: if i in vowels: amount += 1 if amount < k: return 'TRASH' rev = list(reversed(list(line))) ind_from_front = find_nth(rev, vowels, k) suffixes.append(line[-ind_from_front - 1:]) if all([suffixes[0] == x for x in suffixes]): return 'aaaa' if suffixes[0] == suffixes[1] and suffixes[2] == suffixes[3]: return 'aabb' if suffixes[0] == suffixes[2] and suffixes[1] == suffixes[3]: return 'abab' if suffixes[0] == suffixes[3] and suffixes[1] == suffixes[2]: return 'abba' return 'TRASH' all_rhymes = set() for _ in range(n): lines = [input(), input(), input(), input()] all_rhymes.add(rhyme_type(lines, k)) if 'TRASH' not in all_rhymes and len(all_rhymes) == 2 and 'aaaa' in all_rhymes: all_rhymes.remove('aaaa') print(list(all_rhymes)[0]) elif len(all_rhymes) > 1 or 'TRASH' in all_rhymes: print('NO') else: print(list(all_rhymes)[0])
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.io.BufferedReader; import java.io.InputStreamReader; import java.util.Hashtable; import java.util.StringTokenizer; public class C139 { public static void main(String[] args) throws Exception { System.out.println(res()); } public static String res() throws Exception { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(r.readLine()); int n = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st .nextToken()); String prev = null; for (int x = 0; x < n; x++) { Hashtable<String, Character> ht = new Hashtable<String, Character>(); char[] ar = new char[4]; char p = 'a'; for (int i = 0; i < 4; i++) { String line = r.readLine(); int dex = line.length() - 1; int count = 0; while (count != k) { if (dex == -1) { return "NO"; } if (line.substring(dex, dex + 1).matches("[aeiou]")) { count++; } dex--; } String suffix = line.substring(dex + 1); if (ht.containsKey(suffix)) { ar[i] = ht.get(suffix); } else { ht.put(suffix, p++); ar[i] = ht.get(suffix); } } String pattern = new String(ar); if (pattern.equals("aaaa")) continue; else if (prev == null) { prev = pattern; } else if (!prev.equals(pattern)) { return "NO"; } } if (prev == null) { return "aaaa"; } if (prev.equals("aabb") || prev.equals("abab") || prev.equals("abba")) { return prev; } 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; int n, k; int main() { int i, j; cin >> n >> k; bool ac = true; string ans; for (i = 0; i < n; i++) { string ss, s[4], str; str = "a "; for (j = 0; j < 4; j++) { cin >> ss; s[j] = ""; int t = 0; for (int h = ss.length() - 1; h >= 0; h--) { if (ss[h] == 'a' || ss[h] == 'i' || ss[h] == 'o' || ss[h] == 'u' || ss[h] == 'e') t++; s[j] += ss[h]; if (t >= k) break; } if (t < k) { ac = false; } } int c = 1; for (j = 1; j < 4; j++) { int x; for (x = 0; x < j; x++) { if (s[x] == s[j]) { str[j] = str[x]; break; } } if (x >= j) { str[j] = 'a' + c; c++; } } if (i == 0 || ans == "aaaa") { ans = str; } if (ans != str && str != "aaaa") ac = false; } if (!ac) { cout << "NO" << endl; } else { if (ans == "aaaa" || ans == "aabb" || ans == "abba" || ans == "abab") cout << ans << 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 main() { int n, k; cin >> n >> k; vector<string> s; for (int i = 0; i < 4 * n; i++) { string p; cin >> p; string temp; int count = 0; for (int j = 0; j < p.size(); j++) { if (p[j] == 'a' || p[j] == 'e' || p[j] == 'i' || p[j] == 'o' || p[j] == 'u') count++; } if (count < k) { cout << "NO"; exit(0); } count = 0; for (int j = p.size() - 1; j >= 0; j--) { temp.push_back(p[j]); if (p[j] == 'a' || p[j] == 'e' || p[j] == 'i' || p[j] == 'o' || p[j] == 'u') count++; if (count == k) break; } s.push_back(temp); } string a[] = {"aaaa", "aabb", "abab", "abba"}; vector<vector<int> > l; for (int i = 0; i < s.size(); i = i + 4) { vector<int> temp; if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == s[i + 3]) temp.push_back(0); if (s[i] == s[i + 1] && s[i + 2] == s[i + 3]) temp.push_back(1); if (s[i] == s[i + 2] && s[i + 1] == s[i + 3]) temp.push_back(2); if (s[i] == s[i + 3] && s[i + 1] == s[i + 2]) temp.push_back(3); if (temp.size() == 0) { cout << "NO"; exit(0); } l.push_back(temp); } for (int i = 0; i < l.size(); i++) { for (int j = 0; j < l[i].size(); j++) { int flag = 0; for (int k = 0; k < l.size(); k++) { if (k != i) { for (int m = 0; m < l[k].size(); m++) if (l[k][m] == l[i][j]) flag++; } } if (flag == l.size() - 1) { cout << a[l[i][j]]; exit(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; int main() { int n, k; int i, j, w, x, y; bool rhyme[4][4], scheme[5000][4]; string str[4]; map<int, string> name; name[0] = "aaaa"; name[1] = "aabb"; name[2] = "abab"; name[3] = "abba"; cin >> n >> k; for (i = 0; i < (n); i++) { for (j = 0; j < (4); j++) { cin >> str[j]; } memset(rhyme, false, sizeof(rhyme)); for (w = 0; w < (4); w++) { int ln = str[w].size(); int cnt = 0; string suff = ""; for (x = (ln - 1); x >= 0; x--) { if (str[w][x] == 'a' || str[w][x] == 'e' || str[w][x] == 'i' || str[w][x] == 'o' || str[w][x] == 'u') { cnt++; } if (cnt == k) { suff = str[w].substr(x, ln - x + 1); break; } } if (cnt < k) { cout << "NO" << endl; return 0; } for (x = (w + 1); x < (4); x++) { int sz = str[x].size(); cnt = 0; string ss = ""; for (y = (sz - 1); y >= 0; y--) { if (str[x][y] == 'a' || str[x][y] == 'e' || str[x][y] == 'i' || str[x][y] == 'o' || str[x][y] == 'u') { cnt++; } if (cnt == k) { ss = str[x].substr(y, sz - y + 1); if (ss == suff) { rhyme[w][x] = rhyme[x][w] = true; } break; } } } } if (rhyme[0][1] == true && rhyme[1][2] == true && rhyme[2][3] == true) { scheme[i][0] = true; } if (rhyme[0][1] == true && rhyme[2][3] == true) { scheme[i][1] = true; } if (rhyme[0][2] == true && rhyme[1][3] == true) { scheme[i][2] = true; } if (rhyme[0][3] == true && rhyme[1][2] == true) { scheme[i][3] = true; } } for (i = 0; i < (4); i++) { for (j = 0; j < (n); j++) { if (scheme[j][i] == false) { break; } } if (j == n) { cout << name[i] << 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
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.StreamTokenizer; import java.io.Writer; import java.util.Arrays; public class s99_c { public static void main(String[] args) throws IOException { new s99_c().run(); } int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } String nextString() throws IOException { in.nextToken(); return (String) in.sval; } StreamTokenizer in; Writer writer; Reader reader; char[] arr = new char[] {'a', 'e', 'i', 'o', 'u'}; String[] arr2 = new String[] { "aaaa", "aabb", "abab", "abba" }; void run() throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; reader = oj ? new InputStreamReader(System.in, "ISO-8859-1") : new FileReader("input/is99_c.txt"); writer = new OutputStreamWriter(System.out, "ISO-8859-1"); in = new StreamTokenizer(new BufferedReader(reader)); PrintWriter out = new PrintWriter(writer); int n = nextInt(); int k = nextInt(); String[][] arr = new String[n][4]; int[] type = new int[n]; Arrays.fill(type, -1); for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { arr[i][j] = nextString(); } } for (int i = 0; i < n; i++) { String[] suffixes = new String[4]; for (int j = 0; j < 4; j++) { suffixes[j] = getStr(k, arr[i][j]); if (suffixes[j] == null) { out.println("NO"); out.flush(); out.close(); return; } } if (suffixes[0].equals(suffixes[1]) && suffixes[2].equals(suffixes[3])) { type[i] = 1; } if (suffixes[0].equals(suffixes[2]) && suffixes[1].equals(suffixes[3])) { type[i] = 2; } if (suffixes[0].equals(suffixes[3]) && suffixes[1].equals(suffixes[2])) { type[i] = 3; } if (suffixes[0].equals(suffixes[1]) && suffixes[1].equals(suffixes[2]) && suffixes[2].equals(suffixes[3])) { type[i] = 0; } if (type[i] == -1) { out.println("NO"); out.flush(); out.close(); return; } } int ethalon = 0; for (int i = 0; i < n; i++) { if (type[i] == 0) continue; if (ethalon == 0) { ethalon = type[i]; continue; } if (ethalon != type[i]) { out.println("NO"); out.flush(); out.close(); return; } } out.print(arr2[ethalon]); out.flush(); out.close(); } private String getStr(int k, String str) { int length = str.length(); for (int i = length - 1; i >= 0; i--) { if (Arrays.binarySearch(arr, str.charAt(i)) > -1) { k--; } if (k == 0) { return str.substring(i); // StringBuilder b = new StringBuilder(); // for (int j = i; j < length; j++) // b.append(str.charAt(j)); // return b.toString(); } } return null; } }
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.*; import java.lang.Math.*; public class poems { public static void main(String args[])throws Exception { Scanner in=new Scanner(System.in); //BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=in.nextInt(),k=in.nextInt(); int i,j,l; String str[]=new String[4]; int a[]=new int[n]; for(i=0;i<n;i++) { str[0]=in.next(); str[1]=in.next(); str[2]=in.next(); str[3]=in.next(); if(aaaa(str,k)==true) a[i]=0; else if(aabb(str,k)==true) a[i]=1; else if(abab(str,k)==true) a[i]=2; else if(abba(str,k)==true) a[i]=3; else { a[i]=-100; } } int val=0; for(i=0;i<n;i++) { if(a[i]!=0) { val=a[i];break;} } for(;i<n;i++) { if(a[i]!=val && a[i]!=0){ System.out.println("NO");break;} } if(i==n) { if(val==0) System.out.println("aaaa"); else if(val==1) System.out.println("aabb"); else if(val==2) System.out.println("abab"); else if(val==3) System.out.println("abba"); else System.out.println("NO"); } } public static boolean aaaa(String str[],int k)throws Exception { int i,j,l=0,len=0; for(i=0;i<4;i++) { l=str[i].length(); len=count(l,str[i],k); if(len==0)return false; else { str[i]=str[i].substring(l-len); } } if((str[0].equals(str[1]))&&(str[1].equals(str[2]))&&(str[1].equals(str[3]))) return true; else return false; } public static boolean abab(String str[],int k)throws Exception { int i,j,l=0,len=0; for(i=0;i<4;i++) { l=str[i].length(); len=count(l,str[i],k); if(len==0)return false; else { str[i]=str[i].substring(l-len); } } if((str[0].equals(str[2]))&&(str[1].equals(str[3]))) return true; else return false; } public static boolean abba(String str[],int k)throws Exception { int i,j,l=0,len=0; for(i=0;i<4;i++) { l=str[i].length(); len=count(l,str[i],k); if(len==0)return false; else { str[i]=str[i].substring(l-len); } } if((str[0].equals(str[3]))&&(str[1].equals(str[2]))) return true; else return false; } public static boolean aabb(String str[],int k)throws Exception { int i,j,l=0,len=0; for(i=0;i<4;i++) { l=str[i].length(); len=count(l,str[i],k); if(len==0)return false; else { str[i]=str[i].substring(l-len); } } if((str[0].equals(str[1]))&&(str[2].equals(str[3]))) return true; else return false; } public static int count(int l,String s,int k)throws Exception { int i,ctr=0,len=0; for(i=l-1;i>=0;i--) { len++; char ch=s.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') ctr++; if(ctr==k) break; } if(ctr<k)return 0; return(len); } }
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 char vow[5] = {'a', 'e', 'i', 'o', 'u'}; string S[4]; int C[4]; void doMagic() { if (S[0] == S[1] && S[1] == S[2] && S[2] == S[3] && S[3] == S[0]) { C[0]++; C[1]++; C[2]++; C[3]++; return; } if (S[0] == S[1] && S[2] == S[3]) { C[1]++; return; } if (S[0] == S[2] && S[1] == S[3]) { C[2]++; return; } if (S[0] == S[3] && S[1] == S[2]) { C[3]++; return; } } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { string tmp; cin >> tmp; int cnt = 0; for (int i = tmp.length() - 1; i >= 0 && cnt < k; i--) { for (int j = 0; j < 5; j++) if (tmp[i] == vow[j]) { cnt++; break; } S[j].push_back(tmp[i]); } if (cnt < k) { cout << "NO"; goto exit; } } doMagic(); } if (C[0] == n) { cout << "aaaa"; goto exit; } if (C[1] == n) { cout << "aabb"; goto exit; } if (C[2] == n) { cout << "abab"; goto exit; } if (C[3] == n) { cout << "abba"; goto exit; } cout << "NO"; exit: 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; vector<string> words(4 * n); string S; int aabb = 0, abab = 0, abba = 0, aaaa = 0, NO = 0; for (int i = (0); i < (4 * n); ++i) { cin >> S; int size = S.size(); int Kth = 0; string newS = ""; bool canRhyme = false; for (int j = size - 1; j >= 0; --j) { if (S[j] == 'a' || S[j] == 'e' || S[j] == 'i' || S[j] == 'u' || S[j] == 'o') { ++Kth; } newS = S[j] + newS; if (Kth == k) { canRhyme = true; break; } } if (canRhyme) words[i] = newS; else { stringstream out; out << i; words[i] = out.str(); } } for (int i = 0; i < n; ++i) { if (words[4 * i].compare(words[4 * i + 1]) == 0 && words[4 * i + 2].compare(words[4 * i + 3]) == 0) { if (words[4 * i].compare(words[4 * i + 2]) == 0) { ++aaaa; ++aabb; ++abab; ++abba; } else ++aabb; } else if (words[4 * i].compare(words[4 * i + 2]) == 0 && words[4 * i + 1].compare(words[4 * i + 3]) == 0) ++abab; else if (words[4 * i].compare(words[4 * i + 3]) == 0 && words[4 * i + 1].compare(words[4 * i + 2]) == 0) ++abba; else ++NO; } if (NO) cout << "NO" << endl; else { if (aaaa == n) cout << "aaaa" << endl; else if (aabb == n) cout << "aabb" << endl; else if (abab == n) cout << "abab" << endl; else if (abba == n) 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; vector<int> text; int n, k; long long p = 31; bool vowel(char ch) { return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } int type_of(string &s1, string &s2, string &s3, string &s4) { long long hash1 = 0, hash2 = 0, hash3 = 0, hash4 = 0; long long cur_p = 1; int dt = k; for (int i = int(s1.size()) - 1; i >= 0 && dt > 0; i--) { if (vowel(s1[i])) dt--; hash1 += (s1[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; cur_p = 1; dt = k; for (int i = int(s2.size()) - 1; i >= 0 && dt > 0; i--) { if (vowel(s2[i])) dt--; hash2 += (s2[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; cur_p = 1; dt = k; for (int i = int(s3.size()) - 1; i >= 0 && dt > 0; i--) { if (vowel(s3[i])) dt--; hash3 += (s3[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; cur_p = 1; dt = k; for (int i = int(s4.size()) - 1; i >= 0 && dt > 0; i--) { if (vowel(s4[i])) dt--; hash4 += (s4[i] - 'a' + 1) * cur_p; cur_p *= p; } if (dt > 0) return 0; if (hash1 == hash2 && hash2 == hash3 && hash3 == hash4 && hash4 == hash1) return 4; if (hash1 == hash2 && hash3 == hash4) return 3; if (hash1 == hash3 && hash2 == hash4) return 2; if (hash1 == hash4 && hash2 == hash3) return 1; return 0; } int main() { cin >> n >> k; string s1, s2, s3, s4; for (int i = 0; i < n; i++) { cin >> s1 >> s2 >> s3 >> s4; text.push_back(type_of(s1, s2, s3, s4)); if (text.back() == 0) { cout << "NO\n"; return 0; } } int frst = -1; for (int i = 0; i < text.size(); i++) { if (text[i] == 4) continue; if (frst == -1) { frst = text[i]; continue; } if (text[i] != frst) { cout << "NO\n"; return 0; } } if (frst == -1) cout << "aaaa\n"; if (frst == 3) cout << "aabb\n"; if (frst == 2) cout << "abab\n"; if (frst == 1) 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; vector<vector<string> > v; int mat[2509][4]; int vowel(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return 1; return 0; } int main() { int n, k; string s; int ans1 = 1, ans2 = 1, ans3 = 1, ans4 = 1; cin >> n >> k; int i; int x, y; for (x = 0; x < n; x++) { vector<string> z; for (y = 0; y < 4; y++) { cin >> s; int len = s.length(); int cnt = 0; for (i = len - 1; i >= 0; i--) { if (vowel(s[i])) cnt++; if (cnt == k) break; } if (cnt < k) { cout << "NO\n"; return 0; } z.push_back(s.substr(i, len - 1 - i + 1)); } if (z[0] == z[1] && z[2] == z[3]) mat[x][0] = 1; if (z[0] == z[2] && z[1] == z[3]) mat[x][1] = 1; if (z[0] == z[3] && z[1] == z[2]) mat[x][2] = 1; if (z[0] == z[1] && z[1] == z[2] && z[2] == z[3]) mat[x][3] = 1; ans1 = (ans1 & mat[x][0]); ans2 = (ans2 & mat[x][1]); ans3 = (ans3 & mat[x][2]); ans4 = (ans4 & mat[x][3]); } if (ans4 == 1) cout << "aaaa\n"; else if (ans1 == 1) cout << "aabb\n"; else if (ans2 == 1) cout << "abab\n"; else if (ans3 == 1) cout << "abba\n"; 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
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.StringTokenizer; public class C implements Runnable { private void Solution() throws IOException { int n = nextInt(), k = nextInt(); String[][] stih = new String[n][4]; int[] rif = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { String s = nextToken(); int cur = 0, ind = -1; for (int g = s.length() - 1; g >= 0; g--) { char c = s.charAt(g); if (c == 'e' || c == 'u' || c == 'i' || c == 'o' || c == 'a') cur++; if (cur == k) { ind = g; break; } } if (ind == -1) { System.out.println("NO"); return; } stih[i][j] = s.substring(ind); } } for (int i = 0; i < n; i++) { if (stih[i][0].equals(stih[i][1]) && stih[i][0].equals(stih[i][2]) && stih[i][0].equals(stih[i][3]) && stih[i][1].equals(stih[i][2]) && stih[i][1].equals(stih[i][3]) && stih[i][2].equals(stih[i][3])) rif[i] = 1; else if (stih[i][0].equals(stih[i][1]) && stih[i][2].equals(stih[i][3])) rif[i] = 2; else if (stih[i][0].equals(stih[i][2]) && stih[i][1].equals(stih[i][3])) rif[i] = 3; else if (stih[i][0].equals(stih[i][3]) && stih[i][1].equals(stih[i][2])) rif[i] = 4; else rif[i] = -1; } for (int i = 0; i < n; i++) { if (rif[i] == -1) { System.out.println("NO"); return; } } for (int i = 0; i < n; i++) { if (rif[i] != 1) break; if (i == n - 1) { System.out.println("aaaa"); return; } } HashSet<Integer> set = new HashSet<Integer>(); ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if (!set.contains(rif[i])) { set.add(rif[i]); list.add(rif[i]); } } if (list.size() >= 3 || list.size() == 2 && list.get(0) != 1 && list.get(1) != 1) System.out.println("NO"); else { int ans = 0; if (list.get(0) == 1) ans = list.get(1); else ans = list.get(0); if (ans == 2) System.out.println("aabb"); else if (ans == 3) System.out.println("abab"); else System.out.println("abba"); } } public static void main(String[] args) { new C().run(); } BufferedReader in; StringTokenizer tokenizer; PrintWriter out; public void run() { try { String name = ""; name = "system"; if (name.equals("system")) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); } else if (name.equals("")) { in = new BufferedReader(new FileReader(new File("input.txt"))); out = new PrintWriter(new File("output.txt")); } else { in = new BufferedReader(new FileReader(new File(name + ".in"))); out = new PrintWriter(new File(name + ".out")); } Solution(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(in.readLine()); return tokenizer.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } double nextDouble() throws NumberFormatException, 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
import sys def vfind(s, k): rs = s[::-1] cnt = 0 for i in xrange(len(rs)): if rs[i] in "aeiou": cnt += 1 if cnt == k: return rs[:i+1][::-1] else: return None n,k = map(int, raw_input().split()) ls = [] for i in xrange(4*n): ls.append(raw_input()) vls = [] for i in xrange(4*n): vls.append(vfind(ls[i], k)) if None in vls: print "NO" sys.exit(0) vsls = [] for i in xrange(4): j = i a = "" while j < 4*n: a += vls[j] j += 4 vsls.append(a) #print vsls if 1 <= len(set(vsls)) <= 2: if len(set(vsls)) == 1: print "aaaa" else: if vsls[0] == vsls[1] and vsls[2] == vsls[3]: print "aabb" elif vsls[0] == vsls[2] and vsls[1] == vsls[3]: print "abab" elif vsls[0] == vsls[3] and vsls[1] == vsls[2]: print "abba" else: print "NO" 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 n, k; string q[4]; bool can[4]; bool okey(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } bool ok(string a, string b) { if ((int)a.length() < k || (int)b.length() < k) return false; int aa = a.length(); int bb = b.length(); int an = -1, ans1 = 0; int bn = -1, ans2 = 0; for (int i = aa - 1; i >= 0; i--) { if (okey(a[i])) ans1++; if (ans1 == k) { an = i; break; } } if (an == -1) return false; for (int i = bb - 1; i >= 0; i--) { if (okey(b[i])) ans2++; if (ans2 == k) { bn = i; break; } } if (bn == -1) return false; if (a.substr(an, aa - an) == b.substr(bn, bb - bn)) return true; return false; } void go(string g[4]) { if (!(ok(g[0], g[1]) && ok(g[2], g[3]))) can[0] = false; if (!(ok(g[0], g[2]) && ok(g[1], g[3]))) can[1] = false; if (!(ok(g[0], g[3]) && ok(g[1], g[2]))) can[2] = false; for (int i = 0; i < 4; i++) for (int j = i + 1; j < 4; j++) if (!ok(g[i], g[j])) { can[3] = false; return; } } int main() { q[0] = "aabb"; q[1] = "abab"; q[2] = "abba"; q[3] = "aaaa"; for (int i = 0; i <= 3; i++) can[i] = true; scanf("%d%d\n", &n, &k); for (int i = 1; i <= n; i++) { string g[4]; char c = '+'; for (int j = 0; j < 4; j++) { g[j] = ""; c = '+'; while (scanf("%c", &c) == 1) if (c != '\n') g[j] += c; else break; } go(g); } for (int i = 3; i >= 0; i--) if (can[i]) { cout << q[i]; 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.PrintWriter; import java.util.Scanner; import java.util.TreeSet; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); boolean no = false; String type = ""; for (int i = 0; i < n; i++) { String[] quatrain = new String[4]; for (int j = 0; j < 4; j++) quatrain[j] = in.nextLine(); String currType = getType(quatrain, k); if (type.equals("NO") || !(type.equals("") || currType.equals(type) || type.equals("aaaa") || currType.equals("aaaa"))) no = true; else if (type.equals("") || !currType.equals("aaaa")) type = currType; } out.println(no ? "NO" : type); out.close(); } private static String getType(String[] quatrain, int k) { String[] ends = new String[4]; for (int i = 0; i < 4; i++) { int count = 0; int pos = -1; for (int j = quatrain[i].length() - 1; j >= 0; j--) { char c = quatrain[i].charAt(j); if (isVowel(c)) count++; if (count == k) { pos = j; break; } } if (pos == -1) return "NO"; ends[i] = quatrain[i].substring(pos); } TreeSet<String> set = new TreeSet<String>(); for (int i = 0; i < 4; i++) set.add(ends[i]); if (set.size() == 1) return "aaaa"; else if (set.size() == 2) if (ends[0].equals(ends[3]) && ends[1].equals(ends[2])) return "abba"; else if (ends[0].equals(ends[1]) && ends[2].equals(ends[3])) return "aabb"; else if (ends[0].equals(ends[2]) && ends[1].equals(ends[3])) return "abab"; return "NO"; } private static boolean isVowel(char c) { return "aeiou".contains(Character.valueOf(c).toString()); } }
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; map<long long, long long> mp; string s[2504][4]; bool f[4] = {0}; int main() { long long n, m; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) cin >> s[i][j]; map<string, int> mp; for (int i = 0; i < n; i++) { mp.clear(); for (int j = 0; j < 4; j++) { string t = ""; int x = 0; for (int k = s[i][j].size() - 1; k >= 0 && x < m; k--) { if (s[i][j][k] == 'a' || s[i][j][k] == 'e' || s[i][j][k] == 'i' || s[i][j][k] == 'o' || s[i][j][k] == 'u') x++; t += s[i][j][k]; } if (x < m) { cout << "NO" << endl; return 0; } if (mp.find(t) == mp.end()) mp.insert(pair<string, int>(t, 1 << j)); else mp[t] += (1 << j); } if (mp.size() == 1) f[0] == 1; else if (mp.size() > 2) { cout << "NO" << endl; return 0; } else { map<string, int>::iterator it1 = mp.begin(), it2 = mp.begin(); it2++; if (it1->second == 3 && it2->second == 12 || it1->second == 12 && it2->second == 3) f[1] = true; else if (it1->second == 5 && it2->second == 10 || it1->second == 10 && it2->second == 5) f[2] = true; else if (it1->second == 9 && it2->second == 6 || it1->second == 6 && it2->second == 9) f[3] = true; else { cout << "NO" << endl; return 0; } } } long long p = (long long)f[1] + (long long)f[2] + (long long)f[3]; if (p >= 2) cout << "NO" << endl; else if (p == 0) cout << "aaaa" << endl; else if (f[1]) cout << "aabb" << endl; else if (f[2]) cout << "abab" << endl; else if (f[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; int n, k, m; string s[100005]; int is_vol(char c) { string t = "aeiou"; for (int i = 0; i < t.size(); i++) { if (c == t[i]) return 1; } return 0; } int ok(vector<string> v) { if (v.size() == 0) return 1; set<string> v2; for (int i = 0; i < v.size(); i++) { string temp; int cnt = 0; int ok = 0; for (int j = 0; j < v[i].size(); j++) { if (is_vol(v[i][j])) { cnt++; } if (!ok) temp += v[i][j]; if (cnt == k) { ok = 1; } } if (!ok) return 0; v2.insert(temp); } if (v2.size() == 1) return 1; return 0; } int solve(string t) { vector<int> A, B; for (int i = 0; i < 4; i++) { if (t[i] == 'a') { A.push_back(i); } else { B.push_back(i); } } for (int i = 0; i < m; i += 4) { string c[4]; c[0] = s[i]; c[1] = s[i + 1]; c[2] = s[i + 2]; c[3] = s[i + 3]; vector<string> v1, v2; for (int j = 0; j < A.size(); j++) { v1.push_back(c[A[j]]); } for (int j = 0; j < B.size(); j++) { v2.push_back(c[B[j]]); } if (ok(v1) && ok(v2)) { } else return 0; } return 1; } int main() { cin >> n >> k; m = n * 4; for (int i = 0; i < m; i++) { cin >> s[i]; reverse(s[i].begin(), s[i].end()); } if (solve("aaaa")) { cout << "aaaa"; } else if (solve("aabb")) { cout << "aabb"; } else if (solve("abab")) { cout << "abab"; } else if (solve("abba")) { cout << "abba"; } else { cout << "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
import java.util.Scanner; public class C { static String[]a; static int k; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); k = sc.nextInt(); String[]ss = {"aabb", "abab", "abba", "aaaa"}; a = new String[5]; int ans = -1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= 4; j++) { a[j] = sc.next(); } int tip = -1; if (check(1,2) && check(2, 3) && check(3, 4)) { tip = 3; } else { if (check(1, 2) && check(3, 4)) { tip = 0; } else { if (check(1, 3) && check(2, 4)) { tip = 1; } else { if (check(1, 4) && check(2, 3)) { tip = 2; } else { System.out.println("NO"); return; } } } } if (tip==3 && ans!=-1) continue; if (ans==-1 || ans==3) ans = tip; else { if (ans != tip) { System.out.println("NO"); return; } } } if (ans==-1) { System.out.println("NO"); } else { System.out.println(ss[ans]); } } private static boolean check(int k1, int k2) { int ind1 = a[k1].length()-1; int ind2 = a[k2].length()-1; String voice = "aeoiu"; int cnt = 0; while (cnt < k && ind1 >= 0) { if (voice.indexOf(a[k1].charAt(ind1)) >= 0) { cnt++; } ind1--; } if (cnt!=k) return false; cnt = 0; while (cnt < k && ind2 >= 0) { if (voice.indexOf(a[k2].charAt(ind2)) >= 0) { cnt++; } ind2--; } if (cnt!=k) return false; ind1++; ind2++; if (a[k1].substring(ind1).equals(a[k2].substring(ind2))) { return true; } return false; } }
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; int n, k, m; string s[100005]; int is_vol(char c) { string t = "aeiou"; for (int i = 0; i < t.size(); i++) { if (c == t[i]) return 1; } return 0; } int ok(vector<string> v) { if (v.size() == 0) return 1; set<string> v2; for (int i = 0; i < v.size(); i++) { string temp; int cnt = 0; int ok = 0; for (int j = 0; j < v[i].size(); j++) { if (is_vol(v[i][j])) { cnt++; } if (!ok) temp += v[i][j]; if (cnt == k) { ok = 1; } } if (!ok) return 0; v2.insert(temp); } if (v2.size() == 1) return 1; return 0; } int solve(string t) { vector<int> A, B; for (int i = 0; i < 4; i++) { if (t[i] == 'a') { A.push_back(i); } else { B.push_back(i); } } for (int i = 0; i < m; i += 4) { string c[4]; c[0] = s[i]; c[1] = s[i + 1]; c[2] = s[i + 2]; c[3] = s[i + 3]; vector<string> v1, v2; for (int j = 0; j < A.size(); j++) { v1.push_back(c[A[j]]); } for (int j = 0; j < B.size(); j++) { v2.push_back(c[B[j]]); } if (ok(v1) && ok(v2)) { } else return 0; } return 1; } int main() { cin >> n >> k; m = n * 4; for (int i = 0; i < m; i++) { cin >> s[i]; reverse(s[i].begin(), s[i].end()); } if (solve("aaaa")) { cout << "aaaa"; } else if (solve("aabb")) { cout << "aabb"; } else if (solve("abab")) { cout << "abab"; } else if (solve("abba")) { cout << "abba"; } else { cout << "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; const int N = 2500 * 4 + 10; int n, k, d, sz; bool isV[2000], M = 0; string arr[N]; set<string> st[N]; string change(string z) { int c = 0; string s = ""; for (int i = z.size() - 1; i >= 0; i--) { s += z[i]; if (isV[z[i]]) M = 1, c++; if (c == k) break; } if (c != k) M = 0; reverse(s.begin(), s.end()); return s; } bool checkAABB() { vector<string> v(10); for (int i = 0; i < sz; i++) { for (int j = 0; j < 4; j++) { v[j] = arr[i * 4 + j]; } if (v[0] != v[1] || v[2] != v[3]) return 0; } return 1; } bool checkABAB() { vector<string> v(10); for (int i = 0; i < sz; i++) { for (int j = 0; j < 4; j++) { v[j] = arr[i * 4 + j]; } if (v[0] != v[2] || v[1] != v[3]) return 0; } return 1; } bool checkABBA() { vector<string> v(10); for (int i = 0; i < sz; i++) { for (int j = 0; j < 4; j++) { v[j] = arr[i * 4 + j]; } if (v[0] != v[3] || v[2] != v[1]) return 0; } return 1; } bool checkAAAA() { vector<string> v(10); for (int i = 0; i < sz; i++) { for (int j = 0; j < 4; j++) { v[j] = arr[i * 4 + j]; } if (v[0] != v[1] || v[2] != v[3] || v[1] != v[2]) return 0; } return 1; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); isV['a'] = isV['o'] = isV['i'] = isV['u'] = isV['e'] = 1; cin >> n >> k; sz = n; n *= 4; bool Valid = 1; for (int i = 0; i < n; i++) { cin >> arr[i]; M = 0; arr[i] = change(arr[i]); if (!M) Valid = 0; } if (!Valid) return cout << "NO" << endl, 0; if (checkAAAA()) cout << "aaaa" << endl; else if (checkABAB()) cout << "abab" << endl; else if (checkABBA()) cout << "abba" << endl; else if (checkAABB()) cout << "aabb" << 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
VOWELS = ['a', 'e', 'o', 'u', 'i'] def solve(): n, k = map(int,raw_input().split()) poem = [] for i in xrange(4*n): s = raw_input().strip() i = len(s) for j in xrange(k): i -= 1 while i >= 0 and s[i] not in VOWELS: i -= 1 if i < 0: return "NO" poem.append(s[i:]) #print poem schemes = (((1, 2), (3, 4)), ((1, 3), (2, 4)), ((1, 4), (2, 3))) mask = 7 for i in xrange(n): cmask = 0 a, b, c, d = poem[i * 4 : (i + 1) * 4] if a == b and c == d: cmask |= 1 if a == c and b == d: cmask |= 2 if a == d and b == c: cmask |= 4 mask &= cmask ansmap = {7: 'aaaa', 1: 'aabb', 2: 'abab', 4: 'abba', 0: 'NO'} assert mask in ansmap return ansmap[mask] print solve()
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, len, cur, r[2510], size; string s[5], sub[5]; bool ok; bool check(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'u' || ch == 'o') return true; return false; } int rifm(string s1, string s2, string s3, string s4) { if (s1 == s2 && s1 == s3 && s1 == s4) return 4; else if (s1 == s2 && s3 == s4) return 1; else if (s1 == s3 && s2 == s4) return 2; else if (s1 == s4 && s2 == s3) return 3; else return 0; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= 4; j++) cin >> s[j]; for (int j = 1; j <= 4; j++) { len = s[j].length() - 1; cur = 0; sub[j] = "A"; while (len != -1) { if (check(s[j][len])) cur++; if (cur == k) { int leng = s[j].length(); sub[j] = s[j].substr(len, leng - len + 1); break; } len--; } if (sub[j] == "A") ok = true; } r[size++] = rifm(sub[1], sub[2], sub[3], sub[4]); } if (ok) { cout << "NO"; return 0; } sort(r, r + size); if (r[0] == 0) { cout << "NO"; return 0; } int ans = r[0]; for (int i = 0; i < size; i++) { if (r[i] != r[0] && r[i] != 4) { cout << "NO"; return 0; } } if (r[0] == 1) cout << "aabb"; else if (r[0] == 2) cout << "abab"; else if (r[0] == 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 int INFint = 2147483647; const long long INF = 9223372036854775807ll; const long long MOD = 1000000007ll; const long double EPS = 1e-9; string s[4], r[4]; int main() { ios_base::sync_with_stdio(0); map<char, bool> gl; gl['a'] = 1, gl['e'] = 1, gl['i'] = 1, gl['o'] = 1, gl['u'] = 1; int n, k; cin >> n >> k; vector<int> t(n); for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) cin >> s[j]; for (int j = 0; j < 4; j++) { int cnt = 0; int pos = -1; for (int it = s[j].size() - 1; it > -1; it--) { if (cnt == k) break; if (gl[s[j][it]]) { cnt++; pos = it; } } if (cnt != k) { cout << "NO" << endl; return 0; } r[j] = s[j].substr(pos, s[j].size()); } if (r[0] == r[1] && r[2] == r[3] && r[2] == r[1]) t[i] = 1; else if (r[0] == r[1] && r[2] == r[3] && r[2] != r[1]) t[i] = 2; else if (r[0] == r[2] && r[1] == r[3] && r[2] != r[1]) t[i] = 3; else if (r[0] == r[3] && r[2] == r[1] && r[0] != r[1]) t[i] = 4; } vector<int> v; for (int i = 0; i < n; i++) { if (t[i] != 1) v.push_back(t[i]); } if (v.size() == 0) { cout << "aaaa"; return 0; } int tp = v[0]; for (int i = 1; i < v.size(); i++) { if (v[i] != tp) { cout << "NO"; return 0; } } if (tp == 2) { cout << "aabb"; return 0; } if (tp == 3) { cout << "abab"; return 0; } if (tp == 4) { cout << "abba"; return 0; } cout << "NO" << endl; fprintf(stderr, "\nTIME = %lf\n", 1.0 * clock() / CLOCKS_PER_SEC); 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 mer(vector<int> &v) { int last = 0; for (int i = 0; i < v.size(); i++) { if (v[i] != 0) { if (v[i] == -1) return -1; else if (last == 0) last = v[i]; else if (last != v[i]) return -1; } } return last; } int c(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ? 1 : 0); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> res; for (int i = 0; i < n; i++) { string s[4], t[4]; for (int j = 0; j < 4; j++) { cin >> s[j]; t[j] = "1"; int cnt = 0; for (int l = s[j].size() - 1; l >= 0; l--) { cnt += c(s[j][l]); if (cnt == k) { t[j] = s[j].substr(l); break; } } } if (t[0] == "1" || t[1] == "1" || t[2] == "1" || t[3] == "1") res.push_back(-1); else if (t[0] == t[1] && t[1] == t[2] && t[2] == t[3]) res.push_back(0); else if (t[0] == t[1] && t[2] == t[3]) res.push_back(1); else if (t[0] == t[2] && t[1] == t[3]) res.push_back(2); else if (t[0] == t[3] && t[1] == t[2]) res.push_back(3); else res.push_back(-1); } int resp = mer(res); if (resp == -1) cout << "NO"; else if (resp == 0) cout << "aaaa"; else if (resp == 1) cout << "aabb"; else if (resp == 2) cout << "abab"; else if (resp == 3) cout << "abba"; 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.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class C { private void solve() throws IOException { int blocks = nextInt(); int k = nextInt(); boolean ok = true; String res = "aaaa"; for (int i = 0; i < blocks; i++) { String[] a = new String[4]; for (int j = 0; j < 4; j++) { a[j] = split(nextToken(), k); if (a[j].equals("")) { ok = false; } } if (!ok) continue; String s = solve(a); res = pair(res, s); if (res.equals("")) ok = false; } if (!ok) { System.out.println("NO"); return; } System.out.println(res); } private String pair(String s1, String s2) { if (s1.equals("aaaa")) return s2; if (s2.equals("aaaa")) return s1; if (s1.equals(s2)) return s1; return ""; } private String solve(String[] a) { if (a[0].equals(a[1]) && a[1].equals(a[2]) && a[2].equals(a[3])) return "aaaa"; if (a[0].equals(a[1]) && a[2].equals(a[3])) return "aabb"; 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"; return ""; } private String split(String t, int k) { int n = 0; for (int i = t.length() - 1; i >= 0; i--) { if ("aieou".contains(t.charAt(i) + "")) n++; if (n == k) return t.substring(i); } return ""; } public static void main(String[] args) { new C().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.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
#include <bits/stdc++.h> int k; int vowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } int rhyme(char *s, char *t) { int n, m, i, j, cnt; n = strlen(s); m = strlen(t); for (i = n - 1, cnt = 0; i >= 0 && cnt < k; i--) if (vowel(s[i])) cnt++; if (cnt < k) return 0; for (j = m - 1, cnt = 0; j >= 0 && cnt < k; j--) if (vowel(t[j])) cnt++; if (cnt < k) return 0; i++, j++; return strcmp(s + i, t + j) == 0; } int aaaa[] = {1, 2, 3, 0}; int aabb[] = {1, 0, 3, 2}; int abab[] = {2, 3, 0, 1}; int abba[] = {3, 2, 1, 0}; char ans[][8] = {"aaaa", "aabb", "abab", "abba", "NO"}; int check(int *aa, char ss[][10000 + 1]) { int i; for (i = 0; i < 4; i++) if (!rhyme(ss[i], ss[aa[i]])) return 0; return 1; } int main() { static char ss[4][10000 + 1]; int n, i, j, idx; scanf("%d%d", &n, &k); idx = -1; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) scanf("%s", ss[j]); if (check(aaaa, ss)) { if (idx == -1) idx = 0; } else if (check(aabb, ss)) { if (idx != -1 && idx != 1 && idx != 0) { idx = 4; break; } idx = 1; } else if (check(abab, ss)) { if (idx != -1 && idx != 2 && idx != 0) { idx = 4; break; } idx = 2; } else if (check(abba, ss)) { if (idx != -1 && idx != 3 && idx != 0) { idx = 4; break; } idx = 3; } else { idx = 4; break; } } printf("%s\n", ans[idx]); return 0; }
CPP