Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

出現頻度操作

有限数列の変換操作に出現頻度操作というものがあります。数列 $S = \{s_1, s_2,... s_n\}$ の変換結果は同じ長さの数列となります。その結果を $C = \{c_1,c_2, ..., c_n\}$ とすると、 $c_i$ は数列 $S$ における $s_i$ の個数を表します。

  例えば $S = \{3,4,1,5,9,2,6,5,3\}$ ならば $C = {2,1,1,2,1,1,1,2,2}$ となります。さらにこの数列 $C$ に出現頻度操作を行うと $P = \{4,5,5,4,5,5,5,4,4\}$ を得ます。この数列は出現頻度操作で変わることがありません。このような数列 $P$ を数列 $S$ の不動点と呼びます。どのような数列に対しても出現頻度操作を繰り返せば、その不動点を求めることが出来るということが知られています。

 下の例は出現頻度操作の手順を示したものです。1 行目を数列 $S$ 、2 行目を数列 $C$、最終行を数列 $P$ とします。数列 $S$ の最初の要素($s_1 = 2$) と同じ数は 3 個あるので数列 $C$ の最初の要素 $c_1$ は 3、次の要素 ($s_2 = 7$) と同じ数は 2 個あるので $c_2 = 2$、といった具合に個数を数え $c_i$ を求めていきます。

 数列の長さ $n$ と数列 $S$ を入力し、不動点の数列 $P$ および、$P$ を得るために実行した出現頻度操作の最小の回数を出力するプログラムを作成してください。

Input

複数のデータセットが与えられます。各データセットは以下の形式で与えられます。

$n$

$s_1$ $s_2$ ... $s_n$

1 行目に数列の長さを表す整数 $n$ ($n \leq 12$) が与えられます。２行目に数列 $S$ の要素を表す整数　$s_i$ ($1 \leq s_i \leq 100$) が空白区切りで与えられます。

入力は０一つの行で終わります。データセットの数は 200 を超えません。

Output

各データセットについて、１行目に出現頻度操作の最小の実行回数(整数)、２行目に対応する不動点の数列 $P$ の要素 $p_1$, $p_2$, ...,  $p_n$ を空白区切りで出力してください。

Sample Input

10
4 5 1 1 4 5 12 3 5 4
0

Output for the Sample Input

3
6 6 4 4 6 6 4 4 6 6



Sample input:
10
4 5 1 1 4 5 12 3 5 4
0


Sample output:
3
6 6 4 4 6 6 4 4 6 6

Submission status: Wrong Answer

Solution code:
import sys
class ofa():
 def cal(self,n):
  list1=map(int,raw_input().split())
  dic={}
  #while True:
  x=0
  
  
  while True:
   for i in range(n):    
    if list1[i] not in dic:
     dic[list1[i]]=1
    else :
     dic[list1[i]]+=1
   pre=list(list1)
   for i in range(n):    
    for j in dic.keys():	 
	 if list1[i]==j:	  
	  list1[i]=dic[j]
	  break   
   x+=1   
   if pre==list1:
    break
   dic.clear()
  print x-1
  for i in range(n):
   if i==n-1:
    sys.stdout.write(str(list1[i]))
   else :
    sys.stdout.write(str(list1[i])+" ")

Submission status: Wrong Answer

Solution code:
def f(x):
  a=x
  c=0
  while 1:
    A=list(set(a))
    B=[a.count(e) for e in A]
    b=[B[A.index(e)] for e in a]
    if a==b: return c,a
    a=b
    c+=1
  return

while 1:
  n=input()
  if n==0: break
  c,x= f(map(int,raw_input().split()))
  print c
  for e in x: print e,

Submission status: Wrong Answer

Solution code:
def make_count_dic(array):
    dic = {}
    for n in array:
        if n not in dic:
            dic[n] = 1
        else:
            dic[n] += 1
    return dic
    
def make_new_array(array):
    dic = make_count_dic(array)
    new_array = [] 
    for k in array:
        new_array.append(dic[k])
    return new_array  
    
    
while True:
    try:
        n = int(raw_input())
        if n == 0:
            break
    except EOFError:
        break
    
    array = map(int,raw_input().split())
    
    c = 1
    next_array = make_new_array(array)
    while next_array != array:
        array = next_array
        c +=1
    else:
        array = make_new_array(array)
        c +=1
    print c
    print ' '.join(map(str,array))

Submission status: Wrong Answer

Solution code:
def f(l,c):
  m = [l.count(i) for i in l]
  if l == m:
    print(c)
    m = [str(i) for i in m]
    print(' '.join(m))
    return
  else:
    c += 1
    return f(m,c)
  
while True:
  n = int(input())
  if n == 0:
    break
  l = [i for i in input().split()]
  f(l,0)

Submission status: Wrong Answer

Solution code:
def f(l,c):
  m = [l.count(i) for i in l]
  if l == m:
    print(c)
    m = [str(i) for i in m]
    print(' '.join(m))
    return
  else:
    c += 1
    f(m,c)
  
while True:
  n = int(input())
  if n == 0:
    break
  l = [i for i in input().split()]
  f(l,0)

Submission status: Wrong Answer

Solution code:
def f(l,c):
  m = [l.count(i) for i in l]
  c += 1
  if l == m:
    print(c-1)
    m = [str(i) for i in m]
    print(' '.join(m))
    return
  else:
    f(m,c)
  
while True:
  n = int(input())
  if n == 0:
    break
  l = [i for i in input().split()]
  f(l,0)

Submission status: Wrong Answer

Solution code:
while True:
	n = int(input())
	if n == 0:
	    break
	s = []
	u = 0
	v = 0
	s = list(input().split())
	while True:
		t = []
		for i in range(n):
			for j in range(n):
				if s[i] == s[j]:
					u += 1
			t.append(u)
			t_str = list(map(str, t))
			u = 0
		if s == t:
			print(str(v))
			print(" ".join(t_str))
			break
		else:
			s = t
			v +=1

Submission status: Wrong Answer

Solution code:
while True:
	n = int(input())
	if n == 0:
	    break
	s = []
	u = 0
	v = 0
	s = list(input().split())
	while True:
		t = []
		for i in range(n):
			for j in range(n):
				if s[i] == s[j]:
					u += 1
			t.append(u)
			t_str = list(map(str, t))
			u = 0
		if s == t:
			print(str(v))
			print(" ".join(t_str))
			break
		else:
			s = t
			v += 1

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    pre = [S.count(s) for s in S]
    cur = []
    cnt = 1
    while cnt < n:
        cur = [pre.count(p) for p in pre]
        if cur == pre:
            break
        pre = [c for c in cur]
        cnt += 1
    print(cnt)
    print(*cur)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    pre = [S.count(s) for s in S]
    cur = []
    cnt = 1
    while cnt < n:
        cur = [pre.count(p) for p in pre]
        if cur == pre:
            break
        pre = [c for c in cur]
        cnt += 1
    print(cnt)
    print(' '.join(map(str, cur)))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    pre, cur = [S.count(s) for s in S], []
    cnt = 1
    while True:
        cur = [pre.count(p) for p in pre]
        if cur == pre:
            break
        pre = [c for c in cur]
        cnt += 1
    print(cnt)
    print(' '.join(map(str, cur)))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    pre, cur = [S.count(s) for s in S], []
    cnt = 1
    while pre:
        cur = [pre.count(p) for p in pre]
        pre = 0 if cur == pre else cur[::]
        cnt += 1
    print(cnt-1)
    print(' '.join(map(str, cur)))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    pre, cur = [S.count(s) for s in S], []
    cnt = 0
    while pre:
        cur = [pre.count(p) for p in pre]
        pre = 0 if cur == pre else cur[::]
        cnt += 1
    print(cnt)
    print(' '.join(map(str, cur)))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    pre, cur = [S.count(s) for s in S], []
    cnt = 0
    while pre:
        cur = [pre.count(p) for p in pre]
        pre = 0 if cur == pre else cur[::]
        cnt += 1
    print(str(cnt))
    print(' '.join(map(str, cur)))

Submission status: Wrong Answer

Solution code:
while 1:
 n=int(input())
 if n==0:break
 s=input().split()
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Wrong Answer

Solution code:
while 1:
 if'0'==input():break
 s=input().split()
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Wrong Answer

Solution code:
import sys
for a in sys.stdin:
 if' 'in a:
  s=list(map(int,a.split()))
  c=0
  while 1:
   t=s;s=[t.count(e)for e in t]
   if t==s:break
   c+=1
  print(c);print(*s)

Submission status: Wrong Answer

Solution code:
import sys
for a in sys.stdin:
 if' 'not in a:break
 s=list(map(int,a.split()))
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Wrong Answer

Solution code:
import sys
for a in sys.stdin:
 if' 'in a:
  s=list(map(int,a.strip().split()))
  c=0
  while 1:
   t=s;s=[t.count(e)for e in t]
   if t==s:break
   c+=1
  print(c);print(*s)

Submission status: Wrong Answer

Solution code:
import sys
for a in sys.stdin:
 if'0'not in a:
  s=list(map(int,a.split()))
  c=0
  while 1:
   t=s;s=[t.count(e)for e in t]
   if t==s:break
   c+=1
  print(c);print(*s)

Submission status: Wrong Answer

Solution code:
# AOJ 0108 Operation of Frequency of Appearance
# Python3 2018.6.18 bal4u

f = [0]*105
a = [[0 for j in range(15)] for i in range(2)]
while True:
	n = int(input())
	if n == 0: break
	a[0] = list(map(int, input().split()))
	a[1] = [0]*n
	cnt = k1 = 0
	while True:
		for i in range(n): f[a[k1][i]] += 1
		k2 = 1-k1
		for i in range(n): a[k2][i] = f[a[k1][i]]
		if a[0] == a[1]: break
		for i in range(n): f[a[k1][i]] = 0
		k1 = k2
		cnt += 1
	print(cnt)
	print(*a[0])

Submission status: Wrong Answer

Solution code:
while True:
    n=input()
    if n==0:
        break
    S=list(map(int,raw_input().split()))
    pre,post=[],[]
    cnt=0
    for i in range(n):
        post.append(S.count(S[i]))
    while pre<>post:
        pre=post[:]
        post=[]
        cnt+=1
        for i in range(n):
            post.append(pre.count(pre[i]))
    print cnt
    print " ".join(map(str,post))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit
from itertools import count


def main(readline=stdin.readline):
    readline()

    while 1:
        ary = [int(s) for s in readline().split()]
        if ary == [0]:
            exit()

        for case in count():
            ran = {i: ary.count(i) for i in ary}
            if all(key == cnt for key, cnt in ran.items()):
                break
            ary = [ran[i] for i in ary]

        print(case)
        print(*ary)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit
from itertools import count
from collections import Counter


def main(readline=stdin.readline):
    readline()
    ran = Counter()

    while 1:
        ary = [int(s) for s in readline().split()]
        if ary == [0]:
            exit()

        for case in count():
            ran.clear()
            for i in ary:
                ran[i] += 1
            if all(key == cnt for key, cnt in ran.items()):
                break
            ary = [ran[i] for i in ary]

        print(case)
        print(*ary)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit
from itertools import count
from collections import Counter


def main(readline=stdin.readline):
    readline()
    ran = Counter()

    while 1:
        ary = [int(s) for s in readline().split()]
        if ary == [0]:
            exit()

        for case in count():
            ran.clear()
            for i in ary:
                ran[i] += 1
            ary = [ran[i] for i in ary]
            if all(key == cnt for key, cnt in ran.items()):
                break

        print(case if case else 1)
        print(*ary)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit
from itertools import count
from collections import Counter


def main(readline=stdin.readline):
    readline()
    ran = Counter()

    while 1:
        ary = [int(s) for s in readline().split()]
        if ary == [0]:
            exit()

        for case in count():
            ran.clear()
            for i in ary:
                ran[i] += 1
            tmp = ary[:]
            ary = [ran[i] for i in ary]
            if tmp == ary:
                break

        print(case)
        print(*ary)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def foa(a):
    b = a[:]
    for i in range(len(b)):
        b[i] = a.count(a[i])
    if b == a:
        return b
    else:
        return foa(b)

while True:
    n = int(raw_input())
    if n == 0:
        break
    a = map(int, raw_input().split())
    print " ".join(map(str, foa(a)))

Submission status: Time Limit Exceeded

Solution code:
while True:
	n = int(input())
	if n == 0:
	    break
	s = []
	u = 0
	v = 0
	s = list(input().split())
	while True:
		t = []
		for i in range(n):
			for j in range(n):
				if s[i] == s[j]:
					u += 1
			t.append(u)
			t_str = list(map(str, t))
			u = 0
		if s == t_str:
			print(str(v))
			print(" ".join(t_str))
			break
		else:
			s = t
			v += 1

Submission status: Time Limit Exceeded

Solution code:
import sys
a=sys.stdin
while 1:
 if'0'==a.readline():break
 s=list(map(int,a.readline().split()))
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Accepted

Solution code:
import sys


def _nextstep(s):
    retval=[0 for i in range(len(s))]
    for i,si in enumerate(s):
        retval[i]=s.count(si)
    return retval

def fudouten(s):
    count=0
    while True:
        next=_nextstep(s)
        if next==s:
            return count,s
        else:
            s=next
            count+=1

while True:
    n=int(input())
    if n==0:
        sys.exit()
    s=[int(i) for i in input().split(" ")]
    count,ans=fudouten(s)
    print(count)
    print(" ".join([str(s) for s in ans]))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    else:
        s = list(map(int, input().split(' ')))

    times = 0
    old_nums = [-1] * n
    while old_nums != s:
        new_nums = []
        for i in s:
            new_nums.append(s.count(i))
        old_nums = s[::]
        s = new_nums[::]
        times += 1
    print(times - 1)
    print(' '.join(map(str, s)))

Submission status: Accepted

Solution code:
while True:
    n = input()
    if n == 0:
        break
    S = map(int,raw_input().split())
    S2 = [0] * n
    num = 0
    while True:
        for i in range(n):
            S2[i] = S.count(S[i])
        if S2 == S:
            break
        else:
            S, S2 = S2, S
            num += 1
    print num
    for i in S:
        print i,
    print

Submission status: Accepted

Solution code:
import sys
class ofa():
 def cal(self,n):
  list1=map(int,raw_input().split())
  dic={}
  #while True:
  x=0
  
  
  while True:
   for i in range(n):    
    if list1[i] not in dic:
     dic[list1[i]]=1
    else :
     dic[list1[i]]+=1
   pre=list(list1)
   for i in range(n):    
    for j in dic.keys():	 
	 if list1[i]==j:	  
	  list1[i]=dic[j]
	  break   
   x+=1   
   if pre==list1:
    break
   dic.clear()
  print x-1
  for i in range(n):
   if i==n-1:
    sys.stdout.write(str(list1[i]))
   else :
    sys.stdout.write(str(list1[i])+" ")
  print "" 
if __name__=="__main__":
 while True:
  n=input()
  if n==0:break
  ans=ofa()
  ans.cal(n)

Submission status: Accepted

Solution code:
def f(x):
  a=x
  c=0
  while 1:
    A=list(set(a))
    B=[a.count(e) for e in A]
    b=[B[A.index(e)] for e in a]
    if a==b: return c,a
    a=b
    c+=1
  return

while 1:
  n=input()
  if n==0: break
  c,x= f(map(int,raw_input().split()))
  print c
  print " ".join(map(str,x))

Submission status: Accepted

Solution code:
def f(x):
  a,c=x,0
  while 1:
    b=[a.count(e) for e in a]
    if a==b: return c," ".join(map(str,a))
    else: a,c=b,c+1

while 1:
  if input()==0: break
  c,s=f(map(int,raw_input().split()))
  print c; print s

Submission status: Accepted

Solution code:
def f(x):
  a=x; c=0
  while 1:
    b=[a.count(e) for e in a]
    if a==b: return c," ".join(map(str,a))
    else: a=b; c+=1

while 1:
  if input()==0: break
  c,s=f(map(int,raw_input().split()))
  print c; print s

Submission status: Accepted

Solution code:
while input()!=0:
  a=map(int,raw_input().split())
  c=0
  while 1:
    b=[a.count(e) for e in a]
    if a==b: break
    else: a=b; c+=1
  print c
  for e in a: print e,
  print

Submission status: Accepted

Solution code:
while input()!=0:
  a=map(int,raw_input().split())
  c=0
  while 1:
    b=[a.count(e) for e in a]
    if a==b: break
    else: a=b; c+=1
  print c
  print " ".join(map(str,a))

Submission status: Accepted

Solution code:
while int(raw_input())!=0:
  a=map(int,raw_input().split())
  c=0
  while 1:
    b=[a.count(e) for e in a]
    if a==b: break
    else: a=b; c+=1
  print c
  print " ".join(map(str,a))

Submission status: Accepted

Solution code:
while int(raw_input())!=0:
  a = map(int, raw_input().split())
  c = 0
  while 1:
    b = [a.count(e) for e in a]
    if a==b: break
    else: a=b; c+=1
  print c
  for e in a: print e,
  print

Submission status: Accepted

Solution code:
def f(a,c):
  b = [a.count(e) for e in a]
  if a!=b: b,c=f(b,c+1)
  return b, c

while input()!=0:
  a = map(int,raw_input().split())
  a, c = f(a,0)
  print c
  print " ".join(map(str,a))

Submission status: Accepted

Solution code:
def f(a,c):
  b = [a.count(e) for e in a]
  if a!=b: b,c=f(b,c+1)
  return b, c

while int(raw_input())!=0:
  a = map(int,raw_input().split())
  a,c = f(a,0)
  print c
  print " ".join(map(str,a))

Submission status: Accepted

Solution code:
def f(a, c):
  b = [a.count(e) for e in a]
  if a!=b:
    b, c=f(b, c+1)
  return b, c

while int(raw_input())!=0:
  a = map(int, raw_input().split())
  a, c = f(a,0)
  print c
  print " ".join(map(str, a))

Submission status: Accepted

Solution code:
def getnewline(line,N):
    newline=[0 for x in range(N)]
    dnum={}
    for x in line:
        if x in dnum:
            dnum[x]=dnum[x]+1
        else:
            dnum[x]=1
    for i in range(N):
        newline[i]=dnum[line[i]]
    return newline

while(1):
    N=int(raw_input())
    if N==0:
        break
    else:
        line=[int(x) for x in raw_input().split()]
        n=0
        continuecount=1
        while(continuecount):
            newline=getnewline(line,N)
            continuecount=line!=newline
            line=newline
            n=n+1
        print n-1
        print ' '.join([str(x) for x in line])# your code goes here

Submission status: Accepted

Solution code:
def count(n, ls, c = 0):
    next_ls = [0]*n
    for i in range(n):
       next_ls[i] = ls.count(ls[i])
    if ls == next_ls:
        return c, ls
    else:
        return count(n, next_ls, c+1)

    
while True:
   n = input()
   if n == 0:
       break
   ls = map(int, raw_input().split())
   a = count(n, ls)
   print a[0]
   print ' '.join(map(str, a[1]))

Submission status: Accepted

Solution code:
while True:
    n = input()
    if n == 0: break
    s = [int(x) for x in raw_input().split()]
    cnt = 0
    
    while True:
        c = [0] * n
        for i in range(n):
            c[i] = s.count(s[i])
        if s == c: break
        else:
            s = c
            cnt += 1
    
    print cnt
    print " ".join([str(x) for x in c])

Submission status: Accepted

Solution code:
def ocean(_S):
    global S, count
    count += 1
    while _S:
        a = _S[0]
        n_S = []
        c = 1
        for v in _S[1:]:
            if v == a:
                c += 1
            else:
                n_S.append(v)
        else:
            dic[a] = str(c)
            _S = n_S
    else:
        _S = []
        for v in S:
            _S.append(dic[v])
        else:
            if S == _S:
                print count-1
                print ' '.join(_S)
                return
            else:
                S = _S[:]
                ocean(_S)

while True:
    N = int(raw_input())
    if N == 0: break
    S = raw_input().split(' ')
    dic = {}
    count = 0
    ocean(S[:])

Submission status: Accepted

Solution code:
while True:
	n = input()
	if n == 0:
		break
	
	array = map(int,raw_input().split())
	aray = []
	count = 0
	while array != aray:
		aray = array
		agency = []
		for i in xrange(n):
			agency.append(array.count(array[i]))
		array = agency
		count += 1
		
	print count - 1
	
	string = ""
	for var in array:
		string += str(var) + " "
		
	print string[:len(string) - 1]

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    a = [int(x) for x in input().split()]
    cnt = 0
    while True:
        b = {}
        for i in a:
            if i in b:
                b[i] += 1
            else:
                b[i] = 1
        c = 0
        for k, v in b.items():
            if k == v:
                c += 1
        if len(b) == c:
            break
        else:
            for i in range(n):
                a[i] = b[a[i]]
            cnt += 1
    print(cnt)
    print(*a)

Submission status: Accepted

Solution code:
while True:
	n = int(input())
	if n == 0:
	    break
	s = []
	u = 0
	v = 0
	s = list(map(int, input().split()))
	while True:
		t = []
		for i in range(n):
			for j in range(n):
				if s[i] == s[j]:
					u += 1
			t.append(u)
			t_str = list(map(str, t))
			u = 0
		if s == t:
			print(str(v))
			print(" ".join(t_str))
			break
		else:
			s = t
			v += 1

Submission status: Accepted

Solution code:
def FOA(a, count):
	b = [a.count(a[i]) for i in range(len(a))]
	if  b == a:
		print(count)
		print(" ".join(map(str, b)))
		return
	else:
		return FOA(b, count+1)

while True:
	n = int(input())
	if n == 0:
		break
	a = list(map(int, input().split()))
	FOA(a, 0)

Submission status: Accepted

Solution code:
def FOA(a, count):
	b = [a.count(a[i]) for i in range(len(a))]
	if  b == a:
		print(count)
		print(" ".join(map(str, b)))
	else:
		return FOA(b, count+1)

while True:
	n = int(input())
	if n == 0:
		break
	a = list(map(int, input().split()))
	FOA(a, 0)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    Num_lis = list(map(int,input().split()))
    New_lis = []
    num = 0
    while True:
        for i in range(n):
            New_lis.append(Num_lis.count(Num_lis[i]))
        if Num_lis == New_lis:
            break
        num += 1
        Num_lis = New_lis
        New_lis = []
    print(num)
    ans = str(Num_lis[0])
    for j in range(1,n):
        ans = ans + " " + str(Num_lis[j])
    print(ans)

Submission status: Accepted

Solution code:
def f(a,c):
    b=[a.count(i) for i in a]
    if b==a:print(c);print(' '.join(map(str,b)));return
    f(b,c+1) 
while 1:
    n=int(input())
    if not n:break
    a=list(map(int,input().split()))
    f(a,0)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    S = list(map(int, input().split()))
    cnt = 0
    while S:
        cur = [S.count(s) for s in S]
        if cur == S:
            break
        cnt += 1
        S = cur[::]
    print(cnt)
    print(' '.join(map(str, cur)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

def freq_op(A):
    B = []
    for a in A:
        B.append(A.count(a))
    return B

for s in sys.stdin:
    n = int(s)
    if n == 0:
        break

    A = list(map(int, input().split()))

    cnt = 0
    while True:
        B = freq_op(A)
        cnt += 1
        if B == A:
            break
        else:
            A = B

    print(cnt - 1)
    print(*A)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def c2f(data):
    num = 0
    while True:
        counter = Counter(data)
        next = [counter[item] for item in data]
        if data == next:
            break
        num += 1
        data = next

    return num, data

while True:
    num = int(sys.stdin.readline())
    if num == 0:
        break
    line = sys.stdin.readline()
    data = [int(item) for item in line.split(' ')]

    num, result = c2f(data)
    print(num)
    print(*result, sep = ' ')

Submission status: Accepted

Solution code:
# Aizu Problem 0108: Operation of Frequency of Appearance

#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def operation_of_frequency_of_appearance(S):
    S_old = S[:]
    steps = 0
    while True:
        unique = list(set(S))
        counts = {u: S.count(u) for u in unique}
        S = [counts[s] for s in S]
        if S == S_old:
            return steps, S
        S_old = S[:]
        steps += 1

    
while True:
    N = int(input())
    if N == 0:
        break
    steps, S = operation_of_frequency_of_appearance([int(_) for _ in input().split()])
    print(steps)
    print(' '.join([str(s) for s in S]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0108

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        # S = [4, 5, 1, 1, 4, 5, 12, 3, 5, 4]
        n = int(input())
        if n == 0:
            break
        S = [int(x) for x in input().split(' ')]

        A = []                  #  After[]
        B = S[:]                #  Before[]
        count = 0
        while True:
            for s in B:
                A.append(B.count(s))
            if A == B:
                break
            B = A[:]
            A = []
            count += 1

        print(count)
        print(*A)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0108
AC
"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        # S = [4, 5, 1, 1, 4, 5, 12, 3, 5, 4]
        n = int(input())
        if n == 0:
            break
        S = [int(x) for x in input().split(' ')]

        A = []                  #  After[]
        B = S[:]                #  Before[]
        count = 0
        while True:
            # for s in B:
            #    A.append(B.count(s))
            A = [B.count(b) for b in B]
            if A == B:
                break
            B = A[:]
            A = []
            count += 1

        print(count)
        print(*A)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    a = list(map(int, input().split()))
    b = []
    cnt = 0
    while True:
        for m in a:
            b.append(a.count(m))
        if a == b:
            break
        cnt += 1
        a = b
        b = []
    print(cnt)
    print(' '.join(map(str,b)))

Submission status: Accepted

Solution code:
import sys
#from me.io import dup_file_stdin

#@dup_file_stdin
def solve():
    while True:
        n = int(sys.stdin.readline())
        if n == 0 : return
        a = list(map(int,sys.stdin.readline().split(' ')))
        ops = 0
        while True:
            cnt = dict()
            for x in a:
                cnt[x] = cnt.get(x,0) + 1
            changed = False
            for x in a:
                if x != cnt[x]:
                    changed = True
                    break
            if changed:
                a = [cnt[x] for x in a]
                ops += 1
            else:
                print(ops)
                print(' '.join(map(str,a)))
                break
solve()

Submission status: Accepted

Solution code:
while 1:
	n=int(input())
	if n==0:
		break
	s=list(map(int,input().split()))
	cont=0
	while 1:
		tmp=[]
		for i in range(n):
			tmp.append(s.count(s[i]))
		if s==tmp:
			print(cont)
			break
		else:
			s=tmp
			cont+=1
	for i in range(n):
		if i != n-1:
			print(s[i],end=' ')
		else:
			print(s[i])

Submission status: Accepted

Solution code:
output = []

while True:
    length = int(input())

    if length == 0:
        break

    oldList = [int(item) for item in input().split(" ")]
    operateCount = 0

    while True:

        numberCount = {}
        for item in oldList:
            if item in numberCount:
                numberCount[item] += 1
            else:
                numberCount[item] = 1

        newList = []
        for number in oldList:
            newList.append(numberCount[number])

        if oldList == newList:
            output.append(str(operateCount))
            items = [str(item) for item in newList]
            items = " ".join(items)
            output.append(items)
            break

        operateCount += 1
        oldList = newList

print("\n".join(output))

Submission status: Accepted

Solution code:
while True:
    length = int(input())

    if length == 0:
        break

    oldList = [int(item) for item in input().split(" ")]
    operateCount = 0

    while True:

        numberCount = {}
        for item in oldList:
            if item in numberCount:
                numberCount[item] += 1
            else:
                numberCount[item] = 1

        newList = []
        for number in oldList:
            newList.append(numberCount[number])

        if oldList == newList:
            print(str(operateCount))
            items = [str(item) for item in newList]
            items = " ".join(items)
            print(items)
            break

        operateCount += 1
        oldList = newList

Submission status: Accepted

Solution code:
while 1:
 n=int(input())
 if n==0:break
 s=list(map(int,input().split()))
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Accepted

Solution code:
while 1:
 if'0'==input():break
 s=list(map(int,input().split()))
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Accepted

Solution code:
import sys
a=sys.stdin
while 1:
 if'0\n'==a.readline():break
 s=list(map(int,a.readline().split()))
 c=0
 while 1:
  t=s;s=[t.count(e)for e in t]
  if t==s:break
  c+=1
 print(c);print(*s)

Submission status: Accepted

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

while True:
    N = int(input())
    if N == 0:
        break
    p = [int(i) for i in input().split()]
    cnt = 0

    while True:
        flag = True
        s = [0 for i in range(101)]
        for i in range(N):
            s[p[i]] += 1
        for i in range(N):
            if p[i] != s[p[i]]:
                flag = False
                p[i] = s[p[i]]
        if flag:
            break
        cnt += 1
    print(cnt)
    for i in range(N-1):
        print(p[i], end = " ")
    print(p[N-1])

Submission status: Accepted

Solution code:
from collections import Counter

while True:
  n = int(input())
  if not n:
    break
  
  slst = list(map(int, input().split()))
  temp = [i for i in slst]
  ans = 0

  while True:
    ans += 1
    counter = Counter()
    
    for i in slst:
      counter[i] += 1
    slst = [counter[i] for i in temp]
    
    if temp == slst:
      ans -= 1
      break

    temp = [i for i in slst]
  print(ans)
  print(*slst)

Submission status: Accepted

Solution code:
from collections import Counter

def pr(s):
    c=0
    while 1:
        b=s
        t=Counter(s)
        s=[t[i] for i in s]
        c+=1
        if b==s:break
    return s,c-1

while 1:    
    le=int(input())
    if not le:break
    s=list(map(int,input().split()))
    s,c=pr(s)
    print(c)
    print(" ".join(map(str,s)))

Submission status: Accepted

Solution code:
# AOJ 0108 Operation of Frequency of Appearance
# Python3 2018.6.18 bal4u

f = [0]*105
a = [[0 for j in range(15)] for i in range(2)]
while True:
	n = int(input())
	if n == 0: break
	a[0] = list(map(int, input().split()))
	a[1] = [0]*n
	cnt = k1 = 0
	while True:
		for i in range(n): f[a[k1][i]] += 1
		k2 = 1-k1
		for i in range(n): a[k2][i] = f[a[k1][i]]
		for i in range(n): f[a[k1][i]] = 0
		if a[0] == a[1]: break
		k1 = k2
		cnt += 1
	print(cnt)
	print(*a[0])