description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = list(map(int, input().split())) s, d, c = list(), list(), list() for i in range(m): si, di, ci = list(map(int, input().split())) s.append(si - 1) d.append(di - 1) c.append(ci) s, d, c, num = zip(*sorted(list(zip(s, d, c, list(range(m)))), key=lambda x: x[1])) exam_num = [(-1) for _ in range(n)] for i in range(m): exam_num[d[i]] = i cnt = [(0) for _ in range(m)] def run(): res = [] for i in range(n): j = exam_num[i] if j != -1: if cnt[j] < c[j]: print(-1) return res.append(m + 1) else: next_exam = -1 for j in range(m): if s[j] <= i and d[j] > i and cnt[j] < c[j]: next_exam = j break if next_exam == -1: res.append(0) else: res.append(num[j] + 1) cnt[j] += 1 print(" ".join([str(x) for x in res])) run()
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
def update(si, ei, di, lst, i, ee): cur = si cnt = 0 while cnt < di and cur < ei: if lst[cur] == 0: lst[cur] = i + 1 cnt += 1 cur += 1 lst[ei] = ee + 1 return True if cnt >= di else False n, e = map(int, input().split()) lst = [0] * n fl = True query = [] for i in range(e): si, ei, di = map(int, input().split()) query.append((si, ei, di, i)) query.sort(key=lambda e: e[1]) for el in query: si, ei, di, i = el si -= 1 ei -= 1 if ei - si < di: fl = False break elif not update(si, ei, di, lst, i, e): fl = False break if not fl: print(-1) else: print(" ".join(map(str, lst)))
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
rd = lambda: map(int, input().split()) n, m = rd() a = sorted(([*rd()] + [i + 1] for i in range(m)), key=lambda x: x[1]) r = [0] * n for x in a: r[x[1] - 1] = m + 1 for i in range(x[0] - 1, x[1] - 1): if not r[i]: r[i] = x[3] x[2] -= 1 if not x[2]: break if x[2]: print(-1) exit() print(*r)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) ls = [(0) for i in range(n)] prep = [] order = [] for i in range(m): s, d, c = map(int, input().split()) ls[d - 1] = m + 1 prep.append([s, d, c]) order.append([d, i]) order.sort() for i in range(n): if ls[i] == 0: for j in range(m): if prep[order[j][1]][0] - 1 <= i < prep[order[j][1]][1] - 1: if prep[order[j][1]][2]: ls[i] = order[j][1] + 1 prep[order[j][1]][2] -= 1 break flag = True for i in range(m): if prep[i][2] != 0: flag = False if flag: print(*ls) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
l = input().split() n = int(l[0]) m = int(l[1]) l = [] hashipapers = dict() curr = [(0) for i in range(m)] for i in range(m): lo = input().split() s = int(lo[0]) d = int(lo[1]) c = int(lo[2]) hashipapers[d - 1] = i, c l.append((d - 1, s - 1, c, i)) l.sort() lfi = [] poss = 1 for i in range(n): if i in hashipapers: lfi.append(m + 1) if curr[hashipapers[i][0]] < hashipapers[i][1]: poss = 0 break continue found = 0 for j in l: if j[0] > i and i >= j[1] and curr[j[3]] < j[2]: found = 1 curr[j[3]] += 1 lfi.append(j[3] + 1) break if found == 0: lfi.append(0) if poss == 0: print(-1) else: for i in lfi: print(i, end=" ")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
def main(): n, m = map(int, input().split()) a = [] ans = [0] * n for i in range(m): x, y, z = map(int, input().split()) ans[y - 1] = m + 1 a.append([y - 1, x - 1, z, i + 1]) a.sort() for i in range(m): count = 0 for j in range(a[i][1], a[i][0]): if count == a[i][2]: break if ans[j] == 0: ans[j] = a[i][3] count += 1 if count < a[i][2]: print(-1) return for i in range(n): print(ans[i], end=" ") main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
def sol(): n, m = list(map(int, input().split())) arr = [] for d in range(m): preArr = list(map(int, input().split())) preArr[0] -= 1 preArr[1] -= 1 arr.append(preArr) out = [m] * n for i in range(n): ind = 999999999999999 exm = False exmDate = 9999999999999 for g in range(m): if arr[g][1] == i: exm = True break if arr[g][1] > i and arr[g][2] > 0 and arr[g][0] <= i: if arr[g][1] < exmDate: exmDate = arr[g][1] ind = g if exm: out[i] = m + 1 elif exmDate == 9999999999999: out[i] = 0 else: out[i] = ind + 1 arr[ind][2] -= 1 for i in range(m): if arr[i][2] != 0: break else: print(*out) return 0 print(-1) sol()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
f = lambda: map(int, input().split()) n, m = f() t = [] p = [0] * (n + 1) for i in range(1, m + 1): s, d, c = f() if p[d]: exit(print(-1)) p[d] = m + 1 t.append((i, s, d, c)) t.sort(key=lambda q: q[2]) for i, s, d, c in t: while c: if not p[s]: p[s] = i c -= 1 if s == d: exit(print(-1)) s += 1 print(*p[1:])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
from sys import stdin input = stdin.buffer.readline n, m = map(int, input().split()) exams = [] for i in range(m): s, d, c = map(int, input().split()) exams.append((i, s, d, c)) exams.sort(key=lambda x: x[2]) dp = [-1] + [0] * n for i in exams: dp[i[2]] = m + 1 flag = False for exam in exams: s = exam[1] d = exam[2] c = exam[3] while s < d and c > 0: if dp[s] == 0: dp[s] = exam[0] + 1 c = c - 1 s = s + 1 if c > 0: flag = True break if flag: print(-1) else: print(*dp[1:])
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = [int(i) for i in input().split()] s = [0] * m d = [0] * m c = [0] * m for i in range(m): s[i], d[i], c[i] = [(int(j) - 1) for j in input().split()] c[i] += 1 if d[i] - s[i] < c[i]: print(-1) quit() ans = [0] * n for i in d: ans[i] = m + 1 exam = [] for i in range(n): exam.append([]) for j in range(m): if s[j] <= i < d[j]: exam[i].append(j) for i in range(n): if ans[i] == 0: day_min = n + 1 for j in exam[i]: if d[j] < day_min and c[j] > 0: day_min = d[j] exam_min = j if day_min != n + 1: ans[i] = exam_min + 1 c[exam_min] -= 1 elif c[d.index(i)] != 0: print(-1) quit() for i in range(n): if i != n - 1: print(ans[i], end=" ") else: print(ans[i])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) res = [] ans = [0] * (n + 1) ind = [0] * (n + 1) for i in range(m): s, d, c = map(int, input().split()) ind[d] = i + 1 res.append([d, s, c]) res.sort() flag = 0 for i in range(m): [d, s, c] = res[i] j = s ex = ind[d] ans[d] = m + 1 while c > 0 and j < d: if ans[j] == 0: ans[j] = ex c -= 1 j += 1 if c != 0: print(-1) flag = 1 break if flag == 0: print(*ans[1:])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
T = input().split(" ") n = int(T[0]) m = int(T[1]) L = [0] * n M = [] D = [0] * m F = [0] * m for i in range(m): S = input().split(" ") a = int(S[0]) b = int(S[1]) c = int(S[2]) L[b - 1] = m + 1 F[i] = b - 1 M.append((a - 1, b - 2, i + 1)) D[i] = c M.sort() for i in range(n): if L[i] == 0: G = [] for j in range(len(M)): if M[j][0] <= i: G.append((M[j][1], M[j][2])) G.sort() for k in range(len(G)): if D[G[k][1] - 1] > 0 and F[G[k][1] - 1] > i: D[G[k][1] - 1] -= 1 L[i] = G[k][1] break b = True for j in range(len(D)): if D[j] > 0: b = False if b: for i in range(n - 1): print(L[i], end=" ") print(L[n - 1]) else: print(-1)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
T = int(input()) for i in range(T): counter = {} string = input().strip() for char in string: counter[char] = counter.get(char, 0) + 1 chars = list(counter.keys()) chars.sort() if len(chars) == 2 and abs(ord(chars[0]) - ord(chars[1])) == 1: print("No answer") elif ( len(chars) == 3 and abs(ord(chars[0]) - ord(chars[1])) == 1 and abs(ord(chars[1]) - ord(chars[2])) == 1 ): print("No answer") elif len(chars) == 3: if abs(ord(chars[0]) - ord(chars[1])) == 1: print( counter[chars[0]] * chars[0] + counter[chars[2]] * chars[2] + counter[chars[1]] * chars[1] ) else: print( counter[chars[2]] * chars[2] + counter[chars[0]] * chars[0] + counter[chars[1]] * chars[1] ) else: ans = [] if len(chars) > 1: for idx in range(1, len(chars), 2): ans.append(chars[idx] * counter[chars[idx]]) for idx in range(0, len(chars), 2): ans.append(chars[idx] * counter[chars[idx]]) print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
import sys T = int(input()) for t in range(T): q = True s = input() n = len(s) a = [] b = [] x = [] for i in range(n): o = ord(s[i]) if o % 2 == 0: a.append(o) else: b.append(o) if len(a) == 0 or len(b) == 0: print(s) q = False if q: a.sort() b.sort() if abs(a[-1] - b[0]) != 1: q = False x = a + b if abs(b[-1] - a[0]) != 1: q = False x = b + a if q: print("No answer") else: s = "" for i in range(n): s += chr(x[i]) print(s)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
T = int(input()) for i in range(T): s = list(input()) s.sort() k = len(s) s1 = [] s3 = [] s2 = [0] * 150 pro = True for j in range(k): s2[ord(s[j])] += 1 for j in range(97, 123, 2): if s2[j] > 0: s3 += [chr(j)] * s2[j] for j in range(98, 123, 2): if s2[j] > 0: s3 += [chr(j)] * s2[j] for j in range(98, 123, 2): if s2[j] > 0: s1 += [chr(j)] * s2[j] for j in range(97, 123, 2): if s2[j] > 0: s1 += [chr(j)] * s2[j] for j in range(k - 1): if abs(ord(s1[j]) - ord(s1[j + 1])) == 1: pro = False break if pro: print(*s1, sep="") else: pro = True for j in range(k - 1): if abs(ord(s3[j]) - ord(s3[j + 1])) == 1: pro = False if pro: print(*s3, sep="") else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) ss = [] for i in range(n): ss.append(input()) for i in range(n): s = ss[i] f = [ord(a) for a in s] s_even = sorted([x for x in f if x % 2 == 0]) s_odd = sorted([x for x in f if x % 2 == 1]) if s_even == []: print("".join(map(chr, s_odd))) elif s_odd == []: print("".join(map(chr, s_even))) elif s_even[-1] - s_odd[0] == 1 or s_even[-1] - s_odd[0] == -1: if s_odd[-1] - s_even[0] == 1 or s_odd[-1] - s_even[0] == -1: print("No answer") else: print("".join(map(chr, s_odd + s_even))) else: print("".join(map(chr, s_even + s_odd)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR LIST EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
T = int(input().strip()) for y in range(T): st = input().strip() l = [] for k in range(26): l.append(0) for i in range(0, len(st)): l[ord(st[i]) - ord("a")] += 1 nlist = [] for k in range(26): x = 0 if l[k]: nlist.append(k) if len(nlist) == 1: print(st) elif len(nlist) == 2: if nlist[1] - nlist[0] == 1: print("No answer") else: print(st) elif len(nlist) == 3: if nlist[1] - nlist[0] == 1 and nlist[2] - nlist[1] == 1: print("No answer") else: if nlist[1] - nlist[0] == 1: sr = ( chr(nlist[-2] + ord("a")) * l[nlist[-2]] + chr(nlist[-1] + ord("a")) * l[nlist[-1]] + chr(nlist[-3] + ord("a")) * l[nlist[-3]] ) else: sr = ( chr(nlist[-2] + ord("a")) * l[nlist[-2]] + chr(nlist[-3] + ord("a")) * l[nlist[-3]] + chr(nlist[-1] + ord("a")) * l[nlist[-1]] ) print(sr) else: sr = "" q = 0 for i in range(0, len(nlist) // 4): sr1 = "" sr1 = sr1 + chr(nlist[4 * i + 2] + ord("a")) * l[nlist[4 * i + 2]] sr1 = sr1 + chr(nlist[4 * i] + ord("a")) * l[nlist[4 * i]] sr1 = sr1 + chr(nlist[4 * i + 3] + ord("a")) * l[nlist[4 * i + 3]] sr1 = sr1 + chr(nlist[4 * i + 1] + ord("a")) * l[nlist[4 * i + 1]] sr = sr + sr1 if len(nlist) % 4 == 1: sr = sr + chr(nlist[-1] + ord("a")) * l[nlist[-1]] elif len(nlist) % 4 == 2: sr = ( chr(nlist[-1] + ord("a")) * l[nlist[-1]] + sr + chr(nlist[-2] + ord("a")) * l[nlist[-2]] ) elif len(nlist) % 4 == 3: sr = ( chr(nlist[-2] + ord("a")) * l[nlist[-2]] + sr + chr(nlist[-1] + ord("a")) * l[nlist[-1]] + chr(nlist[-3] + ord("a")) * l[nlist[-3]] ) print(sr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR STRING VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
T = int(input()) for _ in range(T): s = list(input()) s.sort() groups = [] letter = s[0] count = 1 for c in s[1:]: if letter == c: count += 1 else: groups.append((ord(letter), count)) letter = c count = 1 groups.append((ord(letter), count)) last = groups[0] i = 1 while i < len(groups): g = groups[i] if abs(g[0] - last[0]) == 1: if i + 1 == len(groups) or abs(groups[i + 1][0] - last[0]) == 1: z = groups[0] groups.remove(z) groups.insert(i - 1, z) last = z if abs(z[0] - g[0]) != 1: i -= 1 else: groups.remove(g) groups.append(g) i -= 1 else: last = g i += 1 failed = False last = groups[0] for i in range(1, len(groups)): if abs(last[0] - groups[i][0]) == 1: print("No answer") failed = True break last = groups[i] if not failed: s = "" for g in groups: s += chr(g[0]) * g[1] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
alphabetlist = [ "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", ] dic = {i: e for e, i in enumerate(alphabetlist)} for _ in range(int(input())): S = input() used = [0] * 26 for i in range(len(S)): id = dic[S[i]] used[id] += 1 s = -1 t = -1 for i in range(0, 26, 2): for j in range(1, 26, 2): if abs(i - j) != 1 and used[i] and used[j]: s, t = i, j break else: continue break else: checko = False checke = False for i in range(0, 26, 2): checko |= used[i] for i in range(1, 26, 2): checke |= used[i] if checko and checke: print("No answer") else: print(S) continue anso = [alphabetlist[s]] * used[s] for i in range(0, 26, 2): if i != s: for j in range(used[i]): anso.append(alphabetlist[i]) anso = anso[::-1] anse = [alphabetlist[t]] * used[t] for i in range(1, 26, 2): if i != t: for j in range(used[i]): anse.append(alphabetlist[i]) ans = anso + anse print("".join(ans))
ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): line = input() items = sorted([(x * line.count(x)) for x in set(line)]) for start in range(len(items)): copy = items[:] string = copy.pop(start) while copy: for i in range(len(copy)): if abs(ord(copy[i][0]) - ord(string[-1])) != 1: string += copy.pop(i) break else: break if copy: continue else: print(string) break else: print("No answer")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = sorted(input()) a = b = "" for e in s: if ord(e) % 2: a += e else: b += e f = 0 p = "0" for e in a + b: if abs(ord(e) - ord(p)) == 1: f |= 1 p = e p = "0" for e in b + a: if abs(ord(e) - ord(p)) == 1: f |= 2 p = e if f == 3: s = "No answer" elif f == 1: s = b + a else: s = a + b print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def ok(k): t = True for i in range(len(k) - 1): t &= abs(ord(k[i]) - ord(k[i + 1])) != 1 return t n = int(input()) for i in range(n): f = list(input()) odd = [] even = [] h = len(f) for i in range(h): if ord(f[i]) % 2 != 0: odd.append(f[i]) else: even.append(f[i]) odd.sort() even.sort() if ok(odd + even): print("".join(odd + even)) elif ok(even + odd): print("".join(even + odd)) else: print("No answer")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for i in range(t): s = input() a = [(0) for j in range(26)] c = [] for x in s: a[ord(x) - 97] += 1 for j in range(len(a)): if a[j] != 0: c.append(j) b = len(c) d = b // 2 if b == 1: print(s) elif b == 2: flag = True for j in range(len(a) - 1): if a[j] > 0 and a[j + 1] > 0: print("No answer") flag = False break if flag: print(s) elif b == 3: flag = True memo = -1 for j in range(len(a) - 2): if a[j] > 0 and a[j + 1] > 0 and a[j + 2] > 0: print("No answer") flag = False break if a[j] > 0 and a[j + 1] > 0: memo = j if flag: if memo < 0: print(s) else: print(chr(c[1] + 97) * a[c[1]], end="") if c[1] - c[0] == 1: print(chr(c[2] + 97) * a[c[2]], end="") print(chr(c[0] + 97) * a[c[0]], end="") else: print(chr(c[0] + 97) * a[c[0]], end="") print(chr(c[2] + 97) * a[c[2]], end="") print("") else: for j in range(d): print(chr(c[d + j] + 97) * a[c[d + j]], end="") print(chr(c[j] + 97) * a[c[j]], end="") if b % 2 == 1: print(chr(c[-1] + 97) * a[c[-1]], end="") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for i in range(t): s = input() s = sorted(s) t = s[0] k = len(s) - 1 j = 0 cnt = 0 k = 0 asd = len(s) while cnt < asd: if j + 1 >= len(s): break if s[j + 1] == t[-1]: t = t + s[j + 1] j += 1 k = 0 cnt += 1 continue if k % 2 == 0: t = t + s[-1] s = s[:-1] while t[-1] == s[-1]: t = t + s[-1] s = s[:-1] cnt += 1 k += 1 else: t = t + s[1 + j] j += 1 k += 1 cnt += 1 l = list(t) cnt = 0 while abs(ord(t[0]) - ord(t[-1])) != 1 and cnt < len(t): t = t[-1] + t t = t[:-1] cnt += 1 ch = 0 l = list(t) for i in range(1, len(t)): if abs(ord(l[i]) - ord(l[i - 1])) == 1: ch = 1 if ch == 1: print("No answer") else: print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def parse(): n = int(input()) inp = [] for _ in range(n): inp.append(input()) return inp def recurse(prev, s): s = "".join(s) if len(s) == 1: if not prev or abs(ord(prev) - ord(s)) != 1: return s else: return None for l in s: if not prev or abs(ord(prev) - ord(l)) != 1: r = recurse(l, s.replace(l, "")) if r: return l + r return None def main(): inp = parse() for w in inp: s = list(set(list(w))) ret = recurse(None, s) if ret: for l in ret: print(l * w.count(l), end="") print() else: print("No answer") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN NONE FOR VAR VAR IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING IF VAR RETURN BIN_OP VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NONE VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(u): for i in range(len(u) - 1): if abs(ord(u[i]) - ord(u[i + 1])) == 1: return False return True t = int(input()) for _ in range(t): s = input() cnt = {chr(i): (0) for i in range(ord("a"), ord("z") + 1)} for x in s: cnt[x] += 1 s = sorted(list(set(list(s)))) if ( len(s) == 2 and ord(s[1]) == ord(s[0]) + 1 or len(s) == 3 and ord(s[1]) - ord(s[0]) == ord(s[2]) - ord(s[1]) == 1 ): print("No answer") continue if len(s) == 3: if ord(s[0]) == ord(s[1]) - 1: print(s[0] * cnt[s[0]] + s[2] * cnt[s[2]] + s[1] * cnt[s[1]]) else: print(s[1] * cnt[s[1]] + s[0] * cnt[s[0]] + s[2] * cnt[s[2]]) continue a = [s[i] for i in range(len(s)) if i % 2 == 0][::-1] b = [s[i] for i in range(len(s)) if i % 2 == 1][::-1] ans = "" for x in a: ans += x * cnt[x] for x in b: ans += x * cnt[x] print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
gi = lambda: list(map(int, input().strip().split())) for k in range(gi()[0]): s = list(input()) ans = [] s.sort() ans.append(s.pop(0)) flag = False while s: flag = True for j in range(len(s)): if abs(ord(s[j]) - ord(ans[-1])) != 1: ans.append(s.pop(j)) flag = False break if abs(ord(s[j]) - ord(ans[0])) != 1: ans.insert(0, s.pop(j)) flag = False break for i in range(1, len(ans)): if ( abs(ord(ans[i]) - ord(s[j])) != 1 and abs(ord(ans[i - 1]) - ord(s[j])) != 1 ): ans.insert(i, s.pop(j)) flag = False break if flag: break if flag: print("No answer") else: print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def solve(s): ans = [] c_count = [0] * 26 for c in s: c_count[ord(c) - ord("a")] += 1 conflicts = [ (c_count[i - 1] + c_count[(i + 1) % len(c_count)]) for i in range(len(c_count)) ] conflicts[0] -= c_count[-1] conflicts[-1] -= c_count[0] last = max( range(len(conflicts)), key=lambda i: 200 * conflicts[i] + c_count[i] if c_count[i] else 0, ) ans.append(chr(ord("a") + last)) c_count[last] -= 1 if last > 0: conflicts[last - 1] -= 1 if last < len(conflicts) - 1: conflicts[last + 1] -= 1 while any(c_count): new = max( range(len(conflicts)), key=lambda i: ( 200 * conflicts[i] + c_count[i] if c_count[i] and abs(i - last) != 1 else 0 ), ) if not c_count[new] or abs(new - last) == 1: print("No answer") return last = new c_count[last] -= 1 if last > 0: conflicts[last - 1] -= 1 if last < len(conflicts) - 1: conflicts[last + 1] -= 1 ans.append(chr(ord("a") + last)) print("".join(ans)) t = int(input()) for _ in range(t): solve(input())
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = input().strip() arr = [(0) for __ in range(26)] for c in s: arr[ord(c) - ord("a")] += 1 s = "" for i in range(26): if arr[i]: s += chr(i + ord("a")) else: s += " " srr = s.split() if len(srr) == 1 and 2 <= len(srr[0]) <= 3: print("No answer") continue result = "" srr.sort(key=lambda x: len(x), reverse=True) for i in range(len(srr)): s = srr[i] if len(s) == 1: result += s[0] * arr[ord(s[0]) - ord("a")] srr[i] = "" continue for j in range(1, len(s), 2): result += s[j] * arr[ord(s[j]) - ord("a")] ss = "" for j in range(0, len(s), 2): ss += s[j] srr[i] = ss for i in range(len(srr)): s = srr[i] for c in s: result += c * arr[ord(c) - ord("a")] print(result) for i in range(1, len(result)): c1 = result[i - 1] c2 = result[i] if abs(ord(c1) - ord(c2)) == 1: print("No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for t in range(int(input())): s = str(input()) l = set(s) p = "" l = list(l) l.sort() i = 0 while i < len(l): p += l[i] i += 2 i = 1 p1 = "" while i < len(l): p1 += l[i] i += 2 ans = "" if len(p) >= 1 and len(p1) >= 1: if abs(ord(p[len(p) - 1]) - ord(p1[0])) == 1: p = p[-1] + p[: len(p) - 1] p1 = p1[1 : len(p1)] + p1[0] temp = p + p1 for i in temp: ans += i * s.count(i) check = 0 for i in range(len(ans) - 1): if abs(ord(ans[i]) - ord(ans[i + 1])) == 1: check = -1 if check == -1: print("No answer") else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): x = input().strip() c = [0] * 26 for i in x: c[ord(i) - ord("a")] += 1 i = 0 s = "" for i in range(0, 26, 2): s += chr(i + ord("a")) * c[i] tf = "" for i in range(1, 26, 2): tf += chr(i + ord("a")) * c[i] if len(tf) == 0: print(s) continue if len(s) == 0: print(tf) continue for t in [tf, tf[::-1]]: for s in [s, s[::-1]]: if abs(ord(s[-1]) - ord(t[0])) != 1: print(s + t) break else: continue break else: print("No answer")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR LIST VAR VAR NUMBER FOR VAR LIST VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) a = [] for i in range(t): a.append(input()) for i in a: c = [] for j in i: c.append(j) k = list(set(c)) k.sort() s = "" t = "" for j in range(len(k)): if j % 2 == 0: s += k[j] * c.count(k[j]) else: t += k[j] * c.count(k[j]) if len(k) == 2: if ord(k[-1]) - ord(k[0]) == 1: print("No answer") else: print(s[::-1] + t[::-1]) elif len(k) == 3: if ord(k[-1]) - ord(k[0]) == 2: print("No answer") elif ord(k[1]) - ord(k[0]) == 1: print(s + t) elif ord(k[2]) - ord(k[1]) == 1: print(s[::-1] + t) else: print(s + t) else: print(s[::-1] + t[::-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = input() s = sorted(s) a = "" b = "" for i in s: if ord(i) % 2: a = a + i else: b = b + i if len(a) == 0: print(b) elif len(b) == 0: print(a) elif abs(ord(a[-1]) - ord(b[0])) != 1: print(a + b) elif abs(ord(b[-1]) - ord(a[0])) != 1: print(b + a) else: print("No answer")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for k in range(int(input())): a = sorted(input()) s1 = "" s2 = "" for i in a: if ord(i) % 2: s1 += i else: s2 += i ans1 = s1 + s2 ans2 = s2 + s1 flag = 0 for i in range(1, len(ans1)): if abs(ord(ans1[i]) - ord(ans1[i - 1])) == 1: flag = 1 break if flag: flag = 0 ans1 = ans2 for i in range(1, len(ans1)): if abs(ord(ans1[i]) - ord(ans1[i - 1])) == 1: flag = 1 break if flag: print("No answer") else: print(ans1) else: print(ans1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) while t > 0: s = input() l1 = [] l2 = [] l3 = [] for i in range(len(s)): if ord(s[i]) % 2 == 0: l1.append(s[i]) else: l2.append(s[i]) l1.sort() l2.sort() l3 = l2 + l1 l1 = l1 + l2 flag = 0 for i in range(1, len(l1)): if abs(ord(l1[i]) - ord(l1[i - 1])) == 1: flag = 1 else: continue flag1 = 0 for i in range(1, len(l3)): if abs(ord(l3[i]) - ord(l3[i - 1])) == 1: flag1 = 1 else: continue s1 = "" if flag == 1 and flag1 == 1: print("No answer") elif flag == 0: for i in range(len(l1)): s1 += l1[i] print(s1) else: for i in range(len(l3)): s1 += l3[i] print(s1) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
from sys import stdin inp = lambda: stdin.readline().strip() t = int(inp()) for _ in range(t): s = inp() alpha = [0] * 26 for i in s: alpha[ord(i) - 97] += 1 current = 0 flag = False for i in range(26): ans = [] ansLen = 0 if alpha[i] > 0: current = i alphaC = alpha[:] while True: ans.append(f"{chr(97 + current)}" * alphaC[current]) ansLen += alphaC[current] alphaC[current] = 0 if ansLen == len(s): print("".join(ans)) flag = True break for i in range(26): if i == current - 1 or i == current + 1: continue elif alphaC[i] > 0: current = i break else: break if flag: break else: print("No answer")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = [0] * 26 for i in input(): s[ord(i) - 97] += 1 n = 26 - s.count(0) l = sum(s) def chkMask(mask, n, s, l): p = 0 for i in range(26): if s[i] > 0: for j in range(n): if mask[j] == p: mask[j] = -i p += 1 rs = "" for i in mask: c = -i rs += "".join(chr(c + 97) for _ in range(s[c])) for i in range(1, l): if abs(ord(rs[i]) - ord(rs[i - 1])) == 1: return "No answer" return rs if n == 1: print(chkMask([0], n, s, l)) elif n == 2: print(chkMask([0, 1], n, s, l)) elif n == 3: s1 = chkMask([1, 0, 2], n, s, l) s2 = chkMask([0, 2, 1], n, s, l) if s1 != "No answer": print(s1) elif s2 != "No answer": print(s2) else: print(s1) elif n == 4: print(chkMask([2, 0, 3, 1], n, s, l)) else: mask = [2, 0, 3, 1] tmp = 4 while tmp < n: if tmp & 1: mask = [tmp] + mask else: mask = mask + [tmp] tmp += 1 print(chkMask(mask, n, s, l))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN STRING RETURN VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
alpha = list("abcdefghijklmnopqrstuvwxyz") d = {alpha[i]: i for i in range(26)} def isok(string): L = len(string) if any(abs(d[string[i]] - d[string[i + 1]]) == 1 for i in range(L - 1)): return False return True def solve(): s = list(input()) odd = [] even = [] for i, v in enumerate(s): if d[v] % 2 == 0: even.append(v) else: odd.append(v) odd.sort() even.sort() ans1 = "".join(odd + even) ans2 = "".join(even + odd) if isok(ans1): print(ans1) return if isok(ans2): print(ans2) return print("No answer") return def main(): t = int(input()) for i in range(t): solve() return main()
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
import sys def unique(lst): return dict((o, o) for o in lst).values() class Main: def __init__(self): self.buff = None self.index = 0 def next(self): if self.buff is None or self.index == len(self.buff): self.buff = self.next_line() self.index = 0 val = self.buff[self.index] self.index += 1 return val def next_line(self, _map=str): return list(map(_map, sys.stdin.readline().split())) def next_int(self): return int(self.next()) def solve(self): t = self.next_int() for _ in range(0, t): s = sorted([x for x in self.next()]) us = sorted(unique(s)) if len(us) == 1: print("".join(s)) elif len(us) == 2: if ord(us[0]) + 1 == ord(us[1]): print("No answer") else: print("".join(s)) elif len(us) == 3: if ord(us[0]) + 1 == ord(us[1]) and ord(us[0]) + 2 == ord(us[2]): print("No answer") elif ord(us[1]) + 1 != ord(us[2]): print( "".join( [us[0]] * s.count(us[0]) + [us[2]] * s.count(us[2]) + [us[1]] * s.count(us[1]) ) ) elif ord(us[0]) + 1 != ord(us[1]): print( "".join( [us[1]] * s.count(us[1]) + [us[0]] * s.count(us[0]) + [us[2]] * s.count(us[2]) ) ) else: rr = ( [us[1]] * s.count(us[1]) + [us[3]] * s.count(us[3]) + [us[0]] * s.count(us[0]) + [us[2]] * s.count(us[2]) ) for i in range(4, len(us)): if i % 2 == 0: rr = rr + [us[i]] * s.count(us[i]) else: rr = [us[i]] * s.count(us[i]) + rr print("".join(rr)) Main().solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP LIST VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = input() x = list(map(lambda t: ord(t) - ord("a"), list(s))) if len(set([(i % 2) for i in x])) == 1: print(s) continue odd = [i for i in x if i % 2 == 0] even = [i for i in x if i % 2 == 1] odd.sort() even.sort() if abs(odd[-1] - even[0]) > 1: ans = odd + even elif abs(odd[0] - even[-1]) > 1: ans = odd[::-1] + even[::-1] else: print("No answer") continue print("".join([chr(i + ord("a")) for i in ans]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for _ in range(t): s = input() s1 = s2 = "" for i in s: if ord(i) % 2 == 0: s1 += i else: s2 += i s1 = sorted(s1) s2 = sorted(s2) if len(s1) == 0: print("".join(s2)) elif len(s2) == 0: print("".join(s1)) elif abs(ord(s1[-1]) - ord(s2[0])) == 1 and abs(ord(s1[0]) - ord(s2[-1])) == 1: print("No answer") elif abs(ord(s1[-1]) - ord(s2[0])) == 1: print("".join(s2 + s1)) else: print("".join(s1 + s2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
numQueries = int(input()) inputArray = [] for i in range(numQueries): inputArray.append(input()) def f(ordNumber, repetition): return chr(ordNumber + 96) * repetition def resultOfQuery(string): numberOfRepitition = {} for c in string: ordC = ord(c) - 96 if ordC in numberOfRepitition: numberOfRepitition[ordC] += 1 else: numberOfRepitition[ordC] = 1 characters = [] for c in numberOfRepitition: characters.append(c) characters = sorted(characters) if len(characters) == 2: if abs(characters[0] - characters[1]) == 1: return "No answer" else: return f(characters[0], numberOfRepitition[characters[0]]) + f( characters[1], numberOfRepitition[characters[1]] ) elif len(characters) == 3: if characters[2] - characters[0] == 2: return "No answer" elif characters[2] - characters[1] == 1: return ( f(characters[2], numberOfRepitition[characters[2]]) + f(characters[0], numberOfRepitition[characters[0]]) + f(characters[1], numberOfRepitition[characters[1]]) ) else: return ( f(characters[0], numberOfRepitition[characters[0]]) + f(characters[2], numberOfRepitition[characters[2]]) + f(characters[1], numberOfRepitition[characters[1]]) ) else: length = len(characters) if len(characters) == 4: if characters[2] - characters[1] == 1: return ( f(characters[2], numberOfRepitition[characters[2]]) + f(characters[0], numberOfRepitition[characters[0]]) + f(characters[3], numberOfRepitition[characters[3]]) + f(characters[1], numberOfRepitition[characters[1]]) ) if len(characters) % 2 != 0: result = f( characters[len(characters) - 1], numberOfRepitition[characters[len(characters) - 1]], ) characters = characters[: len(characters) - 1] else: result = "" if length != 5: for j in range(len(characters) // 2): result += f(characters[j], numberOfRepitition[characters[j]]) + f( characters[j + len(characters) // 2], numberOfRepitition[characters[j + len(characters) // 2]], ) else: result += ( f(characters[2], numberOfRepitition[characters[2]]) + f(characters[0], numberOfRepitition[characters[0]]) + f(characters[3], numberOfRepitition[characters[3]]) + f(characters[1], numberOfRepitition[characters[1]]) ) return result for string in inputArray: print(resultOfQuery(string))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN STRING RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN STRING IF BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) si = lambda: input() n = ii() o = [] c = 0 def check(s1, s2): ss = s1 + s2 for i in range(len(ss)): if i != len(ss) - 1: if ord(ss[i]) + 1 == ord(ss[i + 1]) or ord(ss[i]) - 1 == ord(ss[i + 1]): return False return True for i in range(n): be = "" bo = "" flag = False ts = si() for j in range(len(ts)): if ord(ts[j]) % 2 == 0: be += ts[j] else: bo += ts[j] be = "".join(sorted(be)) bo = "".join(sorted(bo)) if check(be, bo): print(be + bo) elif check(bo, be): print(bo + be) elif check(bo[::-1], be): print(bo[::-1] + be) elif check(bo, be[::-1]): print(bo + be[::-1]) else: print("No answer") for i in o: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
testcase = int(input()) for i in range(testcase): string = [int(ord(i)) for i in input()] string.sort() even = [] odd = [] for i in string: if (i - 96) % 2 == 1: odd.append(i) else: even.append(i) if len(odd) == 0: print("".join(map(chr, even))) elif len(even) == 0: print("".join(map(chr, odd))) elif abs(even[len(even) - 1] - odd[0]) != 1: print("".join(map(chr, even + odd))) elif abs(odd[len(odd) - 1] - even[0]) != 1: print("".join(map(chr, odd + even))) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(odd, even): count = 0 arr = odd + even for i in range(len(arr) - 1): if abs(ord(arr[i]) - ord(arr[i + 1])) == 1: count += 1 return count q = int(input()) for i in range(q): arr1 = [] arr2 = [] a = input() for h in a: if ord(h) % 2 == 0: arr1.append(h) else: arr2.append(h) arr1.sort() arr2.sort() a1 = check(arr1, arr2) a2 = check(arr2, arr1) if a1 == 0: print("".join(arr1 + arr2)) elif a2 == 0: print("".join(arr2 + arr1)) else: print("No answer")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = sorted(input()) a = [] b = [] for e in s: w = ord(e) if w % 2: a += [w] else: b += [w] f = p = 0 for e in a + b: if abs(e - p) == 1: f |= 1 p = e p = 0 for e in b + a: if abs(e - p) == 1: f |= 2 p = e s = a + b if f == 2 else b + a r = "" if f == 3: print("No answer") else: print(*map(chr, s), sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR LIST VAR VAR LIST VAR ASSIGN VAR VAR NUMBER FOR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for case in range(int(input())): str = sorted(input()) x = "" y = "" for char in str: if ord(char) % 2 == 0: x += char else: y += char if len(y) == 0: print(x) continue if len(x) == 0: print(y) continue if ( abs(ord(x[len(x) - 1]) - ord(y[0])) == 1 and abs(ord(y[len(y) - 1]) - ord(x[0])) == 1 ): print("No answer") elif abs(ord(y[len(y) - 1]) - ord(x[0])) == 1: print(x + y) else: print(y + x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
T = int(input()) for i in range(T): s = input() d = {} for c in s: if c in d: d[c] += 1 else: d[c] = 1 a = list(d.keys()) a = sorted(a) if len(a) == 1: print(a[0] * d[a[0]]) continue if len(a) == 2: if ord(a[1]) - ord(a[0]) == 1: print("No answer") continue if len(a) == 3: if ord(a[1]) - ord(a[0]) == 1 and ord(a[2]) - ord(a[1]) == 1: print("No answer") continue if ord(a[1]) - ord(a[0]) == 1: print(a[0] * d[a[0]] + a[2] * d[a[2]] + a[1] * d[a[1]]) continue odd = "" even = "" for j in range(len(a)): if j % 2 == 0: even += a[j] * d[a[j]] else: odd += a[j] * d[a[j]] print(odd + even)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(s): for i in range(1, len(s)): if abs(ord(s[i]) - ord(s[i - 1])) == 1: return False return True def main(): t = int(input()) for _ in range(t): s = input() odd_str, even_str = [], [] for c in s: if ord(c) % 2: odd_str.append(c) else: even_str.append(c) odd_str.sort() even_str.sort() odd_str = "".join(odd_str) even_str = "".join(even_str) if check(odd_str + even_str): print(odd_str + even_str) elif check(even_str + odd_str): print(even_str + odd_str) else: print("No answer") main()
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def solve(s): odd = "" even = "" x = [] y = [] for char in s: if ord(char) % 2 == 1: x.append(char) else: y.append(char) x = sorted(x) y = sorted(y) x.reverse() y.reverse() odd = "".join(x) even = "".join(y) if len(x) == 0: return even if len(y) == 0: return odd if ( abs(ord(x[len(x) - 1]) - ord(y[0])) == 1 and abs(ord(y[len(y) - 1]) - ord(x[0])) == 1 ): return "No answer" elif abs(ord(y[len(y) - 1]) - ord(x[0])) == 1: return odd + even else: return even + odd n = int(input()) for i in range(n): print(solve(input()))
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN STRING IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) c = 26 for _ in range(t): s = list(input()) s.sort() n = len(s) u = [0] * c for i in range(n): u[ord(s[i]) - ord("a")] += 1 w = 0 d = [] p = [] for i in range(c): if u[i] != 0: w += 1 d.append(chr(i + ord("a"))) p.append(u[i]) if w > 4: for i in range(w // 2): r = (w - 1) // 2 + 1 print(d[i] * p[i] + d[i + r] * p[i + r], end="") if w % 2 == 1: print(d[w // 2] * p[w // 2], end="") print() elif w == 4: print(d[2] * p[2] + d[0] * p[0] + d[3] * p[3] + d[1] * p[1]) elif w == 1: print(d[0] * p[0]) elif w == 2: if abs(ord(d[0]) - ord(d[1])) == 1: print("No answer") else: print(d[0] * p[0] + d[1] * p[1]) elif ord(d[1]) - ord(d[0]) == 1 and ord(d[2]) - ord(d[1]) == 1: print("No answer") elif ord(d[1]) - ord(d[0]) > 1: print(d[2] * p[2] + d[0] * p[0] + d[1] * p[1]) else: print(d[0] * p[0] + d[2] * p[2] + d[1] * p[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(s): s = list(s) s.sort() a1 = [] a2 = [] c = 0 cur = s[0] for i in range(len(s)): if s[i] != cur: c ^= 1 cur = s[i] if c == 0: a1.append(s[i]) else: a2.append(s[i]) a = "".join(a1[::-1] + a2[::-1]) for i in range(1, len(a)): if abs(ord(a[i]) - ord(a[i - 1])) == 1: return 0 return a def check2(s): s = list(s) s.sort() a1 = [] a2 = [] c = 0 cur = s[0] for i in range(len(s)): if s[i] != cur: c ^= 1 cur = s[i] if c == 0: a1.append(s[i]) else: a2.append(s[i]) a = "".join(a1 + a2) for i in range(1, len(a)): if abs(ord(a[i]) - ord(a[i - 1])) == 1: return 0 return a t = int(input()) for i in range(t): s = input() if check(s): print(check(s)) elif check2(s): print(check2(s)) else: print("No answer")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for w in range(t): arr = [(0) for i in range(26)] s = input() l = len(s) for i in range(l): arr[ord(s[i]) - ord("a")] += 1 even = "" for i in range(26): if i % 2 == 0: even += chr(ord("a") + i) * arr[i] for i in range(26): if i % 2 == 1: even += chr(ord("a") + i) * arr[i] odd = "" for i in range(26): if i % 2 == 1: odd += chr(ord("a") + i) * arr[i] for i in range(26): if i % 2 == 0: odd += chr(ord("a") + i) * arr[i] flag1 = 0 for i in range(1, l): if abs(ord(even[i - 1]) - ord(even[i])) == 1: flag1 = 1 flag2 = 0 for i in range(1, l): if abs(ord(odd[i - 1]) - ord(odd[i])) == 1: flag2 = 1 if flag1 == 0: print(even) elif flag2 == 0: print(odd) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): s = input() a = [ch for ch in s if ord(ch) % 2 == 1] b = [ch for ch in s if ord(ch) % 2 == 0] a.sort() b.sort() if len(a) == 0 or len(b) == 0: print("".join(a + b)) else: ax = [a, a[::-1]] bx = [b, b[::-1]] done = 0 for x in ax: for y in bx: if abs(ord(x[-1]) - ord(y[0])) != 1 and done == 0: done = 1 print("".join(x + y)) if not done: print("No answer")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(str): for i in range(0, len(str) - 1): if ( ord(str[i]) + 1 == ord(str[i + 1]) or i > 0 and ord(str[i - 1]) == ord(str[i]) + 1 ): return False return ord(str[-2]) != ord(str[-1]) + 1 def solve_for(str): if len(str) == 1: return str odd, even = [], [] for ch in str: if ord(ch) % 2: odd.append(ch) else: even.append(ch) odd = "".join(sorted(odd)) even = "".join(sorted(even)) if check(odd + even): return odd + even elif check(even + odd): return even + odd return "No answer" cases = int(input()) for case in range(cases): str_in = input() print(solve_for(str_in))
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def position(a): if a == "a": return 1 if a == "b": return 2 if a == "c": return 3 if a == "d": return 4 if a == "e": return 5 if a == "f": return 6 if a == "g": return 7 if a == "h": return 8 if a == "i": return 9 if a == "j": return 10 if a == "k": return 11 if a == "l": return 12 if a == "m": return 13 if a == "n": return 14 if a == "o": return 15 if a == "p": return 16 if a == "q": return 17 if a == "r": return 18 if a == "s": return 19 if a == "t": return 20 if a == "u": return 21 if a == "v": return 22 if a == "w": return 23 if a == "x": return 24 if a == "y": return 25 if a == "z": return 26 trials = int(input()) for i in range(trials): sequence = list(input()) odds = [] evens = [] impossible = "" for j in sequence: if j in ["a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"]: odds.append(j) if j in ["b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"]: evens.append(j) odds_up = sorted(odds) evens_up = sorted(evens) final_1 = odds_up + evens_up final_2 = evens_up + odds_up for j in range(len(final_1) - 1): if abs(position(final_1[j]) - position(final_1[j + 1])) == 1: impossible = "True" if impossible == "": print("".join(final_1)) else: impossible = "" for j in range(len(final_2) - 1): if abs(position(final_2[j]) - position(final_2[j + 1])) == 1: impossible = "True" if impossible == "": print("".join(final_2)) else: print("No answer")
FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR IF VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): s = list(input().rstrip()) n = len(s) a = [] b = [] for i in range(n): if ord(s[i]) % 2 == 1: a.append(s[i]) else: b.append(s[i]) a.sort() b.sort() if len(a) == 0 or len(b) == 0: print("".join(a + b)) continue if not abs(ord(a[-1]) - ord(b[0])) == 1: print("".join(a + b)) continue if not abs(ord(b[-1]) - ord(a[0])) == 1: print("".join(b + a)) continue print("No answer")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) for _ in range(n): s = input() ans = [] thing = {} for i in s: ans.append(i) ans.sort() chars = [] for i in ans: if i in thing: thing[i] += 1 else: thing[i] = 1 chars.append(i) if len(thing) == 2 and ord(ans[0]) == ord(ans[-1]) - 1: print("No answer") continue if len(thing) == 3 and ord(ans[0]) == ord(ans[-1]) - 2: print("No answer") continue ans2 = [] if len(thing) == 3 and ord(chars[0]) == ord(chars[1]) - 1: for i in range(thing[chars[0]]): ans2.append(chars[0]) for i in range(thing[chars[2]]): ans2.append(chars[2]) for i in range(thing[chars[1]]): ans2.append(chars[1]) elif len(thing) == 3 and ord(chars[1]) == ord(chars[2]) - 1: for i in range(thing[chars[1]]): ans2.append(chars[1]) for i in range(thing[chars[0]]): ans2.append(chars[0]) for i in range(thing[chars[2]]): ans2.append(chars[2]) elif len(thing) == 4: for i in range(thing[chars[2]]): ans2.append(chars[2]) for i in range(thing[chars[0]]): ans2.append(chars[0]) for i in range(thing[chars[3]]): ans2.append(chars[3]) for i in range(thing[chars[1]]): ans2.append(chars[1]) else: for i in range(0, len(thing), 2): for j in range(thing[chars[i]]): ans2.append(chars[i]) for i in range(0, len(thing) - 1, 2): for j in range(thing[chars[i + 1]]): ans2.append(chars[i + 1]) print("".join(ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def func(a): l = len(a) odd = [] even = [] for i in range(l): x = ord(a[i]) - 65 if x % 2 == 0: even.append(a[i]) else: odd.append(a[i]) even.sort() odd.sort() a1 = "".join(even) + "".join(odd) a2 = "".join(odd) + "".join(even) flag = 0 for i in range(l - 1): x = ord(a1[i]) - 97 y = ord(a1[i + 1]) - 97 if abs(y - x) == 1: flag = 1 if flag == 0: return a1 else: for i in range(l - 1): x = ord(a2[i]) - 97 y = ord(a2[i + 1]) - 97 if abs(y - x) == 1: return "No answer" return a2 t = int(input()) for i in range(t): a = str(input()) print(func(a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for i in range(t): s = input() n = len(s) d = dict() for j in range(26): d[j] = "" for j in range(n): d[ord(s[j]) - ord("a")] += s[j] s1, s2 = "", "" for j in range(0, 26, 2): s1 += d[j] for j in range(1, 26, 2): s2 += d[j] if s1 == "" or s2 == "": print(s1 + s2) continue if abs(ord(s1[-1]) - ord(s2[0])) != 1: print(s1 + s2) elif abs(ord(s2[-1]) - ord(s1[0])) != 1: print(s2 + s1) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
q = int(input()) for _ in range(q): word = input() dic = {} for l in word: if l in dic: dic[l] += 1 else: dic[l] = 1 if len(dic) == 1: print(word) elif len(dic) == 2: k = list(dic.keys()) k.sort() if ord(k[0]) + 1 == ord(k[1]): print("No answer") else: print(word) elif len(dic) == 3: k = list(dic.keys()) k.sort() if ord(k[0]) + 1 == ord(k[1]) and ord(k[1]) + 1 == ord(k[2]): print("No answer") elif ord(k[0]) + 1 == ord(k[1]): print(k[0] * dic[k[0]] + k[2] * dic[k[2]] + k[1] * dic[k[1]]) else: print(k[2] * dic[k[2]] + k[0] * dic[k[0]] + k[1] * dic[k[1]]) else: l = len(dic) w = l - l % 2 ans = "" k = list(dic.keys()) k.sort() for i in range(l // 2): ans += k[l // 2 + i] * dic[k[l // 2 + i]] ans += k[i] * dic[k[i]] if l & 1: ans += k[w] * dic[k[w]] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
from itertools import groupby IMP = "No answer" for _ in range(int(input())): s = input() s1 = ["".join(x[1]) for x in groupby(sorted(s), lambda c: c)] if len(s1) >= 4: print("".join(s1[1::2] + s1[::2])) elif len(s1) == 1: print(*s1) else: s2 = [ord(c1[0]) for c1 in s1] if s2 != [(s2[0] + i) for i in range(len(s2))]: if len(s1) == 2: print(s) else: c2, c3, c4 = s1 c5, c6, c7 = s2 if c5 + 1 == c6: print(c2 + c4 + c3) else: print(c3 + c2 + c4) else: print(IMP)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for i in range(t): string = input() odd = [] even = [] a = ord("a") for j in range(len(string)): if (ord(string[j]) - a) % 2 == 0: even.append(string[j]) else: odd.append(string[j]) even = sorted(even) odd = sorted(odd) if len(even) == 0: s = "" for j in range(len(odd)): s = s + odd[j] print(s) elif len(odd) == 0: s = "" for j in range(len(even)): s = s + even[j] print(s) elif abs(ord(even[0]) - ord(odd[-1])) != 1: s = "" for j in range(len(odd)): s = s + odd[j] for j in range(len(even)): s = s + even[j] print(s) elif abs(ord(even[-1]) - ord(odd[0])) != 1: s = "" for j in range(len(even)): s = s + even[j] for j in range(len(odd)): s = s + odd[j] print(s) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def valid(s): n = len(s) if n == 1: return True for i in range(1, n): if abs(ord(s[i]) - ord(s[i - 1])) == 1: return False return True def solve(): result = [] for _ in range(int(input())): s = sorted(input()) half1 = [] half2 = [] turn = 1 i = 0 while i < len(s): j = i while j < len(s) and s[j] == s[i]: j += 1 if j == i: j += 1 if turn == 1: half1 += s[i:j] turn = 2 else: half2 += s[i:j] turn = 1 i += j - i ans = "No answer" if not half2: if valid(half2): ans = "".join(half1) elif valid(half1) and valid(half2): if abs(ord(half1[-1]) - ord(half2[0])) != 1: ans = "".join(half1 + half2) elif abs(ord(half1[0]) - ord(half2[-1])) != 1: ans = "".join(half2 + half1) result.append(ans) print("\n".join(result)) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR STRING IF VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check_condition(string): flag = 0 for i in range(len(string) - 1): diff = abs(ord(string[i]) - ord(string[i + 1])) if diff == 1: flag = 1 break return flag t = int(input()) for i in range(t): even = "" odd = "" string = input() for c in string: if ord(c) % 2 == 0: even = even + c else: odd = odd + c even = "".join(sorted(even)) odd = "".join(sorted(odd)) ans1 = even + odd ans2 = odd + even flag1 = check_condition(ans1) flag2 = check_condition(ans2) if flag1 == 0: print(ans1) elif flag2 == 0: print(ans2) else: print("No answer")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
import itertools t = int(input()) for tt in range(t): s = input() h = {} for c in s: if c in h: h[c] += 1 else: h[c] = 1 res = "" ss = sorted(list(set(s))) l = ss[0 : len(ss) // 2] r = ss[len(ss) // 2 : len(ss)] l_c = 0 r_c = 0 if len(ss) == 2 and ord(ss[0]) + 1 == ord(ss[1]): print("No answer") continue if len(ss) == 3 and ord(l[0]) + 1 == ord(r[0]): if ord(r[0]) + 1 == ord(r[1]): print("No answer") continue else: print(r[0] * h[r[0]] + r[1] * h[r[1]] + l[0] * h[l[0]]) continue while True: ok = False if r_c < len(r): res += h[r[r_c]] * r[r_c] r_c += 1 ok = True if l_c < len(l): res += h[l[l_c]] * l[l_c] l_c += 1 ok = True if not ok: break print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
abc = "abcdefghijklmnopqrstuvwxyz" abc2 = abc zyx2 = abc2[::-1] t = int(input()) for query in range(t): s = input() odd = [letter for letter in s if ord(letter) % 2 == 1] even = [letter for letter in s if ord(letter) % 2 == 0] if len(odd) == 0 or len(even) == 0: print(s) else: odd.sort() even.sort() conect1 = odd[-1] + even[0] conect2 = even[-1] + odd[0] if conect1 not in abc2 and conect1 not in zyx2: print("".join(odd + even)) elif conect2 not in abc2 and conect2 not in zyx2: print("".join(even + odd)) else: print("No answer")
ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
from itertools import groupby def solve(ltr): if len(ltr) == 1: print(ltr[0][0] * ltr[0][1]) elif len(ltr) == 2: if ord(ltr[1][0]) == ord(ltr[0][0]) + 1: print("No answer") else: print(ltr[0][0] * ltr[0][1], end="") print(ltr[1][0] * ltr[1][1]) elif len(ltr) == 3: if ord(ltr[1][0]) != ord(ltr[0][0]) + 1: print(ltr[1][0] * ltr[1][1], end="") print(ltr[0][0] * ltr[0][1], end="") print(ltr[2][0] * ltr[2][1]) elif ord(ltr[2][0]) != ord(ltr[1][0]) + 1: print(ltr[1][0] * ltr[1][1], end="") print(ltr[2][0] * ltr[2][1], end="") print(ltr[0][0] * ltr[0][1]) else: print("No answer") elif len(ltr) == 4: print(ltr[2][0] * ltr[2][1], end="") print(ltr[0][0] * ltr[0][1], end="") print(ltr[3][0] * ltr[3][1], end="") print(ltr[1][0] * ltr[1][1]) else: half = (len(ltr) + 1) // 2 for i in range(len(ltr)): if i % 2 == 0: print(ltr[i // 2][0] * ltr[i // 2][1], end="") else: print(ltr[half + i // 2][0] * ltr[half + i // 2][1], end="") print() T = int(input()) for t in range(T): S = sorted(input().strip()) ltr = [] for c in S: if ltr and c == ltr[-1][0]: ltr[-1][1] += 1 else: ltr.append([c, 1]) solve(ltr)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def construct(uniq): cst = [] for i in range(1, len(uniq), 2): cst.append(uniq[i]) for i in range(0, len(uniq), 2): cst.append(uniq[i]) return cst T = int(input()) for t in range(T): s = input() uniq = [] dc = {} for c in s: num = ord(c) if ord(c) in dc: dc[num] += 1 else: dc[num] = 1 uniq.append(num) uniq.sort() cst = construct(uniq) uniq.reverse() cst_rev = construct(uniq) ans = "No answer" i = 1 while i < len(cst): if abs(cst[i] - cst[i - 1]) == 1: break i += 1 if i == len(cst): ans = "cst" i = 1 while i < len(cst_rev): if abs(cst_rev[i] - cst_rev[i - 1]) == 1: break i += 1 if i == len(cst_rev): ans = "cst_rev" if ans != "No answer": if ans == "cst": temp = cst else: temp = cst_rev ans = "" for i in temp: for j in range(dc[i]): ans += chr(i) print(ans)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) def good(s): for i in range(1, len(s)): if abs(ord(s[i]) - ord(s[i - 1])) == 1: return False return True for i in range(t): s = input() count = {} for c in s: if c not in count: count[c] = 0 count[c] += 1 out = "" alpha = "abcdefghijklmnopqrstuvwxyz" a1 = alpha[::2] + alpha[1::2] a2 = alpha[1::2] + alpha[::2] s1 = "" s2 = "" for c in a1: if c in count: s1 += count[c] * c for c in a2: if c in count: s2 += count[c] * c if good(s1): print(s1) elif good(s2): print(s2) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for _ in range(t): s = input() d = {} for c in s: try: d[c] += 1 except: d[c] = 1 i = "a" an = "" while i <= "z": try: if d[i] > 0: for j in range(d[i]): an += i except: pass i = chr(ord(i) + 2) i = "b" ans2 = "" while i <= "z": try: if d[i] > 0: for j in range(d[i]): ans2 += i except: pass i = chr(ord(i) + 2) f = 2 ans = an + ans2 for i in range(1, len(ans)): if abs(ord(ans[i]) - ord(ans[i - 1])) == 1: f -= 1 break if f == 2: print(ans) continue ans = ans2 + an for i in range(1, len(ans)): if abs(ord(ans[i]) - ord(ans[i - 1])) == 1: f -= 1 break if f == 1: print(ans) continue print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR STRING IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR STRING IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def solve(s): cs = [x for x in s] cs.sort() r1, r2 = "", "" alpha1 = [chr(x) for x in range(ord("a"), ord("z") + 2, 2)] alpha2 = [chr(x) for x in range(ord("b"), ord("z") + 2, 2)] for c in alpha1: if s.count(c) != 0: r1 += str(c) * s.count(c) for c in alpha2: if s.count(c) != 0: r2 += str(c) * s.count(c) if len(r1) == 0: return r2 if len(r2) == 0: return r1 if abs(ord(r1[-1]) - ord(r2[0])) != 1: return r1 + r2 if abs(ord(r2[-1]) - ord(r1[0])) != 1: return r2 + r1 return "No answer" T = int(input()) for _ in range(T): s = input() print(solve(s))
FUNC_DEF ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) while t > 0: t = t - 1 A = input() st1 = "" st2 = "" arr = [0] * 26 for i in range(len(A)): ind = ord(A[i]) - ord("a") arr[ind] += 1 for i in range(0, 26, 2): st1 += chr(ord("a") + i) * arr[i] for i in range(1, 26, 2): st2 += chr(ord("a") + i) * arr[i] if len(st1) == 0: print(st2) elif len(st2) == 0: print(st1) elif abs(ord(st1[-1]) - ord(st2[0])) != 1: print(st1 + st2) elif abs(ord(st2[-1]) - ord(st1[0])) != 1: print(st2 + st1) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
alp = list("abcdefghijklmnopqrstuvwxyz") t = int(input()) for i in range(t): s = list(input()) dic = {} for item in s: if item not in dic: dic[item] = 1 else: dic[item] += 1 tot = [0] * len(s) tot = [] for j in range(0, 26, 2): if alp[j] in dic: for x in range(dic[alp[j]]): tot.append(alp[j]) if len(tot) > 0: index1 = alp.index(tot[-1]) index2 = alp.index(tot[0]) else: index1 = index2 = -2 tot2 = [] for j in range(1, 26, 2): if alp[j] in dic: for x in range(dic[alp[j]]): tot2.append(alp[j]) if len(tot2) > 0: index3 = alp.index(tot2[0]) index4 = alp.index(tot2[-1]) else: index3 = index4 = -4 if abs(index1 - index3) != 1: print(*(tot + tot2), sep="") elif abs(index2 - index4) != 1: print(*(tot2 + tot), sep="") else: print("No answer")
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(s): return all( ord(s[i]) != ord(s[i - 1]) + 1 and ord(s[i]) != ord(s[i - 1]) - 1 for i in range(1, len(s)) ) for _ in range(int(input())): s = input() n = len(s) s = sorted(s) s1 = "" s2 = "" for i in s: if ord(i) - 97 & 1: s1 += i else: s2 += i if check(s1 + s2): print(s1 + s2) elif check(s2 + s1): print(s2 + s1) else: print("No answer")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def do(s): if len(s) == 1: print(s[0]) return start = 0 while start < len(s): if start == len(s) - 2: break if abs(ord(s[start + 1]) - ord(s[start])) == 1: start += 1 toreplace = len(s) - 1 s.insert(start, s[toreplace]) del s[len(s) - 1] else: start += 1 x = len(s) for i in range(len(s) - 1, 0, -1): if abs(ord(s[i - 1]) - ord(s[i])) == 1: x = i if x < len(s): while abs(ord(s[x - 1]) - ord(s[x])) <= 1: s.insert(0, s[len(s) - 1]) del s[len(s) - 1] x += 1 if x == len(s): break for i in range(len(s) - 1): if abs(ord(s[i + 1]) - ord(s[i])) == 1: print("No answer") return print("".join(s)) n = int(input()) for i in range(n): s = sorted(list(input())) do(s)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
T = int(input()) for i in range(0, T): s = input() temp = 0 pos = [0] * 26 for j in range(0, len(s)): pos[ord(s[j]) - 97] = pos[ord(s[j]) - 97] + 1 st = "" L1 = [] L2 = [] for j in range(0, len(pos), 2): if pos[j] != 0: L1.append((chr(j + 97), pos[j])) for j in range(1, len(pos), 2): if pos[j] != 0: L2.append((chr(j + 97), pos[j])) ptr1 = -1 ptr2 = -1 flag = 1 for j in range(0, len(L1)): for k in range(0, len(L2)): if abs(ord(L1[j][0]) - ord(L2[k][0])) != 1: ptr1 = j ptr2 = k flag = 0 break if flag == 1 and len(L1) != 0 and len(L2) != 0: print("No answer") elif len(L1) == 0: for j in range(0, len(L2)): st = st + L2[j][0] * L2[j][1] print(st) elif len(L2) == 0: for j in range(0, len(L1)): st = st + L1[j][0] * L1[j][1] print(st) else: for j in range(0, len(L1)): if j != ptr1: st = st + L1[j][0] * L1[j][1] st = st + L1[ptr1][0] * L1[ptr1][1] st = st + L2[ptr2][0] * L2[ptr2][1] for j in range(0, len(L2)): if j != ptr2: st = st + L2[j][0] * L2[j][1] for j in range(1, len(st)): if abs(ord(st[j]) - ord(st[j - 1])) == 1: temp = 1 break if temp == 1: print("No answer") else: print(st)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for i in range(t): s = input() ch = [] nec = [] for i in range(len(s)): if (ord(s[i]) - ord("a")) % 2 == 0: ch.append(ord(s[i]) - ord("a")) else: nec.append(ord(s[i]) - ord("a")) ch.sort() nec.sort() a = len(ch) b = len(nec) ans = "" if a == 0 or b == 0: print(s) continue if ( abs(ch[a - 1] - nec[b - 1]) == 1 and abs(ch[0] - nec[b - 1]) == 1 and abs(ch[a - 1] - nec[0]) == 1 and abs(ch[0] - nec[0]) == 1 ): print("No answer") elif abs(ch[a - 1] - nec[b - 1]) != 1: for i in range(a): ans += chr(ch[i] + ord("a")) for i in range(b - 1, -1, -1): ans += chr(nec[i] + ord("a")) print(ans) elif abs(ch[0] - nec[0]) != 1: for i in range(a - 1, -1, -1): ans += chr(ch[i] + ord("a")) for i in range(b): ans += chr(nec[i] + ord("a")) print(ans) elif abs(ch[a - 1] - nec[0]) != 1: for i in range(a): ans += chr(ch[i] + ord("a")) for i in range(b): ans += chr(nec[i] + ord("a")) print(ans) else: for i in range(b): ans += chr(nec[i] + ord("a")) for i in range(a): ans += chr(ch[i] + ord("a")) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for test in range(t): S = input() L = list(S) Noanswer = 0 L1 = [ord(k) for k in L] D = [[x, L1.count(x)] for x in set(L1)] D.sort(key=lambda x: x[0]) if len(D) > 4: Dleft = [] Dright = [] for k in range(len(D)): if k % 2 == 0: Dleft.append(D[k]) else: Dright.append(D[k]) D1 = Dleft + Dright elif len(D) == 4: D1 = [D[2], D[0], D[3], D[1]] elif len(D) == 3: if D[2][0] - D[1][0] == 1 and D[1][0] - D[0][0] == 1: Noanswer = 1 elif D[1][0] - D[0][0] == 1: D1 = [D[0], D[2], D[1]] else: D1 = [D[1], D[0], D[2]] elif len(D) == 2: if D[1][0] - D[0][0] == 1: Noanswer = 1 else: D1 = D elif len(D) == 1: D1 = D if Noanswer == 0: H = "" for k in D1: for j in range(k[1]): H = H + chr(k[0]) print(H) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) def ok(a, b): if abs(ord(a[len(a) - 1]) - ord(b[0])) == 1: return False else: return True for i in range(n): s = input() par = [] nepar = [] for j in s: if ord(j) % 2 == 0: par.append(j) else: nepar.append(j) par.sort() nepar.sort() if len(par) > 0 and len(nepar) > 0: if ok(par, nepar): for i in par: print(i, end="") for i in nepar: print(i, end="") print() elif ok(nepar, par): for i in nepar: print(i, end="") for i in par: print(i, end="") print() else: print("No answer") else: for i in max(par, nepar): print(i, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
t = int(input()) for i in range(t): x = list(input()) y = {f: x.count(f) for f in x} z = sorted(y.keys()) n = len(z) rer = [""] * n if n == 3: rer = z j = 0 k = 1 if abs(ord(z[1]) - ord(z[2])) != 1: j = 1 k = 2 aide = rer[j] rer[j] = rer[k] rer[k] = aide else: j = 0 k = 1 while k < n: rer[k] = z[j] j += 1 k += 2 j = n - 1 k = n - 2 + n % 2 while k >= 0 and j >= 0: rer[k] = z[j] j -= 1 k -= 2 yes = True m = len(x) out = [""] * m kk = 0 for j in range(n): for k in range(y[rer[j]]): out[kk] = rer[j] kk += 1 for j in range(1, m): if abs(ord(out[j]) - ord(out[j - 1])) == 1: print("No answer") yes = False break if yes == True: print("".join(out))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) for _ in range(n): word = sorted(input()) odd_part, even_part = "", "" for i in word: if ord(i) % 2 == 0: even_part += i else: odd_part += i previous = "0" output = 0 for i in odd_part + even_part: if abs(ord(i) - ord(previous)) == 1: output |= 1 previous = i previous = "0" for i in even_part + odd_part: if abs(ord(i) - ord(previous)) == 1: output |= 2 previous = i if output == 3: print("No answer") elif output == 1: print(even_part + odd_part) else: print(odd_part + even_part)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
dic = {} te = "abcdefghijklmnopqrstuvwxyz" for i in range(1, 27): dic[te[i - 1]] = i for _ in range(int(input())): st = input() dicount = {} for i in st: dicount[i] = dicount.get(i, 0) + 1 ke = list(dicount.keys()) ke.sort() le = len(ke) if le == 1: print(ke[0] * dicount[ke[0]]) elif le == 2: if dic[ke[1]] - dic[ke[0]] == 1: print("No answer") else: print(ke[0] * dicount[ke[0]] + ke[1] * dicount[ke[1]]) elif le == 3: if dic[ke[2]] - dic[ke[1]] == 1 and dic[ke[1]] - dic[ke[0]] == 1: print("No answer") elif dic[ke[2]] - dic[ke[1]] == 1 and dic[ke[1]] - dic[ke[0]] != 1: print( ke[1] * dicount[ke[1]] + ke[0] * dicount[ke[0]] + ke[2] * dicount[ke[2]] ) else: print( ke[0] * dicount[ke[0]] + ke[2] * dicount[ke[2]] + ke[1] * dicount[ke[1]] ) else: ans = ( ke[1] * dicount[ke[1]] + ke[3] * dicount[ke[3]] + ke[0] * dicount[ke[0]] + ke[2] * dicount[ke[2]] ) for i in range(4, le): if i % 2 == 0: ans += ke[i] * dicount[ke[i]] else: ans = ke[i] * dicount[ke[i]] + ans print(ans)
ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
for _ in range(int(input())): no_ans = False ans = [] s = input() c = list(sorted(set(s))) if len(c) == 1: ans = [c[0]] elif len(c) == 2: if ord(c[1]) - ord(c[0]) == 1: no_ans = True else: ans = [c[0], c[1]] elif len(c) == 3: if ord(c[2]) - ord(c[0]) == 2: no_ans = True elif ord(c[1]) - ord(c[0]) > 1: ans = [c[2], c[0], c[1]] else: ans = [c[0], c[2], c[1]] else: ans = list(reversed(c[0:-1:2])) + [c[-1]] + c[1:-1:2] res = "".join([(ch * s.count(ch)) for ch in ans]) print("No answer" if no_ans else res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER LIST VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
from itertools import groupby as gb Q = int(input()) for _ in range(Q): s = input() g = gb(sorted(list(s))) l = [0] * 26 for k, v in g: l[ord(k) - ord("a")] = len(list(v)) p, q = "", "" for i, j in enumerate(l[1::2]): p += chr(ord("a") + i * 2 + 1) * j for i, j in enumerate(l[::2]): q += chr(ord("a") + i * 2) * j s1 = p + q s2 = q + p for i, j in zip(s1, s1[1:]): if abs(ord(i) - ord(j)) == 1: break else: print(s1) continue for i, j in zip(s2, s2[1:]): if abs(ord(i) - ord(j)) == 1: break else: print(s2) continue print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
import sys n = int(input()) lines = sys.stdin.readlines() res = [] for line in lines: line = line.strip() odd = dict() even = dict() s = [] fail = False for c in line: if (ord(c) - ord("a")) % 2 == 0: if not even.get(c): even[c] = 0 even[c] += 1 else: if not odd.get(c): odd[c] = 0 odd[c] += 1 even_keys = sorted(list(even.keys())) odd_keys = sorted(list(odd.keys())) if len(even_keys) > 0 and len(odd_keys) > 0: bad = [ abs(ord(even_keys[-1]) - ord(odd_keys[0])) == 1, abs(ord(even_keys[-1]) - ord(odd_keys[-1])) == 1, abs(ord(even_keys[0]) - ord(odd_keys[0])) == 1, abs(ord(even_keys[0]) - ord(odd_keys[-1])) == 1, ] if bad[0] and bad[1] and not bad[2]: even_keys[0], even_keys[-1] = even_keys[-1], even_keys[0] elif bad[0] and bad[1] and not bad[3]: even_keys[0], even_keys[-1] = even_keys[-1], even_keys[0] odd_keys[0], odd_keys[-1] = odd_keys[-1], odd_keys[0] elif bad[0] and not bad[1]: odd_keys[0], odd_keys[-1] = odd_keys[-1], odd_keys[0] elif bad[0] and bad[1] and bad[2] and bad[3]: res.append("No answer") continue for k in even_keys: s.append(k * even[k]) for k in odd_keys: s.append(k * odd[k]) res.append("".join(s)) print("\n".join(res))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
from itertools import permutations n = int(input()) for _ in range(n): s = list(input()) x = list(set(s)) x.sort() if len(x) < 5: for p in permutations(x): for i, j in zip(p, p[1:]): if abs(ord(j) - ord(i)) == 1: break else: break else: print("No answer") continue else: p = x[::2] + x[1::2] for c in p: print(c * s.count(c), end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
import sys readline = sys.stdin.buffer.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): s = list(ns()) o = list() e = list() s.sort() for x in s: if x & 1: o.append(x) else: e.append(x) for g in (o + e, e + o): for x, y in zip(g, g[1:]): if abs(x - y) == 1: break else: print("".join(map(chr, g))) return print("No answer") return T = ni() for _ in range(T): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def check(s): for i in range(len(s) - 1): if abs(ord(s[i]) - ord(s[i + 1])) == 1: return 0 return 1 t = int(input()) for _ in range(t): o = list() e = list() s = input() for i in range(len(s)): if ord(s[i]) % 2 == 1: o.append(s[i]) else: e.append(s[i]) o.sort() e.sort() if check(o + e): print("".join(o) + "".join(e)) elif check(e + o): print("".join(e) + "".join(o)) else: print("No answer")
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
dic = {} tem = "abcdefghijklmnopqrstuvwxyz" for i in range(26): dic[tem[i]] = i for _ in range(int(input())): st = list(input()) fre = {} for i in st: if i in fre: fre[i] += 1 else: fre[i] = 1 se = set(st) se = list(se) se.sort() le = len(se) flag = True if le == 1: print("".join(st)) elif le == 2: if dic[se[1]] - dic[se[0]] == 1: print("No answer") else: print("".join(st)) else: if le == 3: if dic[se[1]] - dic[se[0]] == 1 and dic[se[2]] - dic[se[1]] == 1: print("No answer") flag = False else: ans = se[0] if dic[se[1]] - dic[se[0]] != 1: ans += se[1] ans = se[2] + ans else: ans = se[1] + se[2] + ans else: ans = se[1] + se[3] + se[0] + se[2] for i in range(4, le): if i % 2 != 0: ans += se[i] else: ans = se[i] + ans if flag: for i in ans: print(i * fre[i], end="") print("")
ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def getn(): return int(input()) def getns(): return [int(x) for x in input().split()] t = getn() for _ in range(t): mp = [[i, 0] for i in range(26)] s = input() for c in s: mp[ord(c) - ord("a")][1] += 1 mp = [x for x in mp if x[1] > 0] lt = [x[0] for x in mp if x[1] > 0] if len(mp) > 3: i = 1 while i < len(mp): print(chr(ord("a") + mp[i][0]) * mp[i][1], end="") i += 2 i = 0 while i < len(mp): print(chr(ord("a") + mp[i][0]) * mp[i][1], end="") i += 2 print() elif len(mp) == 3: if max(lt) - min(lt) == 2: print("No answer") else: if lt[1] - lt[0] == 1: mp = [mp[1], mp[2], mp[0]] else: mp = [mp[1], mp[0], mp[2]] print( chr(ord("a") + mp[0][0]) * mp[0][1] + chr(ord("a") + mp[1][0]) * mp[1][1] + chr(ord("a") + mp[2][0]) * mp[2][1] ) elif len(mp) == 2: if max(lt) - min(lt) == 1: print("No answer") else: print( chr(ord("a") + mp[0][0]) * mp[0][1] + chr(ord("a") + mp[1][0]) * mp[1][1] ) else: print(chr(ord("a") + mp[0][0]) * mp[0][1])
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER VAR VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) strings = [list(map(lambda e: ord(e), list(input()))) for _ in range(n)] def main(element): result = [] even = [] odd = [] for tt in sorted(element): if tt % 2 == 0: even.append(tt) else: odd.append(tt) if len(odd) == 0 or len(even) == 0: result.append(odd + even) elif abs(odd[-1] - even[0]) != 1: result.append(odd + even) elif abs(odd[0] - even[-1]) != 1: result.append(even + odd) else: print("No answer") return final = list(map(lambda e: chr(e), result[0])) print("".join(final)) for element in strings: main(element)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
q = int(input()) def check(s): for i in range(len(s) - 1): if abs(ord(s[i]) - ord(s[i + 1])) == 1: return False return True for _ in range(q): s = input().strip() evens = [] odds = [] for ch in s: if ord(ch) % 2 == 0: evens.append(ch) else: odds.append(ch) evens.sort() odds.sort() evens = "".join(evens) odds = "".join(odds) if check(evens + odds): print(evens + odds) elif check(odds + evens): print(odds + evens) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
N = int(input()) for i in range(N): maps = [0] * 26 A = input() for a in A: maps[ord(a) - ord("a")] += 1 L = [] for i, m in enumerate(maps): if m != 0: L += [[i + ord("a"), m]] ans = [] if len(L) > 4 or len(L) == 3 and abs(L[-1][0] - L[-2][0]) != 1: for i in range(0, len(L), 2): ans += [chr(L[i][0]) * L[i][1]] for i in range(1, len(L), 2): ans += [chr(L[i][0]) * L[i][1]] elif ( len(L) == 4 or len(L) == 3 and abs(L[0][0] - L[1][0]) != 1 or len(L) == 2 and abs(L[0][0] - L[1][0]) != 1 ): for i in range(1, len(L), 2): ans += [chr(L[i][0]) * L[i][1]] for i in range(0, len(L), 2): ans += [chr(L[i][0]) * L[i][1]] elif len(L) == 1: ans += chr(L[0][0]) * L[0][1] if ans != []: print("".join(ans)) else: print("No answer")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR LIST LIST BIN_OP VAR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR LIST BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR LIST BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR LIST BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR LIST BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR LIST EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) s = [input() for i in range(n)] alph = "abcdefghijklmnopqrstuvwxyz" to_int = {} to_char = {} for i in range(26): to_int[alph[i]] = i for i in range(n): memo = {} for j in s[i]: if to_int[j] not in memo: memo[to_int[j]] = 1 else: memo[to_int[j]] += 1 if len(memo) == 1: print(s[i]) elif len(memo) == 2: ans = [] for j in memo: ans.append(j) ans = sorted(ans) if ans[0] + 1 == ans[1]: print("No answer") else: print(s[i]) elif len(memo) == 3: ans = [] for j in memo: ans.append(j) ans = sorted(ans) if ans[0] + 1 == ans[1] and ans[1] + 1 == ans[2]: print("No answer") elif ans[0] + 1 != ans[1]: print( memo[ans[1]] * alph[ans[1]] + memo[ans[0]] * alph[ans[0]] + memo[ans[2]] * alph[ans[2]] ) else: print( memo[ans[0]] * alph[ans[0]] + memo[ans[2]] * alph[ans[2]] + memo[ans[1]] * alph[ans[1]] ) else: ans = [] for j in memo: ans.append(j) ans = sorted(ans) ans1 = ans[0 : len(ans) // 2] ans2 = ans[len(ans) // 2 :] tmp = [] for i in range(len(ans)): if i % 2 == 0: tmp.append(ans2[i // 2]) else: tmp.append(ans1[i // 2]) ans = [] for i in tmp: ans.append(memo[i] * alph[i]) print("".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def solve(): s = input() s = sorted(s) kol = [0] * 26 for i in s: kol[ord(i) - ord("a")] += 1 st = set() for i in s: st.add(ord(i) - ord("a")) v = list(st) v = sorted(v) if viv: print("Sorted", v) n = len(v) v2 = list() for i in range(1, n, 2): v2.append(v[i]) for i in range(0, n, 2): v2.append(v[i]) if len(v2) == 3: if v[0] + 1 == v[1]: v2 = [v[0], v[2], v[1]] else: v2 = [v[1], v[0], v[2]] if viv: print("Look at it:", v2) ans = "" for i in v2: ans += ("" + chr(ord("a") + i)) * kol[i] for i in range(len(ans) - 1): if abs(ord(ans[i]) - ord(ans[i + 1])) == 1: print("No answer") return print(ans) viv = False t = int(input()) while t: solve() t -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
a = int(input()) for i in range(a): s = input() lp = [] li = [] for c in s: if ord(c) % 2 == 0: lp.append(ord(c)) else: li.append(ord(c)) lp.sort() li.sort() linova = [] linova2 = [] for i in range(len(li)): linova.append(li[i]) for i in range(len(lp)): linova2.append(lp[i]) for i in range(len(li)): linova2.append(li[i]) for i in range(len(lp)): linova.append(lp[i]) flag = False flag2 = False for i in range(len(linova) - 1): if abs(linova[i] - linova[i + 1]) == 1: flag = True if abs(linova2[i] - linova2[i + 1]) == 1: flag2 = True if flag2 and flag: print("No answer") break else: if flag: for i in range(len(linova2)): print(chr(linova2[i]), end="") print() else: for i in range(len(linova)): print(chr(linova[i]), end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
n = int(input()) s1 = "" for i in range(n): s = input() s1 = "" for o in range(ord("b"), ord("z") + 1, 2): s1 = s1 + chr(o) * s.count(chr(o)) for o in range(ord("a"), ord("z") + 1, 2): s1 = s1 + chr(o) * s.count(chr(o)) good = True for o in range(ord("b"), ord("z") + 1): if s1.find(chr(o - 1) + chr(o)) > -1 or s1.find(chr(o) + chr(o - 1)) > -1: good = False if not good: s1 = "" for o in range(ord("a"), ord("z") + 1, 2): s1 = s1 + chr(o) * s.count(chr(o)) for o in range(ord("b"), ord("z") + 1, 2): s1 = s1 + chr(o) * s.count(chr(o)) good = True for o in range(ord("b"), ord("z") + 1): if s1.find(chr(o - 1) + chr(o)) > -1 or s1.find(chr(o) + chr(o - 1)) > -1: good = False if not good: s1 = "No answer" print(s1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
def isNotUgly(s): for _ in range(1, len(s)): if abs(ord(s[_]) - ord(s[_ - 1])) == 1: return False return True def solve(): s = input() even = [] odd = [] for _ in s: if ord(_) % 2 == 0: even.append(_) else: odd.append(_) even.sort() odd.sort() a = "".join(even) b = "".join(odd) fin1 = a + b fin2 = b + a if isNotUgly(fin1): print(fin1) elif isNotUgly(fin2): print(fin2) else: print("No answer") for __ in range(int(input())): solve()
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $(1, 2)$ β€” "ab" and $(2, 3)$ β€” "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $T$ separate queries. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 100$) β€” the number of queries. Each of the next $T$ lines contains string $s$ $(1 \le |s| \le 100)$ β€” the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $T = 1$. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th query. If the answer for the $i$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. -----Example----- Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer -----Note----- In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example.
class MyError(RuntimeError): pass n = int(input()) for i in range(n): s = input() odd = [] even = [] for ch in s: if ord(ch) % 2 == 0: even.append(ch) else: odd.append(ch) if len(odd) == 0 or len(even) == 0: print(s) else: u = -1 v = -1 try: for i in range(len(odd)): for j in range(len(even)): if abs(ord(odd[i]) - ord(even[j])) != 1: u, v = i, j raise MyError() print("No answer") except MyError: odd = odd[:u] + odd[u + 1 :] + odd[u : u + 1] even = even[v : v + 1] + even[:v] + even[v + 1 :] print("".join(odd + even))
CLASS_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR