message stringlengths 2 23.4k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 129 108k | cluster float64 6 6 | __index_level_0__ int64 258 216k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will write a simple code generator for a 2D programming language derived from [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck).
The code in this language is a rectangular grid of characters '.' and 'X'. The code is converted to a Brainfuck program as follows: the characters are read in the usual order (top to bottom, left to right), and each 'X' character is converted a Brainfuck instruction to be executed. The instruction is defined by the left, top and right neighbors of the 'X' character using the following conversion table:
<image>
You are given a string. Output a program in the described language which prints this string.
You can download the language interpreter used for judging here: <https://assets.codeforces.com/rounds/952/puzzling-interpreter.cpp> (use C++11 to compile the code). Note several implementation details:
* The first step of the language interpretation is conversion to a Brainfuck program, which is then executed.
* The code must be rectangular, with all lines of the same length. It can have at most 10,000 lines and 10,000 columns, and can have at most 500,000 'X' characters.
* The code has toroidal topology, i.e. the 'X' on the first line will have top neighbor in the last line.
* Brainfuck interpreter has 30000 memory cells which store integers from 0 to 255 with increment/decrement done modulo 256.
* Console input (, command) is allowed in Brainfuck code but has no effect when executed.
Input
The input consists of a single string of characters with ASCII codes between 33 ('!') and 122 ('z'), inclusive. The length of the string is between 1 and 10 characters, inclusive.
Output
Output a program in the described language which, when executed, will print the given message.
Example
Input
$$$
Output
.......X.......
......XXX......
.....XXXXX.....
....XXXXXXX....
...XXXXXXXXX...
..XXXXXXXXXXX..
.XXXXXXXXXXXXX.
...............
X.............X
X..............
X..............
X..............
Note
The example corresponds to the following Brainfuck program:
-
>+<
>+++<
>+++++<
>+++++++<
>+++++++++<
>+++++++++++<
< >
.
.
.
The triangular block decrements the first memory cell and sets the value of the second memory cell to 36 - the ASCII code of '$' character. The next line after the triangular block moves the memory pointer to the second memory cell, and the next three lines print the '$' character three times. | instruction | 0 | 14,746 | 6 | 29,492 |
Tags: constructive algorithms
Correct Solution:
```
n = input()
s = ""
t = ""
for c in n:
i = ord(c)
s += ".X." + ".X.." * i + ".X.X." + ".." * i + "X.."
t += "..." + "XXX." * i + "XX.X." + "X." * i + "XX."
print(s)
print(t)
print(len(s) * ".")
``` | output | 1 | 14,746 | 6 | 29,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will write a simple code generator for a 2D programming language derived from [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck).
The code in this language is a rectangular grid of characters '.' and 'X'. The code is converted to a Brainfuck program as follows: the characters are read in the usual order (top to bottom, left to right), and each 'X' character is converted a Brainfuck instruction to be executed. The instruction is defined by the left, top and right neighbors of the 'X' character using the following conversion table:
<image>
You are given a string. Output a program in the described language which prints this string.
You can download the language interpreter used for judging here: <https://assets.codeforces.com/rounds/952/puzzling-interpreter.cpp> (use C++11 to compile the code). Note several implementation details:
* The first step of the language interpretation is conversion to a Brainfuck program, which is then executed.
* The code must be rectangular, with all lines of the same length. It can have at most 10,000 lines and 10,000 columns, and can have at most 500,000 'X' characters.
* The code has toroidal topology, i.e. the 'X' on the first line will have top neighbor in the last line.
* Brainfuck interpreter has 30000 memory cells which store integers from 0 to 255 with increment/decrement done modulo 256.
* Console input (, command) is allowed in Brainfuck code but has no effect when executed.
Input
The input consists of a single string of characters with ASCII codes between 33 ('!') and 122 ('z'), inclusive. The length of the string is between 1 and 10 characters, inclusive.
Output
Output a program in the described language which, when executed, will print the given message.
Example
Input
$$$
Output
.......X.......
......XXX......
.....XXXXX.....
....XXXXXXX....
...XXXXXXXXX...
..XXXXXXXXXXX..
.XXXXXXXXXXXXX.
...............
X.............X
X..............
X..............
X..............
Note
The example corresponds to the following Brainfuck program:
-
>+<
>+++<
>+++++<
>+++++++<
>+++++++++<
>+++++++++++<
< >
.
.
.
The triangular block decrements the first memory cell and sets the value of the second memory cell to 36 - the ASCII code of '$' character. The next line after the triangular block moves the memory pointer to the second memory cell, and the next three lines print the '$' character three times. | instruction | 0 | 14,747 | 6 | 29,494 |
Tags: constructive algorithms
Correct Solution:
```
data = input()
lens = [(256 - ord(data[0]))]
for i in range(len(data) - 1):
lens.append((512 - (ord(data[i+1]) - ord(data[i]))) % 256)
line0 = "." * 512
line1 = ".X" + ("." * 510)
for i in range(len(data)):
if lens[i] == 0:
print(line1)
else:
print(line0)
print(".X" * lens[i] + ".." * (256 - lens[i]))
print(line1)
``` | output | 1 | 14,747 | 6 | 29,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,181 | 6 | 30,362 |
Tags: implementation, strings
Correct Solution:
```
x, y = input().split(' ')
x = int(x)
y = int(y)
s = ''
s1 = ''
s= (input().split(' '))
s1 = (input().split(' '))
q = int(input())
for i in range(q):
z = int(input())
print(s[(z-1)%x]+s1[(z-1)%y])
``` | output | 1 | 15,181 | 6 | 30,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,182 | 6 | 30,364 |
Tags: implementation, strings
Correct Solution:
```
n,m = [int(x) for x in input().split()]
s = input().strip().split()
t = input().strip().split()
q = int(input())
for _ in range(q):
y = int(input())
r1 = (y-1)%n
r2 = (y-1)%m
print(s[r1]+t[r2])
``` | output | 1 | 15,182 | 6 | 30,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,183 | 6 | 30,366 |
Tags: implementation, strings
Correct Solution:
```
from math import gcd
n, m = list(map(int, input().split()))
ar1 = list(input().split())
ar2 = list(input().split())
kek = n * m // gcd(n, m)
for _ in range(int(input())):
t = int(input())
t %= kek
print(ar1[(t - 1) % n] + ar2[(t - 1) % m])
``` | output | 1 | 15,183 | 6 | 30,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,184 | 6 | 30,368 |
Tags: implementation, strings
Correct Solution:
```
n,m=map(int,input().split())
a=list(map(str,input().split()))
b=list(map(str,input().split()))
for i in range(int(input())):
k=int(input())
x=k%len(a)
y=k%len(b)
print(a[x-1]+b[y-1])
``` | output | 1 | 15,184 | 6 | 30,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,185 | 6 | 30,370 |
Tags: implementation, strings
Correct Solution:
```
n,m=map(int,input().split())
s1=input().split()
s2=input().split()
l=[]
for i in range(n*m):
l.append(s1[i%n]+s2[i%m])
for i in range(int(input())):
k=int(input())
print (l[(k-1)%(n*m)])
``` | output | 1 | 15,185 | 6 | 30,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,186 | 6 | 30,372 |
Tags: implementation, strings
Correct Solution:
```
n,m=map(int,input().split())
pol=input().split()
kan=input().split()
a=[]
for i in range(1, n*m+1):
if(i%n==0):
a.append(pol[n-1])
else:
a.append(pol[i%n-1])
if(i%m==0):
a[-1]+=kan[m-1]
else:
a[-1]+=kan[i%m-1]
p=int(input())
for i in range(p):
t=int(input())
print(a[t%(n*m)-1])
``` | output | 1 | 15,186 | 6 | 30,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,187 | 6 | 30,374 |
Tags: implementation, strings
Correct Solution:
```
a = list(map(int,input().split()))
n = input().split()
s = input().split()
q = int(input())
for i in range(q):
x = int(input())
t1 = x%a[0]
t2 = x%a[1]
print(n[t1-1]+s[t2-1])
``` | output | 1 | 15,187 | 6 | 30,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of n strings s_1, s_2, s_3, β¦, s_{n} and m strings t_1, t_2, t_3, β¦, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces".
The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
<image>
You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
Input
The first line contains two integers n, m (1 β€ n, m β€ 20).
The next line contains n strings s_1, s_2, β¦, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
The next line contains m strings t_1, t_2, β¦, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10.
Among the given n + m strings may be duplicates (that is, they are not necessarily all different).
The next line contains a single integer q (1 β€ q β€ 2 020).
In the next q lines, an integer y (1 β€ y β€ 10^9) is given, denoting the year we want to know the name for.
Output
Print q lines. For each line, print the name of the year as per the rule described above.
Example
Input
10 12
sin im gye gap eul byeong jeong mu gi gyeong
yu sul hae ja chuk in myo jin sa o mi sin
14
1
2
3
4
10
11
12
13
73
2016
2017
2018
2019
2020
Output
sinyu
imsul
gyehae
gapja
gyeongo
sinmi
imsin
gyeyu
gyeyu
byeongsin
jeongyu
musul
gihae
gyeongja
Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. | instruction | 0 | 15,188 | 6 | 30,376 |
Tags: implementation, strings
Correct Solution:
```
from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
x=list(stdin.readline())
x.pop()
return x
def si(): return stdin.readline()
def sieve(x):
a=[True]*(x+1)
sq=floor(sqrt(x))
for i in range(3, sq+1, 2):
if a[i]:
for j in range(i*i, x+1, i):
a[j]=False
if x>1:
p=[2]
else:
p=[]
for i in range(3, x+1, 2):
if a[i]:
p.append(i)
return p
#vowel={'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'}
#pow=[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808]
############# CODE STARTS HERE #############
n, m=mi()
a=list(input().split())
b=list(input().split())
p=[a[-1]]+a[:-1]
q=[b[-1]]+b[:-1]
for _ in range(ii()):
x=ii()
print(p[x%n]+q[x%m])
``` | output | 1 | 15,188 | 6 | 30,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,197 | 6 | 30,394 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
for _ in range(int(input())):
s = input()
l = []
l.append(s[0])
f=0
m = 0
j=0
for i in range(1,len(s)):
if s[i] in l:
if l[j]==s[i]:
f=1
break
if j>0 and j<m and l[j-1]!=s[i] and l[j+1]!=s[i]:
f=1
break
if j==0 and l[j+1]!=s[i]:
f=1
break
if j==m and l[j-1]!=s[i]:
f=1
break
if l[j]==s[i]:
f=1
break
if j>0 and j<m and l[j-1]!=s[i] and l[j+1]!=s[i]:
f=1
break
if j>0 and l[j-1]==s[i]:
j-=1
continue
if j<m and l[j+1]==s[i]:
j+=1
continue
if j==0 and m==0:
j+=1
m+=1
l.append(s[i])
elif j==0 and m>0:
m+=1
l.insert(0,s[i])
elif j==m:
j+=1
m+=1
l.append(s[i])
if f==1:
print('NO')
else:
print('YES')
a = []
for k in range(26):
a.append(-1)
for k in l:
a[ord(k)-97]=1
for k in range(26):
if a[k]==1:
pass
else:
l.append(chr(k+97))
str1 = ""
for i in l:
str1+=i
print(str1)
``` | output | 1 | 15,197 | 6 | 30,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,198 | 6 | 30,396 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
t=int(input())
for tests in range(t):
s=input().strip()
if len(s)==1:
print("YES")
print("abcdefghijklmnopqrstuvwxyz")
continue
L=[set() for i in range(26)]
for i in range(1,len(s)):
x=ord(s[i])-97
y=ord(s[i-1])-97
L[x].add(y)
L[y].add(x)
LL=[len(l) for l in L]
C=Counter(LL)
if max(LL)>2 or C[1]!=2:
print("NO")
continue
NOW=LL.index(1)
USE=[0]*26
USE[NOW]=1
ANS=chr(NOW+97)
NOW=list(L[NOW])[0]
USE[NOW]=1
ANS+=chr(NOW+97)
while LL[NOW]==2:
for x in L[NOW]:
if USE[x]==0:
NOW=x
USE[x]=1
ANS+=chr(x+97)
#print(L)
#print(ANS)
for i in range(26):
if USE[i]==0:
ANS+=chr(i+97)
print("YES")
print(ANS)
``` | output | 1 | 15,198 | 6 | 30,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,199 | 6 | 30,398 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
for _ in range(int(input())):
ans=deque()
s=input()
ans.append(s[0])
a=0
flag="YES"
for i in range(1,len(s)):
if a==0:
if len(ans)>1:
if ans[a+1]==s[i]:
a+=1
else:
ans.appendleft(s[i])
else:
ans.append(s[i])
a+=1
elif a==len(ans)-1:
if ans[a-1]==s[i]:
a-=1
else:
ans.append(s[i])
a+=1
else:
if ans[a-1]==s[i]:
a-=1
elif ans[a+1]==s[i]:
a+=1
else:
flag="NO"
break
for i in range(97,123):
if chr(i) not in ans:
ans.append(chr(i))
if len(ans)!=26:
flag="NO"
print(flag)
if flag=="YES":
print("".join(ans))
``` | output | 1 | 15,199 | 6 | 30,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,200 | 6 | 30,400 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
for _ in range(int(input())):
s=input()
p=s[0]
c=""
q="abcdefghijklmnopqrstuvwxyz"
flag=0
for i in range(1,len(s)):
if s[i] in p:
k=p.find(s[i])
if(len(p)==1):
if(s[i]!=p[0]):
p+=s[i]
elif k==0 :
if p[1]!=s[i-1]:
flag=1
break
elif k==(len(p)-1):
if p[k-1]!=s[i-1]:
flag=1
break
elif p[k-1]!=s[i-1] and p[k+1]!=s[i-1]:
flag=1
break
else:
k=p.find(s[i-1])
if k==0:
c=p
p=s[i]
p+=c
elif k==(len(p)-1):
p+=s[i]
else:
flag=1
break
if flag==1:
print("NO")
else :
print("YES")
for i in q:
if i not in p:
p+=i
print(p)
``` | output | 1 | 15,200 | 6 | 30,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,201 | 6 | 30,402 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
t = int(input())
for i in range(t):
s = input()
f = True
st = set()
answer = ''
pos = 0
n = 0
for l in s:
if l not in st:
st.add(l)
n += 1
if pos == n - 1:
answer = answer + l
pos += 1
elif pos == 1:
answer = l + answer
else:
f = False
break
else:
if pos < n:
if answer[pos] == l:
pos += 1
continue
if pos > 1:
if answer[pos - 2] == l:
pos -= 1
continue
f = False
break
if f:
print('YES')
print(answer, end='')
for l in 'abcdefghijklmnopqrstuvwxyz':
if l not in st:
print(l, end='')
print()
else:
print('NO')
``` | output | 1 | 15,201 | 6 | 30,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,202 | 6 | 30,404 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
for _ in range(int(input())):
s = input()
finish = ""
x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
y = [0]*52
u = 26
d = len(s)
ok = True
s1 = list(s)
for i in range(d):
if (s1[i] in x):
x.remove(s1[i])
if y[u-1]==s1[i]:
u -= 1
elif y[u+1]==s1[i]:
u += 1
else:
if y[u-1]==0:
u -= 1
if (s1[i] in y):
finish += "NO"
ok = False
break
else:
y[u]=s1[i]
elif y[u+1]==0:
u += 1
if (s1[i] in y):
finish += "NO"
ok = False
break
else:
y[u]=s1[i]
else:
finish += "NO"
ok = False
break
if ok:
finish += "YES\n"
for i in range(52):
if y[i]!=0:
finish += y[i]
for i in range(len(x)):
finish += x[i]
print(finish)
``` | output | 1 | 15,202 | 6 | 30,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,203 | 6 | 30,406 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
import sys
from collections import deque
alf = set(list('qwertyuiopasdfghjklzxcvbnm'))
input = lambda: sys.stdin.readline().strip()
# print(help(deque))
for i in range(int(input())):
s = input()
ans = deque()
used = set([s[0]])
ans.append(s[0])
f = False
ind = 0
for i in range(len(s)-1):
if s[i+1]in used:
if ind==0:
if ans[1]==s[i+1]:
ind = 1
else:
f = True
break
elif ind == len(ans)-1:
if ans[ind-1]==s[i+1]:
ind -= 1
else:
f = True
break
else:
if ans[ind-1]==s[i+1]:
ind -= 1
elif ans[ind+1]==s[i+1]:
ind += 1
else:
f = True
break
else:
used.add(s[i+1])
if ind==0:
ans.appendleft(s[i+1])
elif ind == len(ans)-1:
ans.append(s[i+1])
ind+=1
else:
f = True
break
if f:
print('NO')
else:
print("YES")
print(''.join(ans)+''.join(list(alf-set(ans))))
``` | output | 1 | 15,203 | 6 | 30,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,204 | 6 | 30,408 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
from collections import Counter
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
t, = gil()
for _ in range(t):
s = g()
n = len(s)
graph = {}
flag = True
for i in range(n):
if s[i] not in graph:
graph[s[i]] = set()
if i :
graph[s[i]].add(s[i-1])
if i+1 < n:
graph[s[i]].add(s[i+1])
if len(graph[s[i]]) > 2:
flag = False
if n == 1:
print("YES")
print("abcdefghijklmnopqrstuvwxyz")
elif flag :
for ch in graph:
if len(graph[ch]) == 1:
break
if len(graph[ch]) == 1:
print("YES")
ans = [ch]; l = 1
while len(ans) == l:
for ch in graph[ans[-1]]:
if ch not in ans:
ans.append(ch)
break
l += 1
for ch in "abcdefghijklmnopqrstuvwxyz":
if ch not in ans :
ans.append(ch)
print("".join(ans))
else:
print("NO")
else:
print("NO")
``` | output | 1 | 15,204 | 6 | 30,409 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO | instruction | 0 | 15,205 | 6 | 30,410 |
Tags: dfs and similar, greedy, implementation
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
from fractions import gcd
import heapq
raw_input = stdin.readline
pr = stdout.write
mod=998244353
def ni():
return int(raw_input())
def li():
return list(map(int,raw_input().split()))
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return tuple(map(int,stdin.read().split()))
range = xrange # not for python 3.0+
# main code
for t in range(ni()):
s=raw_input().strip()
d=defaultdict(set)
n=len(s)
for i in range(1,n):
if s[i]==s[i-1]:
continue
d[s[i-1]].add(s[i])
d[s[i]].add(s[i-1])
f=0
c=0
st=''
for i in d:
if len(d[i])>2:
f=1
break
elif len(d[i])==2:
c+=1
else:
st=i
if f or c>max(0,len(d)-2):
pr('NO\n')
continue
q=[st]
vis=Counter()
vis[st]=1
ans=''
while q:
x=q.pop(0)
ans+=x
for i in d[x]:
if not vis[i]:
vis[i]=1
q.append(i)
for i in range(97,97+26):
if not d[chr(i)]:
ans+=chr(i)
pr('YES\n')
pr(ans+'\n')
``` | output | 1 | 15,205 | 6 | 30,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,004 | 6 | 32,008 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
for i in range(n):
l = sorted(input())
s = set()
s.update(l)
print('Yes' if ord(l[-1]) - ord(l[0]) == len(l) - 1 and len(s) == len(l) else 'No')
``` | output | 1 | 16,004 | 6 | 32,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,005 | 6 | 32,010 |
Tags: implementation, strings
Correct Solution:
```
t=int(input())
for j in range(t):
s=input()
n=len(s)
d=0
c = [s[i] for i in range(len(s))]
c.sort(reverse = False)
for i in range(n-1):
if(ord(c[i])==(ord(c[i+1])-1)):
d=d+1
if(d==(n-1)):
print("Yes")
else:
print("No")
``` | output | 1 | 16,005 | 6 | 32,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,006 | 6 | 32,012 |
Tags: implementation, strings
Correct Solution:
```
def check(s):
s = sorted(s)
for i in range(1, len(s)):
if ord(s[i]) - ord(s[i-1]) != 1:
return 'No'
return 'Yes'
n = int(input())
for i in range(n):
print(check(input()))
``` | output | 1 | 16,006 | 6 | 32,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,007 | 6 | 32,014 |
Tags: implementation, strings
Correct Solution:
```
n=int(input())
for i in range(n):
s=input()
l=[]
for j in s:
l.append(j)
l.sort()
c=0
for j in range(0,len(l)-1,1):
if(ord(l[j+1])-ord(l[j])!=1):
c=c+1
break
if(c==1):
print("No")
else:
print("Yes")
``` | output | 1 | 16,007 | 6 | 32,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,008 | 6 | 32,016 |
Tags: implementation, strings
Correct Solution:
```
for _ in range(int(input())):
s=list(input())
a=set(s)
print(['NO','YES'][len(s)==len(a) and ord(max(s))-ord((min(s)))+1==len(s)])
``` | output | 1 | 16,008 | 6 | 32,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,009 | 6 | 32,018 |
Tags: implementation, strings
Correct Solution:
```
def diverse(s):
if len(s) == 1 or len(s) == 0:
return True
s = [c for c in s]
s.sort()
if not all(abs(ord(s[i])-ord(s[i+1])) == 1 for i in range(len(s)-1)):
return False
return True
for _ in range(int(input())):
if diverse(input()):
print("Yes")
else:
print("No")
``` | output | 1 | 16,009 | 6 | 32,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,010 | 6 | 32,020 |
Tags: implementation, strings
Correct Solution:
```
n=int(input())
for i in range(n):
f=0
a=input()
b=sorted(a)
for j in range(len(a)-1):
if ord(b[j])+1==ord(b[j+1]):
continue
else:
f=1
break
if f==1:
print('No')
else:
print('Yes')
``` | output | 1 | 16,010 | 6 | 32,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1 β€ n β€ 100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No | instruction | 0 | 16,011 | 6 | 32,022 |
Tags: implementation, strings
Correct Solution:
```
n= int(input())
L = []
for i in range(n):
L.append(input())
for i in L:
i = ''.join(sorted(i))
x = True
for j in range(len(i)-1):
if (ord(i[j+1])-ord(i[j]))!=1:
x = False
if x:
print("YES")
else:
print("NO")
``` | output | 1 | 16,011 | 6 | 32,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,194 | 6 | 32,388 |
Tags: implementation, strings
Correct Solution:
```
import re
s = input().strip()
s = re.sub('\s+', ' ', s)
s = re.sub('\s?([.,!?])\s?', '\\1 ', s)
print(s)
``` | output | 1 | 16,194 | 6 | 32,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,195 | 6 | 32,390 |
Tags: implementation, strings
Correct Solution:
```
string = input()
string = " ".join(string.split())
for j in ".,!?":
string = string.split(f"{j}")
for i in range(len(string)):
string[i] = string[i].strip()
string = f"{j} ".join(string)
print(string)
``` | output | 1 | 16,195 | 6 | 32,391 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,196 | 6 | 32,392 |
Tags: implementation, strings
Correct Solution:
```
sp = False
for ch in input():
if ch == ' ':
sp = True
elif ch.islower():
print(('', ' ')[sp] + ch, end='')
sp = False
else:
print(ch, end='')
sp = True
``` | output | 1 | 16,196 | 6 | 32,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,197 | 6 | 32,394 |
Tags: implementation, strings
Correct Solution:
```
# Why do we fall ? So we can learn to pick ourselves up.
import re
ss = input().strip()
s = re.findall("[\w']+|[.,!?]",ss)
ans = ""
for i in s:
if i == ',' or i == '?' or i == '!' or i == '.':
ans = ans.strip()
ans += i+" "
else:
ans += i+" "
print(ans.strip())
``` | output | 1 | 16,197 | 6 | 32,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,198 | 6 | 32,396 |
Tags: implementation, strings
Correct Solution:
```
x=list(map(str,input()))
z=""
i=0
while (i<len(x)):
while (i<len(x)) and (x[i]!=" ") :
if (x[i]==".") or (x[i]==",") or (x[i]=="!") or (x[i]=="?") and (x[i+1]!=" "):
z=z+x[i]+" "
else:
z=z+x[i]
i+=1
if (i+1<len(x)):
if (x[i+1]!=" ") and (x[i+1]!=".")and(x[i+1]!="?")and(x[i+1]!=",")and(x[i+1]!="!")and (z[len(z)-1]!=" ") :
z=z+x[i]
i+=1
print (z)
``` | output | 1 | 16,198 | 6 | 32,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,199 | 6 | 32,398 |
Tags: implementation, strings
Correct Solution:
```
def main():
line = input()
lineout=[]
for i in range(len(line)):
if line[i] == ' ' and i+1 < len(line):
if line[i+1] == ',' or line[i+1] == '.' or line[i+1] == '!' or line[i+1] == '?':
pass
elif line[i+1] == ' ':
pass
else:
lineout.append(' ')
elif (line[i] == ',' or line[i] == '.' or line[i] == '!' or line[i] == '?' )and i+1 < len(line):
if line[i+1] == ' ':
lineout.append(line[i])
else:
lineout.append(line[i])
lineout.append(' ')
else:
lineout.append(line[i])
print(''.join(lineout))
if __name__ == '__main__':
main()
``` | output | 1 | 16,199 | 6 | 32,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,200 | 6 | 32,400 |
Tags: implementation, strings
Correct Solution:
```
s = input()
for ch in ',.!?':
s = s.replace(ch, ch + ' ')
while ' ' in s:
s = s.replace(' ', ' ')
for ch in ',.!?':
s = s.replace(' ' + ch, ch)
print(s.strip())
``` | output | 1 | 16,200 | 6 | 32,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa | instruction | 0 | 16,201 | 6 | 32,402 |
Tags: implementation, strings
Correct Solution:
```
d=[',','?','.',';','!']
z=input()
for t in d:
z=z.replace(t,'%s '%t)
for i in range(10,0,-1):
z=z.replace(' '*i,' ')
for j in d:
if j in z:
z=z.replace(' %s'%j,j)
print(z)
``` | output | 1 | 16,201 | 6 | 32,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
s=str(input())
l=',.!?'
e=''
for i in range(0,len(s)):
if(s[i] not in l and s[i]!=' '):
e=e+s[i]
elif(s[i]==' '):
if(e[-1]==' '):
pass
else:
e=e+s[i]
else:
if(e[-1]==' '):
e=e[0:len(e)-1]
e=e+s[i]+' '
else:
e=e+s[i]+' '
print(e)
``` | instruction | 0 | 16,202 | 6 | 32,404 |
Yes | output | 1 | 16,202 | 6 | 32,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
S=input()
U=['.',',','!','?',]
N=S
for u in U:
E=N.split(u)
N=''
for s in E:
N=N+s+u+' '
E=N.split()
N=''
for s in E:
if s[0] in U:
N=N[:-1]+s[0]+' '+s[1:]
else:
N=N+s+' '
print(N[:-5])
``` | instruction | 0 | 16,203 | 6 | 32,406 |
Yes | output | 1 | 16,203 | 6 | 32,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
import re
s=re.sub("\ +"," ",input())
s=re.sub(' *([,.!?]) *',r'\1 ',s)
print(s)
``` | instruction | 0 | 16,204 | 6 | 32,408 |
Yes | output | 1 | 16,204 | 6 | 32,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
s=input().strip()
znak=['.',',','!','?']
res=''
for i in s:
if i.isalpha():
res+=i
continue
elif i==' ' and res[-1]!=' ':
res+=i
continue
elif i in znak:
if res[-1]==' ':
res=res[:-1]
res+=i+' '
print(res)
``` | instruction | 0 | 16,205 | 6 | 32,410 |
Yes | output | 1 | 16,205 | 6 | 32,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
string = input().split()
while '' in string:
string.remove('')
while ' ' in string:
string.remove(' ')
answer = ' '.join(string)
answer = answer.replace(',',', ')
answer = answer.replace('.','. ')
answer = answer.replace('!','! ')
answer = answer.replace('?','? ')
answer = answer.replace(' ,',',')
answer = answer.replace(' .','.')
answer = answer.replace(' !','!')
answer = answer.replace(' ?','?')
print(answer)
``` | instruction | 0 | 16,206 | 6 | 32,412 |
No | output | 1 | 16,206 | 6 | 32,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
s = input().split()
t = []
for i in s:
b = i.replace(",",", ")
a = b.split()
t.extend(a)
x = " ".join(t)
x = x.replace(" ,",",")
print (x)
``` | instruction | 0 | 16,207 | 6 | 32,414 |
No | output | 1 | 16,207 | 6 | 32,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
sntc = [x.strip() for x in input().split()]
for i in range(len(sntc)):
for ch in ['.', ',', '!', '?']:
for _ in range(sntc[i].count(ch)):
if sntc[i][0] == ch:
sntc[i] = sntc[i][1:]
sntc[i - 1] += ch
else:
pts = sntc[i].split(ch)
sntc[i] = (ch + ' ').join(pts)
print(' '.join(sntc).replace(' ', ' '))
``` | instruction | 0 | 16,208 | 6 | 32,416 |
No | output | 1 | 16,208 | 6 | 32,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
* if there is no punctuation mark between two words, then they should be separated by exactly one space
* there should be no spaces before each punctuation mark
* there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
Input
The input data contains of a single non-empty line β the text whose length is no more than 10000 characters.
Output
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Examples
Input
galileo galilei was an italian physicist ,mathematician,astronomer
Output
galileo galilei was an italian physicist, mathematician, astronomer
Input
galileo was born in pisa
Output
galileo was born in pisa
Submitted Solution:
```
#!/usr/bin/python3
input_text = input()
puncs = ['.','!',',', '?']
for punc in puncs:
input_text = input_text.replace(punc,f"{punc} ")
input_text = input_text.replace(" "," ")
input_text = input_text.replace(f" {punc}",punc)
print(input_text.replace(" "," "))
``` | instruction | 0 | 16,209 | 6 | 32,418 |
No | output | 1 | 16,209 | 6 | 32,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,242 | 6 | 32,484 |
Tags: dp
Correct Solution:
```
from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
#sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
#sys.stdout = BytesIO()
#register(lambda: os.write(1, sys.stdout.getvalue()))
import io
#input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
#sys.stdin = open("F:\PY\\test.txt", "r")
input = lambda: sys.stdin.readline().rstrip("\r\n")
#input = sys.stdin.readline
def fun1(i, k):
if i<=k:
return i
else:
return k-(i-k)
#solved by dp
s = ' '+input()
lenS = len(s)
dpU = [0]*(lenS+1)
dpL = [0]*(lenS+1)
answer = 0
for i in range(1, lenS):
dpU[i]=dpU[i-1]
if s[i].islower():
dpU[i]+=1
answer = dpU[lenS-1]
for i in range(lenS-1, 0, -1):
dpL[i]=dpL[i+1]
if s[i].isupper():
dpL[i]+=1
answer = min(answer, dpL[i]+dpU[i-1])
print(answer)
sys.exit(0)
def gcs(a, b):
n = len(a)
m = len(b)
f = [[0]*(n+1) for i in range(m+1)]
for i in range(1, n):
for j in range(1, m):
if a[i]==b[j]:
f[i][j]=f[i-1][j-1]+1
else:
f[i][j]=max(f[i-1][j], f[i][j-1])
i, j, l = n-1, m-1, 0
answer = []
while(i!=0):
if f[i][j]==f[i-1][j]+1 and f[i][j]==f[i][j-1]+1:
print(i, j, l)
answer.append(a[i])
i-=1
l = j-1
j-=1
if j==0:
i-=1
j=l
for i in range(0,n):
for j in range(0,m):
print(f[i][j], end=" ")
print("\n", end="")
print(answer)
return f[n-1][m-1]
print(gcs('1baccbca', '2abcabaac'))
for t in range(int(input())):
n = int(input())
ar1 = [' ']+list(map(str, input().split()))
#ar1 = [' ']+ar1
ar2 = [' ']+list(map(str, input().split()))
dp = [[0]*(n+1) for i in range(n+1)]
for i in range(1, n+1):
for j in range(1, n+1):
dp[i][j]=dp[i-1][j]
if ar1[i]==ar2[j]:
dp[i][j]=dp[i-1][j-1]+1
else:
dp[i][j]=max(dp[i][j-1], dp[i-1][j])
'''
sys.exit(0)
lV = [0]*10
lV[1]=1
for i in range(2, 10):
if i<10:
lV[i]=lV[i-1]+i
else:
lV[i]=lV[i-1]+1+i%10
#it's okey
def findOne(n, k):
for i in range(1, 10):
sumA=i
sumA+=lV[min(i+k, 9)]-lV[i]
sumA+=lV[max(i+k-9, 0)]
print(sumA)
if sumA==n:
return i
return 0
findOne(7,9)
for t in range(int(input())):
n, k = map(int, input().split())
oneS = findOne(n,k)
if oneS!=0:
print(oneS)
continue
n-=lV[k]
if n<0:
print(-1)
continue
print(lV)
'''
``` | output | 1 | 16,242 | 6 | 32,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,243 | 6 | 32,486 |
Tags: dp
Correct Solution:
```
string=input()
n=len(string)
upper=0
lower=0
count=0
i=0
k=n-1
while i<n and ord(string[i])<97:
i+=1
for j in range(n-1,i-1,-1):
if ord(string[j])>=97:
lower+=1
else:
upper+=1
if lower>=upper:
count+=upper
lower=0
upper=0
if j==i and lower<upper:
count+=lower
print(count)
``` | output | 1 | 16,243 | 6 | 32,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,244 | 6 | 32,488 |
Tags: dp
Correct Solution:
```
s = input()
n = len(s)
a,b = 0,0
i = 0
j = n-1
while i<n and s[i].isupper():
i+=1
while j>=0 and s[j].islower():
j-=1
aa = []
if i==n or j==-1:
print(0)
else:
j+=1
if s[0].islower():
i = 0
if s[-1].isupper():
j = n
bb,cc=0,0
# print(s[i:j])
for x in s[i:j]:
if x.isupper():
if bb>0:
aa.append(-1*bb)
bb = 0
cc = 0
cc+=1
a+=1
else:
if cc>0:
aa.append(cc)
bb = 0
cc = 0
bb+=1
b+=1
if s[j-1].isupper():
aa.append(cc)
else:
aa.append(-1*bb)
nn = len(aa)
ll = [0 for _ in range(nn)]
rr = [0 for _ in range(nn)]
for i in range(nn):
if aa[i]<0:
ll[i] = (ll[i-1] if i>0 else 0)+abs(aa[i])
else:
ll[i] = (ll[i-1] if i>0 else 0)
for i in range(nn-1,-1,-1):
if aa[i]>0:
rr[i] = (rr[i+1] if i<nn-1 else 0) + aa[i]
else:
rr[i] = (rr[i+1] if i<nn-1 else 0)
ans = 10**18
for i in range(len(aa)-1):
if aa[i]>0 and aa[i+1]<0:
gg = (ll[i-1] if i>0 else 0) + (rr[i+2] if i+2<nn else 0)
ans = min(ans,gg)
# print(aa)
# print(a,b,ans)
print(min(a,b,ans))
``` | output | 1 | 16,244 | 6 | 32,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,245 | 6 | 32,490 |
Tags: dp
Correct Solution:
```
s=input()
a,r=0,0
for x in s:
if x.islower():a+=1
elif x.isupper() and a>0:a-=1;r+=1
print(r)
``` | output | 1 | 16,245 | 6 | 32,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,246 | 6 | 32,492 |
Tags: dp
Correct Solution:
```
t = [0] + [i < 'a' for i in input()]
for i in range(2, len(t)):
t[i] += t[i - 1]
s = t[-1]
print(min(s - 2 * j + i for i, j in enumerate(t)))
``` | output | 1 | 16,246 | 6 | 32,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,247 | 6 | 32,494 |
Tags: dp
Correct Solution:
```
s=input()
count=1
ans=[]
for i in range(1,len(s)):
if((s[i].isupper() and s[i-1].isupper()) or (s[i].islower() and s[i-1].islower())):
count+=1
else:
ans.append(count)
count=1
ans.append(count)
dp=[0 for i in range(len(ans))]
if(s[0].isupper()):
sm=sum(ans[1::2])
lm=sum(ans[0::2])
c1=ans[0]
c2=0
dp[0]=lm-ans[0]
for i in range(1,len(ans)):
if(i%2==1):
dp[i]=lm
c2+=ans[i]
else:
c1+=ans[i]
dp[i]=lm-c1+c2
print(min(dp))
else:
#c1 denotes capital
lm=sum(ans[1::2])
sm=sum(ans[0::2])
c2=ans[0]
c1=0
dp[0]=lm
for i in range(1,len(ans)):
if(i%2==1):
c1+=ans[i]
dp[i]=c2+lm-c1
else:
dp[i]=lm
c2+=ans[i]
print(min(dp))
``` | output | 1 | 16,247 | 6 | 32,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number β the least number of actions needed to make the message fancy.
Examples
Input
PRuvetSTAaYA
Output
5
Input
OYPROSTIYAOPECHATALSYAPRIVETSTASYA
Output
0
Input
helloworld
Output
0 | instruction | 0 | 16,248 | 6 | 32,496 |
Tags: dp
Correct Solution:
```
s = list(input())
n = len(s)
l=u=0
for i in s:
if i.isupper():
u+=1
else:
l+=1
i=low=0
ans=min(u,l)
while i<n and s[i].isupper():
u-=1
i+=1
for j in range(i,n):
if s[j].islower():
ans=min(ans,low+u)
low+=1
else:
u-=1
print(ans)
``` | output | 1 | 16,248 | 6 | 32,497 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.