description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
from sys import stdin input = stdin.readline n = int(input()) a = list(map(int, input().split())) for i in range(n): a[i] = [a[i], 0] was, change = 0, [] q = int(input()) for _ in range(q): l = list(map(int, input().split())) if l[0] == 1: a[l[1] - 1] = [l[2], was] else: change.append(l[1]) was += 1 arr = [-1] for i in range(len(change) - 1, -1, -1): arr.append(max(arr[-1], change[i])) arr = arr[::-1] print(*[max(v[0], arr[v[1]]) for v in a])
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER LIST VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
import sys input = sys.stdin.readline n = int(input()) a = [int(i) for i in input().split()] q = int(input()) qq = [] for _ in range(q): qq.append(tuple(map(int, input().split()))) qq.reverse() ans = [-1] * n cur = 0 for t in qq: if t[0] == 2: cur = max(cur, t[1]) elif ans[t[1] - 1] == -1: ans[t[1] - 1] = max(cur, t[2]) for i in range(n): if ans[i] == -1: ans[i] = max(a[i], cur) print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR 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_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
import sys n = int(sys.stdin.readline()) a = [int(i) for i in sys.stdin.readline().split()] q = int(sys.stdin.readline()) arr = [0] * n upd = [-2] * q for w in range(q): event = [int(j) for j in sys.stdin.readline().split()] if event[0] == 1: p = event[1] x = event[2] a[p - 1] = x arr[p - 1] = w else: x = event[1] upd[w] = x for h in range(q - 2, -1, -1): upd[h] = max(upd[h], upd[h + 1]) for g in range(n): a[g] = max(a[g], upd[arr[g]]) a[g] = str(a[g]) print(" ".join(a))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
from sys import stdin input = stdin.readline n = int(input()) arr = list(map(lambda o: (o, 0), map(int, input().split()))) q = int(input()) parr = [] for i in range(q): e = input().split() if e[0] == "1": arr[int(e[1]) - 1] = int(e[2]), i else: parr.append((i, int(e[1]))) tarr = [0] * q pi = len(parr) - 1 hsf = 0 for i in reversed(range(q)): if pi > -1 and parr[pi][0] == i: hsf = max(hsf, parr[pi][1]) pi -= 1 tarr[i] = hsf print(*map(lambda o: max(o[0], tarr[o[1]]), arr))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) a = list(map(int, input().split())) mark = [(1) for i in range(n)] query = [] q = int(input()) m = -1 for i in range(q): next = list(map(int, input().split())) if next[0] == 2: m = max(m, next[1]) query.append(next) mx = 0 for i in range(n): if a[i] < m: a[i] = m for i in range(q - 1, -1, -1): next = query[i] if next[0] == 2: mx = max(mx, next[1]) elif mark[next[1] - 1]: mark[next[1] - 1] = 0 a[next[1] - 1] = next[2] if a[next[1] - 1] < mx: a[next[1] - 1] = mx print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) a = list(map(int, input().split())) b = [-1] * n q = int(input()) qs = list() for _ in range(q): qs.append(list(map(int, input().split()))) maxx = -1 for i in range(q - 1, -1, -1): if qs[i][0] == 1: p = qs[i][1] - 1 if b[p] == -1: a[p] = max(maxx, qs[i][2]) b[p] = 1 else: maxx = max(maxx, qs[i][1]) for i in range(n): if b[i] == -1: print(max(maxx, a[i]), end=" ") else: print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
import sys input = sys.stdin.readline n = int(input()) l = list(map(int, input().split())) q = int(input()) qq = [] ql = [] li = [] for i in range(n): li.append(i) for i in range(q): a = list(map(int, input().split())) qq.append(a) if a[0] == 2: ql.append((a[1], i)) ql.sort(reverse=True) ll = [] ind = -1 for i in ql: val, indx = i if indx > ind: ll.append((indx, val)) ind = indx cur = 0 first = 0 indx = 0 for i in qq: if i[0] == 1: l[i[1] - 1] = i[2] if first: li.append(i[1] - 1) else: typ, ind = i if cur < len(ll) and indx == ll[cur][0]: for j in li: if l[j] < ll[cur][1]: l[j] = ll[cur][1] cur += 1 li = [] first = 1 indx += 1 print(*l)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) a = [int(x) for x in input().split()] dic = {} for i in range(1, n + 1): dic[i] = [a[i - 1], 0] q = int(input()) counter = 0 plane = [] for i in range(q): x = [int(x) for x in input().split()] if x[0] == 1: dic[x[1]] = [x[2], counter] else: plane.append(x[1]) counter += 1 plane.reverse() maxim = [0] for item in plane: maxim.append(max(maxim[-1], item)) arr = [] for item in sorted(dic): arr.append(max(maxim[counter - dic[item][1]], dic[item][0])) print(*arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) a = list(map(int, input().split())) q = int(input()) last_set = [0] * n alles = [0] for i in range(q): r = list(map(int, input().split())) if len(r) == 2: alles.append(r[1]) else: last_set[r[1] - 1] = len(alles) a[r[1] - 1] = r[2] maxis = alles i = len(maxis) - 1 prev_max = 0 while i >= 0: prev_max = max(prev_max, alles[i]) maxis[i] = prev_max i -= 1 for i in range(n): if last_set[i] < len(maxis): a[i] = max(a[i], maxis[last_set[i]]) print(" ".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) a = list(map(int, input().split())) d = {(i + 1): (x, 0) for i, x in enumerate(a)} q = int(input()) event = [-1] * (q + 1) for i in range(q): get = list(map(int, input().split())) if len(get) == 2: t, val = get event[i + 1] = val else: t, p, x = get d[p] = x, i + 1 for i in range(q - 1, -1, -1): event[i] = max(event[i], event[i + 1]) for k, (val, time) in d.items(): if val < event[time]: d[k] = event[time] else: d[k] = val s = "" for i in range(1, n + 1): s += str(d[i]) + " " print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) sp = list(map(int, input().split())) m = int(input()) pos = [-1] * (n + 1) m1 = 0 mem = [] for i in range(m): sp1 = list(map(int, input().split())) mem.append(sp1) if sp1[0] == 1: sp[sp1[1] - 1] = sp1[2] pos[sp1[1] - 1] = i else: m1 = max(m1, sp1[1]) maxs = [-1] * (m + 1) for i in range(m - 1, -1, -1): sp1 = mem[i] if sp1[0] == 2: if i == m - 1 or sp1[1] > maxs[i + 1]: maxs[i] = sp1[1] else: maxs[i] = maxs[i + 1] else: maxs[i] = maxs[i + 1] for i in range(n): if pos[i] != -1 and sp[i] < maxs[pos[i]]: sp[i] = maxs[pos[i]] elif pos[i] == -1: sp[i] = max(sp[i], maxs[0]) print(*sp)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) n = int(input()) a = list(rrd()) op = [] q = int(input()) for i in range(q): c = list(rrd()) op.append(c) sure = [-1] * (n + 1) hi = 0 for i in range(q - 1, -1, -1): if op[i][0] == 1: cnt = op[i][1] if sure[cnt] is -1: sure[cnt] = max(op[i][2], hi) else: hi = max(hi, op[i][1]) for i in range(1, n + 1): if sure[i] is -1: sure[i] = max(hi, a[i - 1]) print(sure[i])
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $x$ are paid accordingly so that after the payout they have exactly $x$ money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$) — the numer of citizens. The next line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_{i} \le 10^{9}$) — the initial balances of citizens. The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^{5}$) — the number of events. Each of the next $q$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($1 \le p \le n$, $0 \le x \le 10^{9}$), or 2 x ($0 \le x \le 10^{9}$). In the first case we have a receipt that the balance of the $p$-th person becomes equal to $x$. In the second case we have a payoff with parameter $x$. -----Output----- Print $n$ integers — the balances of all citizens after all events. -----Examples----- Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 -----Note----- In the first example the balances change as follows: 1 2 3 4 $\rightarrow$ 3 3 3 4 $\rightarrow$ 3 2 3 4 $\rightarrow$ 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 $\rightarrow$ 3 0 2 1 10 $\rightarrow$ 8 8 8 8 10 $\rightarrow$ 8 8 20 8 10
n = int(input()) a = [int(s) for s in input().split()] q = int(input()) b = [None] * n rnd = 0 xs = [] for i in range(q): qi = [int(s) for s in input().split()] if qi[0] == 1: b[qi[1] - 1] = qi[2], rnd else: xs.append(qi[1]) rnd += 1 maxx = 0 if xs: maxx = xs[-1] for i in range(len(xs) - 2, -1, -1): if xs[i] < maxx: xs[i] = maxx else: maxx = xs[i] xs.append(0) for i in range(n): if not b[i]: a[i] = max(maxx, a[i]) else: a[i] = max(b[i][0], xs[b[i][1]]) print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a playlist consisting of $n$ songs. The $i$-th song is characterized by two numbers $t_i$ and $b_i$ — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of $3$ songs having lengths $[5, 7, 4]$ and beauty values $[11, 14, 6]$ is equal to $(5 + 7 + 4) \cdot 6 = 96$. You need to choose at most $k$ songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$) – the number of songs in the playlist and the maximum number of songs you can choose, respectively. Each of the next $n$ lines contains two integers $t_i$ and $b_i$ ($1 \le t_i, b_i \le 10^6$) — the length and beauty of $i$-th song. -----Output----- Print one integer — the maximum pleasure you can get. -----Examples----- Input 4 3 4 7 15 1 3 6 6 8 Output 78 Input 5 3 12 31 112 4 100 100 13 55 55 50 Output 10000 -----Note----- In the first test case we can choose songs ${1, 3, 4}$, so the total pleasure is $(4 + 3 + 6) \cdot 6 = 78$. In the second test case we can choose song $3$. The total pleasure will be equal to $100 \cdot 100 = 10000$.
import sys from sys import stdin, stdout mod = 10**9 + 7 def modinv(n, p): return pow(n, p - 2, p) def ncr(n, r, p): t = fact[n] * (ifact[r] * ifact[n - r] % p) % p return t def ain(): return list(map(int, sin().split())) def sin(): return stdin.readline() def GCD(x, y): while y: x, y = y, x % y return x def merge1(arr, l, m, r): n1 = m - l + 1 n2 = r - m L = [0] * n1 R = [0] * n2 for i in range(0, n1): L[i] = arr[l + i] for j in range(0, n2): R[j] = arr[m + 1 + j] i, j, k = 0, 0, l while i < n1 and j < n2: if L[i][1] < R[j][1]: arr[k] = L[i] i += 1 elif L[i][1] > R[j][1]: arr[k] = R[j] j += 1 elif L[i][0] < R[j][0]: arr[k] = L[i] i += 1 else: arr[k] = R[j] j += 1 k += 1 while i < n1: arr[k] = L[i] i += 1 k += 1 while j < n2: arr[k] = R[j] j += 1 k += 1 def mergesort1(arr, l, r): if l < r: m = (l + (r - 1)) // 2 mergesort1(arr, l, m) mergesort1(arr, m + 1, r) merge1(arr, l, m, r) def merge2(arr, l, m, r): n1 = m - l + 1 n2 = r - m L = [0] * n1 R = [0] * n2 for i in range(0, n1): L[i] = arr[l + i] for j in range(0, n2): R[j] = arr[m + 1 + j] i, j, k = 0, 0, l while i < n1 and j < n2: if L[i][0] > R[j][0]: arr[k] = L[i] i += 1 else: arr[k] = R[j] j += 1 k += 1 while i < n1: arr[k] = L[i] i += 1 k += 1 while j < n2: arr[k] = R[j] j += 1 k += 1 def mergesort2(arr, l, r): if l < r: m = (l + (r - 1)) // 2 mergesort2(arr, l, m) mergesort2(arr, m + 1, r) merge2(arr, l, m, r) n, k = ain() b = [] for i in range(n): b.append(ain()) mergesort1(b, 0, n - 1) r = [] for i in range(n): r.append([b[i][0], i]) mergesort2(r, 0, n - 1) g = [0] * n s = 0 for i in range(k): s += r[i][0] g[r[i][1]] = 1 p = k s1 = 0 for i in range(n): q = s * b[i][1] s1 = max(s1, q) if g[i] == 1: s -= b[i][0] while p < n: if r[p][1] > i: s += r[p][0] g[r[p][1]] = 1 p += 1 break p += 1 print(s1)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a hallway of length $N-1$ and you have $M$ workers to clean the floor. Each worker is responsible for segment $[L_{i}, R_{i}]$, i.e., the segment starting at $L_{i}$ and ending at $R_{i}$. The segments might overlap. Every unit of length of the floor should be cleaned by at least one worker. A worker can clean $1$ unit of length of the floor in $1$ unit of time and can start from any position within their segment. A worker can also choose to move in any direction. However, the flow of each worker should be continuous, i.e, they can’t skip any portion and jump to the next one, though they can change their direction. What’s the minimum amount of time required to clean the floor, if the workers work simultaneously? ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains of $M + 1$ lines of input. First line contains $2$ space separated integers $N$, $M$, length of the hallway and number of workers. Each of the next $M$ lines contain $2$ space separated integers $L_{i}$, $R_{i}$, endpoints of the segment under $i^{th}$ worker. ------ Output: ------ For each testcase, output in a single line minimum time required to clean the hallway or $-1$ if it's not possible to clean the entire floor. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $2 ≤ N ≤ 10^{9}$ $1 ≤ M ≤ 10^{5}$ $1 ≤ L_{i} < R_{i} ≤ N$ The sum of $M$ over all tests is atmost $2*10^{5}$ ----- Sample Input 1 ------ 3 10 3 1 10 1 5 6 10 10 1 2 10 10 2 5 10 1 5 ----- Sample Output 1 ------ 3 -1 5 ----- explanation 1 ------ Case $1$: The first worker cleans the segment $[4, 7]$, the second cleans $[1, 4]$ and the third cleans $[7, 10]$ each taking $3$ units of time. So the minimum time required is $3$ units. Case $2$: Segment $[1, 2]$ is not covered by any worker and hence the entire hallway can't be cleaned. Case $3$: The first worker cleans the segment $[5, 10]$ taking $5$ units of time, and the second cleans $[1, 5]$ taking $4$ units of time. So the minimum time required is $max(5, 4) = 5$ units.
t2 = int(input()) for i in range(t2): n2, m2 = map(int, input().split()) v2 = [] for _m2 in range(m2): x2, y2 = map(int, input().split()) v2.append([y2, x2]) v2.sort() l2 = 0 r2 = n2 - 1 ct = -1 while l2 <= r2: mid = (l2 + r2) // 2 seg = 1 for i in range(m2): if seg >= v2[i][0] or v2[i][1] > seg: continue else: seg = min(seg + mid, v2[i][0]) if seg == n2: ct = mid r2 = mid - 1 else: l2 = mid + 1 print(ct)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than x are paid accordingly so that after the payout they have exactly x money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the numer of citizens. The next line contains n integers a_1, a_2, ..., a_n (0 ≤ a_{i} ≤ 10^{9}) — the initial balances of citizens. The next line contains a single integer q (1 ≤ q ≤ 2 ⋅ 10^{5}) — the number of events. Each of the next q lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x (1 ≤ p ≤ n, 0 ≤ x ≤ 10^{9}), or 2 x (0 ≤ x ≤ 10^{9}). In the first case we have a receipt that the balance of the p-th person becomes equal to x. In the second case we have a payoff with parameter x. Output Print n integers — the balances of all citizens after all events. Examples Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 Note In the first example the balances change as follows: 1 2 3 4 → 3 3 3 4 → 3 2 3 4 → 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 → 3 0 2 1 10 → 8 8 8 8 10 → 8 8 20 8 10
n = int(input()) a = [int(item) for item in input().split()] q = int(input()) qu = list() an = [-1] * n m = 0 for i in range(q): qu.append(tuple(int(item) for item in input().split())) for i in range(q): if qu[q - i - 1][0] == 2: m = max(m, qu[q - i - 1][1]) elif an[qu[q - i - 1][1] - 1] == -1: an[qu[q - i - 1][1] - 1] = max(m, qu[q - i - 1][2]) for i in range(n): if an[i] == -1: an[i] = max(m, a[i]) print(*an)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the government makes payouts to the poor: all citizens who have strictly less money than x are paid accordingly so that after the payout they have exactly x money. In this case the citizens don't send a receipt. You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{5}) — the numer of citizens. The next line contains n integers a_1, a_2, ..., a_n (0 ≤ a_{i} ≤ 10^{9}) — the initial balances of citizens. The next line contains a single integer q (1 ≤ q ≤ 2 ⋅ 10^{5}) — the number of events. Each of the next q lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x (1 ≤ p ≤ n, 0 ≤ x ≤ 10^{9}), or 2 x (0 ≤ x ≤ 10^{9}). In the first case we have a receipt that the balance of the p-th person becomes equal to x. In the second case we have a payoff with parameter x. Output Print n integers — the balances of all citizens after all events. Examples Input 4 1 2 3 4 3 2 3 1 2 2 2 1 Output 3 2 3 4 Input 5 3 50 2 1 10 3 1 2 0 2 8 1 3 20 Output 8 8 20 8 10 Note In the first example the balances change as follows: 1 2 3 4 → 3 3 3 4 → 3 2 3 4 → 3 2 3 4 In the second example the balances change as follows: 3 50 2 1 10 → 3 0 2 1 10 → 8 8 8 8 10 → 8 8 20 8 10
from sys import stdin input = stdin.readline n = int(input()) a = list(map(int, input().split())) q = int(input()) last = [0] * (n + 1) up = [0] * (q + 1) for i in range(1, q + 1): k = list(map(int, input().split())) if k[0] == 1: a[k[1] - 1] = k[2] last[k[1] - 1] = i else: up[i] = k[1] for i in range(q - 1, -1, -1): up[i] = max(up[i], up[i + 1]) for i in range(n): a[i] = max(a[i], up[last[i]]) print(*a)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ non-negative integers. It is guaranteed that $a$ is sorted from small to large. For each operation, we generate a new array $b_i=a_{i+1}-a_{i}$ for $1 \le i < n$. Then we sort $b$ from small to large, replace $a$ with $b$, and decrease $n$ by $1$. After performing $n-1$ operations, $n$ becomes $1$. You need to output the only integer in array $a$ (that is to say, you need to output $a_1$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2\le n\le 10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2.5\cdot 10^5$, and the sum of $a_n$ over all test cases does not exceed $5\cdot 10^5$. -----Output----- For each test case, output the answer on a new line. -----Examples----- Input 5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0 Output 81 3 1 2 0 -----Note----- To simplify the notes, let $\operatorname{sort}(a)$ denote the array you get by sorting $a$ from small to large. In the first test case, $a=[1,10,100]$ at first. After the first operation, $a=\operatorname{sort}([10-1,100-10])=[9,90]$. After the second operation, $a=\operatorname{sort}([90-9])=[81]$. In the second test case, $a=[4,8,9,13]$ at first. After the first operation, $a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$. After the second operation, $a=\operatorname{sort}([4-1,4-4])=[0,3]$. After the last operation, $a=\operatorname{sort}([3-0])=[3]$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) cz = 0 while len(l) > 1: temp = list() for i in range(len(l) - 1): val = l[i + 1] - l[i] if val > 0: temp.append(val) else: cz += 1 if cz > 0: temp.append(0) cz -= 1 temp.sort() l = temp if len(l) == 0: print(0) else: print(l[0])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You are given an array $a$ consisting of $n$ non-negative integers. It is guaranteed that $a$ is sorted from small to large. For each operation, we generate a new array $b_i=a_{i+1}-a_{i}$ for $1 \le i < n$. Then we sort $b$ from small to large, replace $a$ with $b$, and decrease $n$ by $1$. After performing $n-1$ operations, $n$ becomes $1$. You need to output the only integer in array $a$ (that is to say, you need to output $a_1$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2\le n\le 10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2.5\cdot 10^5$, and the sum of $a_n$ over all test cases does not exceed $5\cdot 10^5$. -----Output----- For each test case, output the answer on a new line. -----Examples----- Input 5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0 Output 81 3 1 2 0 -----Note----- To simplify the notes, let $\operatorname{sort}(a)$ denote the array you get by sorting $a$ from small to large. In the first test case, $a=[1,10,100]$ at first. After the first operation, $a=\operatorname{sort}([10-1,100-10])=[9,90]$. After the second operation, $a=\operatorname{sort}([90-9])=[81]$. In the second test case, $a=[4,8,9,13]$ at first. After the first operation, $a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$. After the second operation, $a=\operatorname{sort}([4-1,4-4])=[0,3]$. After the last operation, $a=\operatorname{sort}([3-0])=[3]$.
for tcase in range(int(input())): n = int(input()) a = list(map(int, input().split())) c0 = len([x for x in a if x == 0]) a = [x for x in a if x > 0] while len(a) > 1: if c0 > 0: a.append(0) c0 -= 1 a.sort() b = [] for i in range(len(a) - 1): if a[i] == a[i + 1]: c0 += 1 else: b.append(a[i + 1] - a[i]) a = b[:] print(a[0] if len(a) > 0 else 0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER
You are given an array $a$ consisting of $n$ non-negative integers. It is guaranteed that $a$ is sorted from small to large. For each operation, we generate a new array $b_i=a_{i+1}-a_{i}$ for $1 \le i < n$. Then we sort $b$ from small to large, replace $a$ with $b$, and decrease $n$ by $1$. After performing $n-1$ operations, $n$ becomes $1$. You need to output the only integer in array $a$ (that is to say, you need to output $a_1$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2\le n\le 10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2.5\cdot 10^5$, and the sum of $a_n$ over all test cases does not exceed $5\cdot 10^5$. -----Output----- For each test case, output the answer on a new line. -----Examples----- Input 5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0 Output 81 3 1 2 0 -----Note----- To simplify the notes, let $\operatorname{sort}(a)$ denote the array you get by sorting $a$ from small to large. In the first test case, $a=[1,10,100]$ at first. After the first operation, $a=\operatorname{sort}([10-1,100-10])=[9,90]$. After the second operation, $a=\operatorname{sort}([90-9])=[81]$. In the second test case, $a=[4,8,9,13]$ at first. After the first operation, $a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$. After the second operation, $a=\operatorname{sort}([4-1,4-4])=[0,3]$. After the last operation, $a=\operatorname{sort}([3-0])=[3]$.
import sys input = sys.stdin.readline def high_bound(arr, val): l = -1 r = len(arr) while r - l > 1: m = (l + r) // 2 if arr[m] <= val: l = m else: r = m return r for _ in range(int(input())): n = int(input()) (*a,) = map(int, input().split()) cnt = high_bound(a, 0) a = [0] * (cnt > 0) + a[cnt:] cnt = max(cnt - 1, 0) for i in range(n - 1, 0, -1): b = [] for j in range(len(a) - 1): d = a[j + 1] - a[j] if d: b.append(d) else: cnt += 1 if cnt: b.append(0) cnt -= 1 b.sort() a = b print(b[0])
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
You are given an array $a$ consisting of $n$ non-negative integers. It is guaranteed that $a$ is sorted from small to large. For each operation, we generate a new array $b_i=a_{i+1}-a_{i}$ for $1 \le i < n$. Then we sort $b$ from small to large, replace $a$ with $b$, and decrease $n$ by $1$. After performing $n-1$ operations, $n$ becomes $1$. You need to output the only integer in array $a$ (that is to say, you need to output $a_1$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2\le n\le 10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2.5\cdot 10^5$, and the sum of $a_n$ over all test cases does not exceed $5\cdot 10^5$. -----Output----- For each test case, output the answer on a new line. -----Examples----- Input 5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0 Output 81 3 1 2 0 -----Note----- To simplify the notes, let $\operatorname{sort}(a)$ denote the array you get by sorting $a$ from small to large. In the first test case, $a=[1,10,100]$ at first. After the first operation, $a=\operatorname{sort}([10-1,100-10])=[9,90]$. After the second operation, $a=\operatorname{sort}([90-9])=[81]$. In the second test case, $a=[4,8,9,13]$ at first. After the first operation, $a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$. After the second operation, $a=\operatorname{sort}([4-1,4-4])=[0,3]$. After the last operation, $a=\operatorname{sort}([3-0])=[3]$.
t = int(input()) while t: p = int(input()) s = list(map(int, input().split())) check = 0 while p > 1: a = [] if check > 0: check -= 1 a.append(s[0]) for i in range(1, len(s)): if s[i] == s[i - 1]: check += 1 else: a.append(s[i] - s[i - 1]) s = a if len(s) == 0: break s = sorted(a) p -= 1 if len(s) == 0: print(0) else: print(s[0]) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given an array $a$ consisting of $n$ non-negative integers. It is guaranteed that $a$ is sorted from small to large. For each operation, we generate a new array $b_i=a_{i+1}-a_{i}$ for $1 \le i < n$. Then we sort $b$ from small to large, replace $a$ with $b$, and decrease $n$ by $1$. After performing $n-1$ operations, $n$ becomes $1$. You need to output the only integer in array $a$ (that is to say, you need to output $a_1$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2\le n\le 10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2.5\cdot 10^5$, and the sum of $a_n$ over all test cases does not exceed $5\cdot 10^5$. -----Output----- For each test case, output the answer on a new line. -----Examples----- Input 5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0 Output 81 3 1 2 0 -----Note----- To simplify the notes, let $\operatorname{sort}(a)$ denote the array you get by sorting $a$ from small to large. In the first test case, $a=[1,10,100]$ at first. After the first operation, $a=\operatorname{sort}([10-1,100-10])=[9,90]$. After the second operation, $a=\operatorname{sort}([90-9])=[81]$. In the second test case, $a=[4,8,9,13]$ at first. After the first operation, $a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$. After the second operation, $a=\operatorname{sort}([4-1,4-4])=[0,3]$. After the last operation, $a=\operatorname{sort}([3-0])=[3]$.
t = int(input()) while t > 0: t -= 1 n = int(input()) arrA = list(map(int, input().split())) arr = arrA.copy() arrA.sort() for j in range(1, n): k = [] for i in range(len(arrA) - 1): k.append(arrA[i + 1] - arrA[i]) k.sort() arrA = k[k.count(0) :] if len(arrA) < n - j: arrA.insert(0, 0) if len(arrA) == 1: print(arrA[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for tea in range(int(input())): n = int(input()) s = input() inds = [] for i in range(n): if s[i] == s[0]: inds.append(i) good = 1 while True: sobad = False for i in inds: if i + good >= n: sobad = True break if s[i + good] != s[good]: sobad = True break if sobad: break good += 1 if good == n: break print(s[:good])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) while t > 0: n = int(input()) strr = input() f_count = strr.count(strr[0]) piece = len(strr) // f_count for i in range(piece): if strr.count(strr[: piece - i]) == f_count: print(strr[: piece - i]) break else: continue t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = input() c = 0 L = [] for i in range(n): if s[i] == s[0]: c += 1 L.append(i) p = n // c i = 1 f = 0 while i <= p: for j in L: if j + i >= n: f = 1 break if s[i] != s[j + i]: f = 1 break if f: break i += 1 print(s[:i])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
ts = int(input()) while ts > 0: n = int(input()) st = input() pos = [] for i in range(1, len(st)): if st[i] == st[0]: pos.append(i) check = 1 flag = True while check < n: for i in pos: if i + check > n - 1 or st[i + check] != st[check]: flag = False break if not flag: break check += 1 print(st[0:check]) ts -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input().strip())): n = int(input().strip()) string = input().strip() if string.count(string[0]) == 1: print(string) elif string.count(string[0]) == len(string): print(string[0]) else: for i in range(1, len(string)): if string[0] == string[i]: sub = string[:i] break max_count = string.count(string[0]) sc = string.count(sub) if sc >= max_count: print(sub) else: for i in range(1, len(sub)): if string.count(sub[:-i]) >= max_count: print(sub[:-i]) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
def snek(s, n): x = s[0] k = [] n2 = 0 for i in range(n): if s[i] == x: k.append(i) n2 += 1 for j in range(1, n): c = 0 k1 = [] for z in k: if z != n - 1 and s[z + 1] == s[j]: c += 1 k1.append(z + 1) k = k1 if n2 == c: x += s[j] else: break return x t = int(input()) for i in range(t): n = int(input()) s = input() print(snek(s, n))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for i in range(0, T): N = int(input()) s = input() L = [] for j in range(0, len(s)): if s[j] == s[0]: L.append(j) if len(L) == 1: print(s) else: c = 1 flag = 0 while flag == 0: temp = 0 for j in range(0, len(L)): if L[j] + c < len(s): if s[L[j] + c] == s[c]: temp = temp + 1 else: flag = 1 break if temp == len(L): c = c + 1 else: flag = 1 print(s[:c])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
try: test = int(input()) while test > 0: n = int(input()) s = input() l = set(s.split(s[0])) m = 0 ans = s for i in l: k = s.count(s[0] + i) if k >= m: m = k ans = s[0] + i print(ans) test -= 1 except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = str(input()) x = s.count(s[0]) if x == 1: print(s) else: l, r = 0, n // x + 1 mid = (l + r) // 2 while l != r: mid = (l + r) // 2 if s.count(s[:mid]) < x: r = mid else: l = mid if l == r - 1: if s.count(s[:l]) > s.count(s[:r]): mid = l else: mid = r break print(s[:mid])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
def check(lst): for i in range(len(lst)): if i > 0: if lst[i] != lst[i - 1]: return False return True for i in range(int(input())): n = int(input()) stn = input() y = len(stn) l = [] for i in range(len(stn)): if stn[i] == stn[0]: l.append(i) x = [] while len(x) != len(l): x.append(1) ans = 1 f = l[:] while True: f = list(map(lambda z, y: z + y, f, x)) t = [] p = False for i in f: if i >= len(stn): p = True break else: t.append(stn[i]) if p: break if check(t): ans += 1 else: break if len(f) == 1: print(stn) else: print(stn[:ans])
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for i in range(T): n = int(input()) st = input() s = set(st.split(st[0])) counter = st.count(st[0]) a = st[0] for j in s: c = st.count(st[0] + j) if c == counter: if len(a) < len(st[0] + j): a = st[0] + j print(a)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for testcase in range(T): N = int(input()) text = input() start = 1 end = N dictCount = {} i = start while i < end: c = 0 count = 0 j = i while i < end and c < j and text[i] == text[c]: i += 1 c += 1 if c not in dictCount: dictCount[c] = 1 else: dictCount[c] += 1 if c == 0: i += 1 maxLen = -1 maxCount = 0 for k in dictCount: if dictCount[k] > maxCount: maxCount = dictCount[k] maxLen = k elif dictCount[k] == maxCount: if k > maxLen: maxCount = dictCount[k] maxLen = k if text[0] not in text[1:]: print(text) else: print(text[:maxLen])
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 ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
n = int(input()) t = 0 while t < n: N = int(input()) S = input() maxoc = S.count(S[0]) x = N // maxoc for i in range(0, x): pref = S[: x - i] if S.count(pref) == maxoc: print(pref) break t = t + 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for z in range(int(input())): N = int(input()) S = str(input()) count = 0 ls = [] new_string = S[0] for i in range(N): if new_string == S[i]: ls.append(i) count += 1 if count == 1: print(S) else: check = True for i in range(1, N + 1): count_1 = 0 for k, j in enumerate(ls): if j != N - 1 and S[i] == S[j + 1]: count_1 += 1 ls[k] = j + 1 if count_1 == count: new_string += S[i] else: break print(new_string)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for i in range(t): n = int(input()) string = input() first = string.count(string[0]) total = len(string) maxLength = total // first for j in range(maxLength): if string.count(string[: maxLength - j]) == first: print(string[: maxLength - j]) break
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 ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for _ in range(t): n = int(input()) s = input() max_length = n for j in range(1, n): i = 0 if s[i] == s[j]: k = j + 1 lim = min(j + max_length, n) while k < lim: i += 1 if s[i] != s[k]: break k += 1 max_length = k - j print(s[:max_length])
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 ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for testcase in range(T): N = int(input()) S = str(input()) maxlen = N if N == 1: print(S) continue fst = S[0] idxlist = [] for i in range(1, N): if S[i] == fst: idxlist.append(i) if len(idxlist) == 0: print(S) continue ichar = idxlist[0] for i in range(len(idxlist)): ichar = idxlist[i] j = 0 while ichar < N: if S[ichar] == S[j]: ichar += 1 else: break j += 1 maxlen = min(maxlen, j) print(S[0:maxlen])
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 ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
def sol(s, n): c = 1 l = [] for i in range(n): if s[i] == s[0]: l.append(i) while 1: if c >= n: break for i in range(len(l)): if l[i] + 1 >= n: return c - 1 if s[l[i] + 1] == s[c]: l[i] += 1 else: return c - 1 c += 1 return n - 1 for _ in range(int(input())): n = int(input()) s = input() print(s[: sol(s, n) + 1])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) S = input() X = [] for _ in range(n): if S[_] == S[0]: X.append(_) x = len(X) if x == 1: print(S) continue for _ in range(0, X[1] + 1): for __ in range(x): if n <= X[__] + _: break if S[X[__] + _] != S[_]: break else: continue break print(S[:_])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): m = n = int(input()) s = input() for i in range(n - 1, 0, -1): if s[i] == s[0]: m = min(m, n - i) m = next((j for j in range(m) if s[i + j] != s[j]), m) print(s[:m])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) st = input() t = st[0] if st.count(t) == 1: print(st) continue l = [] for i, j in enumerate(st): if j == t: l.append(i) c = 1 l1 = len(l) while 1: co = 0 if l[0] + c != n: t = st[l[0] + c] else: break for i in l: if i + c == n: break if st[i + c] == t: co += 1 else: break if co != l1: break c = c + 1 print(st[:c])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for i in range(t): n = int(input()) s = input() if s.count(s[0]) == 1: print(s) else: max = 0 res = "" ans = set(s.split(s[0])) for j in ans: if s.count(s[0] + j) >= max: max = s.count(s[0] + j) res = s[0] + j print(res)
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 IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for i in range(t): n = int(input()) s = input() indices = [] for i in range(len(s)): if s[i] == s[0]: indices.append(i) maximum_possible = 0 for i in range(1, n): if s[i] == s[0]: maximum_possible = i break else: maximum_possible = n ans = 1 if len(indices) == 1: ans = len(s) else: done = False while ans < maximum_possible and not done: for i in indices: if i + ans >= n or s[i + ans] != s[ans]: done = True break else: ans += 1 print(s[:ans])
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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = input() new_string = s[0] l = [] count_1 = 0 for i in range(n): if s[i] == new_string: l.append(i) count_1 += 1 for i in range(1, n): count_2 = 0 l1 = [] for j in l: if j != n - 1 and s[j + 1] == s[i]: count_2 += 1 l1.append(j + 1) l = l1 if count_1 == count_2: new_string += s[i] else: break print(new_string)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for _ in range(t): pfx = [] n = int(input()) s = input() for i in range(1, n): if s[i] == s[0]: pfx.append(i) if len(pfx) == 0: print(s) else: mc = 0 flag = 1 lim = pfx[0] for i in range(1, lim + 1): for j in pfx: if i + j < n: if s[i] != s[i + j]: flag = 0 break else: flag = 0 break if flag == 0: mc = i break print(s[:mc])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) while t > 0: n = int(input()) l = input() l = l + "00" b = l[0] g = [] for i in range(1, n): if l[i] == b: g.append(i) flag = 1 if g == []: print(l[0:n]) else: for i in range(1, g[0] + 1): if flag == 1: for j in g: if l[i] != l[j + i]: print(l[0:i]) flag = 0 break t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for i in range(0, t): n = int(input()) s = input() ch = s[0] lis = [] for j in range(0, n): if s[j] == ch: lis.append(j) l = len(lis) if l == 1: print(s) else: ht = 0 for j in range(1, n): for k in range(0, l): if lis[k] + j > n - 1 or s[lis[k] + j] != s[j]: ht = 1 break if ht == 1: break print(s[:j])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for _ in range(T): N = int(input()) que = input() n = len(que) start = [i for i in range(1, N) if que[0] == que[i]] if start == []: print(que) else: ans = "" for i in range(0, start[0]): flag = 0 for j in start: if j + i >= N or que[j + i] != que[i]: flag = 1 break if flag: break ans = ans + que[i] print(ans)
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
def Tee(): N = int(input()) arr = input() arr_copy = arr[1:] min_possible = arr.count(arr[0]) if min_possible == 1: return arr elif min_possible == N: return arr[0] index = 0 index_list = [0] prev_index = index index = arr_copy.find(arr[0]) while index >= 0: index_list.append(prev_index + index + 1) prev_index = prev_index + index + 1 arr_copy = arr_copy[index + 1 :] index = arr_copy.find(arr[0]) length = len(index_list) add = 1 while True: for i in range(1, length): if index_list[i] + add >= N or arr[add] != arr[index_list[i] + add]: return arr[:add] add += 1 for i in range(int(input())): print(Tee())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
def res(): for _ in range(int(input())): n = int(input()) s = input().rstrip() c = s.count(s[0]) if c == 1: print(s) else: i = 1 + s[1:].index(s[0]) for j in range(len(s[:i]), 0, -1): if s.count(s[:j]) == c: print(s[:j]) break res()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
test = int(input()) for _ in range(test): n = int(input()) s = input() if len(s) == 1: print(s) continue first, counts, cur_len = s[0], [], 1 for i in range(n): if s[i] == first: counts.append(i) if len(counts) == 1: print(s) continue while True: for i in range(len(counts) - 1): if counts[i] + cur_len >= n or counts[i + 1] + cur_len >= n: break if s[counts[i] + cur_len] != s[counts[i + 1] + cur_len]: break else: cur_len += 1 continue break print(s[:cur_len])
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 IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = input() l = s[0] ind = [] for i in range(len(s)): if s[i] == l: ind.append(i) else: fl = 0 ans = 1 for i in range(n): for k in ind[1:]: if k + i >= n: fl = 1 elif s[k + i] != s[i]: fl = 1 if fl: break if fl: break ans += 1 print("".join(s[: ans - 1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for _ in range(T): N = int(input()) S = input() l = [0] for i in range(1, N): if S[i] == S[0]: l.append(i) result = S[0] if len(l) == 1: print(S) else: for i in range(1, l[1]): ch = S[i] flag = 1 for j in l: if j + i < N: if S[j + i] != ch: flag = -1 break else: flag = -1 break if flag == -1: break else: result = S[0 : i + 1] print(result)
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 ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = input() idx = [] ts = "" for i in range(n): if s[i] == s[0]: idx.append(i) idxl = len(idx) ans = 0 for i in range(1, n): j = 0 while j < idxl: if idx[j] + i >= n or s[idx[j] + i] != s[i]: ans = i break j += 1 if ans: break if ans: print(s[0:ans]) else: print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for t in range(int(input())): n = int(input()) text = input() check = [i for i in range(1, n) if text[i] == text[0]] l = len(check) if not l: print(text) continue pat = text[0] next_index = 1 flag = 0 for i in range(n - check[-1] - 1): for j in check: if text[j + next_index] != text[next_index]: flag = 1 break if flag: break pat = pat + text[next_index] next_index += 1 print(pat)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) for test_case in range(T): N = int(input()) S = input() index = list() for i in range(N): if S[i] == S[0]: index.append(i) ans, flag, count = S[0], True, 1 while flag and index[-1] + count < N: if all([(S[i + count] == S[count]) for i in index]): ans = S[: count + 1] else: flag = False count += 1 print(ans)
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 ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) txt = input().strip() i, ans, start = 1, n, 1 while i < n: if txt[i] == txt[0]: ans = min(ans, start) start = 1 i += 1 else: if txt[start] == txt[i]: start += 1 else: ans = min(ans, start) i += 1 ans = min(ans, start) print(txt[:ans])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for i in range(t): n = int(input()) s = input().strip() x = s[0] ind = [] for j in range(n): if s[j] == x: ind.append(j) if len(ind) == 1: print(s) continue f = 1 travel = 0 op = "" while f: x = s[travel] if ind[len(ind) - 1] + travel >= n: f = 0 break for index in ind: if s[index + travel] == x: continue else: f = 0 break if f: op = op + x travel = travel + 1 print(op)
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 FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) a = list(input()) b = [0] * n j = 0 while j < n: b[j] += 1 i = j k = 0 while j < n and k < i and a[j] == a[k]: b[k] += 1 k += 1 j += 1 j += 1 c = max(b) j = n - 1 while b[j] != c: j -= 1 print("".join(a[: j + 1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
from sys import stdin input = stdin.readline def answer(): pos = [i for i in range(n)] m, ans, st = -1000000000.0, "", "" for ch in s: st += ch x = [] for i in pos: if i + 1 < n and s[i + 1] == ch: x.append(i + 1) if m <= len(x): m = len(x) ans = st else: break pos = x return ans for T in range(int(input())): n = int(input()) s = input()[:n] print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER STRING STRING FOR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = input() l = [s[0]] m = n j = n for i in range(1, n): if s[i] == l[0]: j = i m = i break l.append(s[i]) temp = 0 for i in range(j, n): if s[i] == l[temp]: temp = temp + 1 if temp == j: temp = 0 else: if temp < m and temp != 0: m = temp if m == 1: break temp = 0 if temp < m and temp != 0: m = temp for i in range(m): print(l[i], end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
def m(): t = int(input()) while t > 0: n = int(input()) s = input() length = len(s) k = "" l = [] l1 = [] c = s.count(s[0]) pcs = int(length / c) if c == 1: print(s) else: for i in range(pcs): if s.count(s[: pcs - i]) == c: print(s[: pcs - i]) break t -= 1 m()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for t in range(int(input())): n = int(input()) s = input() candidates = set() count = 0 for i in range(1, n): if s[i] == s[count] and s[i - count] == s[0]: candidates.add(i) if len(candidates) == 0: print(s) else: candidates = list(candidates) lc = len(candidates) ans = s[0] for i in range(1, n): count1 = 0 for j, c in enumerate(candidates): if c != n - 1 and s[c + 1] == s[i]: candidates[j] = c + 1 count1 += 1 if count1 == lc: ans += s[i] else: break 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) while t: t -= 1 n = int(input()) s = input() l = [] i = 1 l.append(0) while i < n: try: l.append(l[i - 1] + 1 + s[l[i - 1] + 1 :].index(s[0])) except: break i += 1 k = "" i = 1 if len(l) == 1: print(s) else: k += s[i - 1] while i < n: for j in range(len(l)): try: if s[l[j] + 1] != s[i]: i = n break l[j] += 1 except: i = n break if i != n: k += s[i] i += 1 print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for t_i in range(t): n = int(input()) string = input() ncc = n for i in range(1, n): j = i k = 0 nc = ncc flag = False while j < n and k < nc: if string[k] == string[j]: if flag: ncc += 1 else: ncc = 1 flag = True k += 1 j += 1 else: break print(string[:ncc])
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 ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) s = input() pos = [] for i in range(1, len(s)): if s[i] == s[0]: pos.append(i) if len(pos) == 0: print(s) else: i = 1 f = 1 while i < pos[0]: for j in pos: if i + j >= n: f = 0 break if i + j < n and s[i] != s[j + i]: f = 0 break if f == 0: break i += 1 print(s[:i])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
T = int(input()) while T > 0: T -= 1 N = int(input()) string = input() prefix = [] for i in range(len(string)): prefix.append(1) temp = False first_letter = string[0] var = "A" k = 0 for i in range(1, len(string)): if temp: var += string[i] if string[i] == first_letter: var = string[i] k = 0 if var == string[: k + 1]: prefix[k] += 1 temp = True k += 1 else: temp = False k = 0 max = -1 for i in range(len(prefix)): if max <= prefix[i]: max = prefix[i] index = i if max != 1: print(string[: index + 1]) else: print(string)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for i in range(int(input())): n = int(input()) s = input() l = [] c = 0 for i in range(1, n): if s[i] == s[0]: l.append(i) if len(l) == n - 1: p = s else: for i in range(1, n + 1): count = 0 for j in range(len(l)): if l[j] >= i and l[j] + i - 1 < n: if s[l[j] + i - 1] == s[i - 1]: count += 1 else: l[j] = -1 if count >= c: c = count p = s[:i] else: break print(p)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): length = int(input()) s = input() c = 0 pos = [] for ii, i in enumerate(s): if i == s[0]: c += 1 pos.append(ii) if c == 1: print(s) continue flag = 0 prev_c = 0 while flag == 0: prev_c += 1 for p in pos: if length < p + prev_c: flag = 1 break if s[0:prev_c] != s[p : p + prev_c]: flag = 1 break print(s[0 : prev_c - 1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for _ in range(t): n = int(input()) st = input() main = [0] maxi = 1 maxi_se = st[0] prev = st[0] for i in range(1, n): if st[i] == st[0]: maxi += 1 main.append(i) for i in range(1, n): prev += st[i] new_main = [] count = 0 for j in range(len(main)): if main[j] + 1 < n and st[main[j] + 1] == st[i]: count += 1 new_main.append(main[j] + 1) if maxi < count: maxi = count maxi_se = prev elif maxi == count: maxi = count maxi_se = prev else: break if len(new_main) == 0: break else: main = new_main[:] print(maxi_se)
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 ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) while t > 0: t -= 1 n = int(input()) s = input() if len(s) == 1: print(s) continue part = s.split(s[0]) if len(part) == 2: print(s) continue part = set(part) if "" in part: part.remove("") pre = "" + s[0] i = 0 flag = True while True: for item in part: if i >= len(item) or item[i] != s[i + 1]: flag = False break if not flag: break i += 1 pre += s[i] if s.count(pre) == s.count(s[0]): print(pre) else: print(s[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for t in range(int(input())): n = int(input()) y = input() a = list(y) h = [] d = [] j = 1 for i in range(n): if a[i] == a[0]: d.append(i) count = a.count(a[0]) if count == 1: print("".join(a)) elif a[-1] == a[0]: print(a[0]) else: for i in range(d[1] + 1): d = [(x + 1) for x in d] if d[-1] == n: break g = [a[x] for x in d] if g.count(g[0]) == count: j = j + 1 else: break print("".join(a[0:j]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for _ in range(int(input())): n = int(input()) arr = list(input()) list2 = [] i, j, max_len, leng = 0, 1, 1, 1 while i <= n - 1: if arr[i] == arr[0]: j = 1 i += 1 leng = 1 while i <= n - 1 and j <= n - 1 and arr[i] == arr[j] and arr[i] != arr[0]: leng += 1 i += 1 j += 1 list2.append(leng) else: i += 1 print("".join(arr[: min(list2)]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
t = int(input()) for i in range(t): n = int(input()) a = input() l = list(a) li = [] k = 0 for j in range(n): if l[j] == l[0]: li.append(j) k = k + 1 m = 1 flag = 0 while 1: try: for j in range(k): if l[li[j] + m] != l[li[0] + m]: ans = m flag = 1 break except: ans = m flag = 1 if flag == 1: break else: m = m + 1 print(a[:m])
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef wants to prepare a special dish. There is a special ingredient Chef needs for this dish, so he decided to search for it in all of Chefland. After some research, he discovered it in a secret cave. He may only enter the cave if he has a password; fortunately, a string $S$, which serves as a hint for this password, is written next to the cave. Chef knows that the secret password is a non-empty prefix of the string $S$. Also, he knows that: if a prefix occurs in $S$ more often as a substring, then the probability that this prefix is the secret password is higher (the probability that a chosen prefix is the secret password is an increasing function of its number of occurrences in $S$) if two prefixes have the same number of occurrences in $S$, then the longer prefix has a higher probability of being the secret password Chef wants to guess the secret password with the maximum possible probability of success. Help Chef and find the prefix for which this probability is maximum. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one string — the prefix with the highest probability of being the secret password. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100,000$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 3 abc 5 thyth 5 abcbc ----- Sample Output 1 ------ abc th abcbc ----- explanation 1 ------ Example case 1: Each prefix occurs in $S$ the same number of times (once) and the prefix with maximum length is "abc", so the secret password is most likely "abc".
for t in range(int(input())): n = int(input()) a = input() s = list(a) count = 0 indi = n + 1 r = s[0] result = [(1) for i in range(0, n)] for i in range(1, n): if s[i] == r: result[0] += 1 count = 1 indi = i elif s[i] == s[count] and count < indi: result[count] += 1 count += 1 else: count = 0 b = [] for i in range(n): b.append([result[i], i + 1]) b.sort(reverse=True) print(a[: b[0][1]])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) a = [int(x) for x in input().split()] sorted_a = sorted(a) dict_a = {} for x in a: if not x in dict_a: dict_a[x] = 1 else: dict_a[x] += 1 sorted_uniq_a = sorted(dict_a.keys()) max_fib_prefix = [a[0], a[1]] for i in range(0, len(sorted_uniq_a)): for j in range(0, len(sorted_uniq_a)): if i != j or dict_a[sorted_uniq_a[i]] > 1: if sorted_uniq_a[i] + sorted_uniq_a[j] > sorted_uniq_a[-1]: break fib_prefix = [sorted_uniq_a[i], sorted_uniq_a[j]] dict_a[sorted_uniq_a[i]] -= 1 dict_a[sorted_uniq_a[j]] -= 1 while True: next_fib = fib_prefix[-1] + fib_prefix[-2] if not next_fib in dict_a or dict_a[next_fib] == 0: break fib_prefix.append(next_fib) dict_a[next_fib] -= 1 for x in fib_prefix: dict_a[x] += 1 if len(fib_prefix) > len(max_fib_prefix): max_fib_prefix = fib_prefix print(len(max_fib_prefix))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) a = [int(x) for x in input().split()] D = {} for x in a: if x in D: D[x] += 1 else: D[x] = 1 maxans = 0 def check(x, y): num = 2 D[x] -= 1 D[y] -= 1 while x + y in D and D[x + y] > 0: D[x + y] -= 1 x, y = y, x + y num += 1 ans = num while num > 2: D[y] += 1 x, y = y - x, x num -= 1 D[x] += 1 D[y] += 1 return ans for x in D: for y in D: if x == y and D[x] == 1: continue maxans = max(check(x, y), maxans) print(maxans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
import sys sys.setrecursionlimit(10000) n = int(input()) a = list(map(int, input().split())) sb = {} for num in a: if not num in sb: sb[num] = 1 else: sb[num] += 1 def go(a, b): ans = 0 ab = a + b if ab in sb and sb[ab] > 0: sb[ab] -= 1 ans = 1 + go(b, ab) sb[ab] += 1 return ans maxans = 2 for a in sb: for b in sb: if a != b or sb[a] > 1: count = 2 sb[a] -= 1 sb[b] -= 1 count += go(a, b) sb[a] += 1 sb[b] += 1 if count > maxans: maxans = count print(maxans)
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) l = list(map(int, input().split())) d = {} for i in l: if i not in d: d[i] = 0 d[i] += 1 ans = 0 for i in range(len(l)): for j in range(len(l)): if i == j: continue n1, n2 = l[i], l[j] if n1 == 0 and n2 == 0: continue curr = 2 d2 = {n1: 1, n2: 1} while True: n1, n2 = n2, n1 + n2 ans = max(ans, curr) if n2 in d and (d2[n2] if n2 in d2 else 0) < d[n2]: if n2 not in d2: d2[n2] = 0 d2[n2] += 1 curr += 1 else: break print(max(ans, d[0] if 0 in d else -1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) a = [int(i) for i in input().split()] ans = a.count(0) d, s = dict(), set(a) for i in a: if i not in list(d.keys()): d[i] = 0 d[i] += 1 for i in range(n): for j in range(n): if i == j or a[i] == a[j] == 0: continue a1, a2 = a[i], a[j] ln, dl = 2, [a1, a2] d[a1] -= 1 d[a2] -= 1 while a1 + a2 in s: a1, a2 = a2, a1 + a2 d[a2] -= 1 dl.append(a2) if d[a2] < 0: break ln += 1 ans = max(ans, ln) for v in dl: d[v] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER LIST VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
def rec(a, b): res, c = 0, a + b if d.get(c) and d[c] > 0: d[c] -= 1 res = rec(b, c) + 1 d[c] += 1 return res input() d = {} for i in map(int, input().split()): if d.get(i): d[i] += 1 else: d[i] = 1 ans = 2 for a in d: for b in d: if a != b or d[a] > 1: d[a] -= 1 d[b] -= 1 cnt = rec(a, b) + 2 d[a] += 1 d[b] += 1 ans = max(ans, cnt) print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) a = [int(x) for x in input().split()] n0 = len([x for x in a if x == 0]) a = [x for x in a if x != 0] if n0 > 0: a.append(0) n = len(a) pos = {} for i in range(n): if a[i] in pos: pos[a[i]] += 1 else: pos[a[i]] = 1 res = [] max_r = n0 if max_r < 2: max_r = 2 pr = {} for i in range(0, n): for j in range(0, n): if i == j: continue if (a[i], a[j]) in pr: continue if a[i] + a[j] not in pos: continue res = 2 a1 = a[i] a2 = a[j] s = a1 + a2 m = [] pos[a1] -= 1 pos[a2] -= 1 m.append(a1) m.append(a2) while s in pos and pos[s] > 0: pos[s] -= 1 m.append(s) res += 1 a1 = a2 a2 = s s = a1 + a2 for k in range(len(m)): pos[m[k]] += 1 if k > 0: pr[m[k - 1], m[k]] = res - k + 1 if res > max_r: max_r = res pr[a[i], a[j]] = res print(max_r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) a = list(map(int, input().split())) s = {} d = {} p = set() mx = -(10**15) mi = -mx ans = 2 for i in range(n): if a[i] in s: s[a[i]] += 1 else: s[a[i]] = 1 mx = max(mx, a[i]) mi = min(mi, a[i]) if 0 in s: ans = max(ans, s[0]) for i in range(n): for j in range(n): if j == i or a[i] == 0 and a[j] == 0 or (a[i], a[j]) in p: continue f0 = a[i] f1 = a[j] ansi = 2 f2 = 0 d = {} p.add((a[i], a[j])) while True: f2 = f1 + f0 f0 = f1 f1 = f2 if f2 > mx or f2 < mi: break if f2 in s: d[f2] = s[f2] else: break f0 = a[i] f1 = a[j] if f1 == f0: d[f0] = s[f0] - 2 else: d[f0] = s[f0] - 1 d[f1] = s[f1] - 1 while True: f2 = f1 + f0 f0 = f1 f1 = f2 if f2 > mx or f2 < mi: break if f2 in d and d[f2] > 0: ansi += 1 d[f2] -= 1 else: break ans = max(ans, ansi) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
n = int(input()) a = list(map(int, input().split())) ans = a.count(0) d = {} for i in a: if i not in d: d[i] = 1 else: d[i] += 1 ans = max(ans, 2) for i in range(n): for j in range(n): if i != j and (a[i] != 0 or a[j] != 0): first = a[i] second = a[j] temp = [first, second] third = first + second while True: if abs(third) > int(1000000000.0): break if third not in d: break temp.append(third) first = second second = third third = first + second count = 0 f = 1 for k in range(len(temp)): if d[temp[k]] > 0: d[temp[k]] -= 1 count += 1 else: f = 0 for j in range(k): d[temp[j]] += 1 break if f: for k in temp: d[k] += 1 ans = max(ans, count) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
from sys import stdin, stdout def fin(): return stdin.readline().strip() def fout(x): stdout.write(str(x) + "\n") def m_add(mp, val): mp[val] = mp.get(val, 0) + 1 def m_remove(mp, val): if mp[val] == 1: mp.pop(val) else: mp[val] -= 1 n, arr, mp, res = int(fin()), list(map(int, fin().split())), {}, 0 for i in arr: m_add(mp, i) for i in range(n): for j in range(n): if i == j: continue cnt, a, b = 2, arr[i], arr[j] if a == 0 and b == 0: res = max(res, mp[0]) continue m_remove(mp, a) m_remove(mp, b) while a + b in mp: m_remove(mp, a + b) a, b = b, a + b cnt += 1 res = max(res, cnt) while a != arr[i] or b != arr[j]: a, b = b - a, a m_add(mp, a + b) m_add(mp, a) m_add(mp, b) print(res)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR DICT NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
n = int(input()) t = sorted(map(int, input().split())) m = 5001 d = [0, 0] * m for a in t: for b in t: d[b - a] += 1 for i in range(m, 2 * m): d[i] = d[i - 1] + d[i] s = 0 for i in range(1, m): s += d[i] * sum(d[j] * d[-1 - i - j] for j in range(1, m - i)) print(8 * s / (n * n - n) ** 3)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FOR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
import sys n = int(sys.stdin.readline()) a = [int(s) for s in sys.stdin.readline().split()] a.sort() diffs1 = [] for i in range(5000): diffs1.append(0) for i in range(n): for j in range(i + 1, n): diffs1[a[j] - a[i]] += 1 diffs2 = [] for i in range(10000): diffs2.append(0) for i in range(len(diffs1)): for j in range(i, len(diffs1)): if i == j: diffs2[i + j] += diffs1[i] * diffs1[j] else: diffs2[i + j] += 2 * diffs1[i] * diffs1[j] for i in range(1, len(diffs2)): diffs2[i] += diffs2[i - 1] good = 0 for u in range(n - 1, 0, -1): for t in range(u - 1, -1, -1): good += diffs2[a[u] - a[t] - 1] all = n * (n - 1) // 2 all = all * all * all print(float(good) / float(all))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
def main(): n = int(input()) a = list(map(int, input().split())) max_element = max(a) + 1 diff_freq = [(0) for i in range(max_element)] for i in range(n): for j in range(i): diff_freq[abs(a[i] - a[j])] += 1 largest = [(0) for i in range(max_element)] for i in range(max_element - 2, 0, -1): largest[i] = largest[i + 1] + diff_freq[i + 1] good_ones = 0 for i in range(max_element): for j in range(max_element): if i + j < max_element: good_ones += diff_freq[i] * diff_freq[j] * largest[i + j] ans = good_ones / (n * (n - 1) / 2) ** 3 print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
ways = [0] * 5001 s_ways = [0] * 5001 n = int(input()) a = set(map(int, input().split())) for diff in range(4999, 0, -1): for i in a: if i >= diff and i - diff in a: ways[diff] += 1 ways[diff] /= n * (n - 1) / 2 s_ways[diff] = s_ways[diff + 1] + ways[diff] ans = 0 for diff1 in range(1, 5000): for diff2 in range(1, 5000 - diff1): targetdiff = diff1 + diff2 + 1 ans += ways[diff1] * ways[diff2] * s_ways[targetdiff] print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
MAX_N = 5001 a = [0] * MAX_N raz = [0] * (MAX_N + 10) s = [0] * (MAX_N + 10) n = int(input()) a = list(map(int, input().split())) for i in range(n): for j in range(n): if a[i] - a[j] > 0: raz[a[i] - a[j]] += 1 for i in range(1, MAX_N + 1): s[i] = s[i - 1] + raz[i] ans = 0 for i in range(1, MAX_N): if raz[i] == 0: continue for j in range(1, MAX_N): if i + j > MAX_N: break if raz[j] == 0: continue ans += raz[i] * raz[j] * (s[MAX_N] - s[i + j]) ans = ans * 1.0 ans /= s[MAX_N] ans /= s[MAX_N] ans /= s[MAX_N] print(ans)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
from itertools import accumulate n = int(input()) A = [int(x) for x in input().split()] m = max(A) difference = [(0) for i in range(m)] for i in range(n): for j in range(i + 1, n): difference[abs(A[i] - A[j])] += 1 diffsum = [(0) for i in range(2 * m)] for i in range(m): for j in range(m): diffsum[i + j] += difference[i] * difference[j] D = list(accumulate(diffsum)) ans = sum(difference[k] * D[k - 1] for k in range(m)) print(8 * ans / (n * (n - 1)) ** 3)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number. -----Output----- Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 1 2 Output 0.0000000000 Input 3 1 2 10 Output 0.0740740741 -----Note----- In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\frac{1}{3} \cdot \frac{1}{3} \cdot \frac{2}{3} = \frac{2}{27}$.
from itertools import accumulate R = lambda: map(int, input().split()) n = int(input()) arr = sorted(R()) ones = [0] * 5005 for i in range(n): for j in range(i): ones[abs(arr[i] - arr[j])] += 1 twos = [0] * 10005 for i in range(1, 5001): for j in range(1, 5001): if ones[i] and ones[j]: twos[i + j] += ones[i] * ones[j] stwos = list(accumulate(twos)) sat, sm = 0, 0 for i in range(1, 5001): if ones[i]: sat += ones[i] * stwos[i - 1] sm += ones[i] * stwos[-1] print(sat / max(1, sm))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
s = input() n = len(s) isPalindrome = [[(False) for i in range(n)] for j in range(n)] for i in range(n): isPalindrome[i][i] = True for i in range(n - 1, -1, -1): for j in range(i + 1, n): if j - i == 1: isPalindrome[i][j] = s[i] == s[j] else: isPalindrome[i][j] = s[i] == s[j] and isPalindrome[i + 1][j - 1] pairs = [0] * n palindromes = [0] * n palindromes[0] = 1 for i in range(1, n): t = 0 for j in range(1, i + 1): if isPalindrome[j][i]: pairs[i] += palindromes[j - 1] t += 1 if isPalindrome[0][i]: t += 1 pairs[i] += pairs[i - 1] palindromes[i] = palindromes[i - 1] + t print(pairs[n - 1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
vv = lambda s: ord(s) - ord("a") + 1 zz = input() s = list(map(vv, zz)) prim = 31 mod = 10**9 + 7 ln = len(s) prefix = [0] * ln pwrs = [0] * (ln + 1) pwrs[0] = 1 for n in range(1, ln + 1): pwrs[n] = pwrs[n - 1] * prim % mod prefix[0] = s[0] for n in range(1, ln): prefix[n] = (prefix[n - 1] + s[n] * pwrs[n]) % mod def bnp(bs, pw): res = 1 while pw: if pw & 1: res *= bs res %= mod bs *= bs bs %= mod pw //= 2 return res suffix = [0] * ln suffix[-1] = s[-1] bnps = [bnp(n, mod - 2) for n in pwrs] for n in range(ln - 2, -1, -1): suffix[n] = (suffix[n + 1] + s[n] * pwrs[ln - 1 - n]) % mod prefix = [0] + prefix + [0] suffix = [0] + suffix + [0] BIT = [0] * 10**4 lnt = len(BIT) def up(id, val): while id < lnt: BIT[id] += val id += id & -id def qr(id): res = 0 while id: res += BIT[id] id -= id & -id return res res = 0 for a in range(1, ln + 1): for b in range(a, ln + 1): p = (prefix[b] - prefix[a - 1]) * bnps[a - 1] % mod q = (suffix[a] - suffix[b + 1]) * bnps[ln - b] % mod if p == q: res += qr(a - 1) up(b, 1) print(res)
ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
s = input() n = len(s) L = [0] * n R = [0] * n for i in range(n): x = y = i while x >= 0 and y < n and s[x] == s[y]: R[x] += 1 L[y] += 1 x -= 1 y += 1 for i in range(1, n): if s[i - 1] == s[i]: x, y = i - 1, i while x >= 0 and y < n and s[x] == s[y]: R[x] += 1 L[y] += 1 x -= 1 y += 1 for i in range(n - 1): L[i + 1] += L[i] p = 0 for i in range(1, n): p += L[i - 1] * R[i] print(p)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
t = input() n = len(t) k = n // 2 a, b = [1] * n, [1] * n for x, y in [(i, i + 2) for i in range(k - 1)]: while x >= 0 and t[x] == t[y]: a[y] += 1 b[x] += 1 x -= 1 y += 1 for x, y in [(i, i + 1) for i in range(k)]: while x >= 0 and t[x] == t[y]: a[y] += 1 b[x] += 1 x -= 1 y += 1 for x, y in [(i, i + 2) for i in range(k - 1, n - 1)]: while y < n and t[x] == t[y]: a[y] += 1 b[x] += 1 x -= 1 y += 1 for x, y in [(i, i + 1) for i in range(k, n)]: while y < n and t[x] == t[y]: a[y] += 1 b[x] += 1 x -= 1 y += 1 for i in range(n - 1): a[i + 1] += a[i] print(sum(a[i] * b[i + 1] for i in range(n - 1)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
def answer(n, A): p = [[(0) for i in range(n)] for j in range(n)] for i in range(n): for j in range(n - i): r = j c = i + j if r == c: p[r][c] = 1 elif c == r + 1: if A[c] == A[r]: p[r][c] = 1 elif A[r] == A[c] and p[r + 1][c - 1]: p[r][c] = 1 s = [0] * n s[0] = 1 for i in range(1, n): count = 0 for j in range(0, i + 1): if p[j][i] == 1: count += 1 s[i] = s[i - 1] + count dp = [0] * n for i in range(1, n): count = 0 for j in range(i, 0, -1): count += p[j][i] * s[j - 1] dp[i] = dp[i - 1] + count return dp[-1] s = input() print(answer(len(s), s))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
def ispalindrome(s): if s == s[::-1]: return True return False s = input() n = len(s) dp = [[(0) for i in range(n)] for j in range(n)] for i in range(n): dp[i][i] = 1 for i in range(1, n): for j in range(i - 1, -1, -1): if s[i] == s[j] and (j + 1 > i - 1 or dp[j + 1][i - 1]): dp[j][i] = 1 pref = [0] * n suff = [0] * n pref[0] = suff[-1] = 1 for i in range(1, n): for j in range(i, -1, -1): if dp[j][i]: pref[i] += 1 for i in range(n - 2, -1, -1): suff[i] = suff[i + 1] for j in range(i, n): if dp[i][j]: suff[i] += 1 ans = 0 curr = 0 for i in range(n - 1): ans += suff[i + 1] * pref[i] print(ans)
FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR