結果

問題 No.1694 ZerOne
ユーザー lam6er
提出日時 2025-03-26 15:57:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,105 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 82,532 KB
実行使用メモリ 107,380 KB
最終ジャッジ日時 2025-03-26 15:58:45
合計ジャッジ時間 4,023 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other TLE * 1 -- * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def count_valid_swaps(s):
    visited = set()
    q = deque([s])
    visited.add(s)
    
    n = len(s)
    while q:
        current = q.popleft()
        # Find all possible t and u pairs
        # t is [i, j], u is [k, l], j < k
        # Check all possible pairs
        for i in range(n):
            for j in range(i, n):
                t0 = current[i:j+1].count('0')
                t1 = (j - i + 1) - t0
                # Now find u after j+1
                for k in range(j+1, n):
                    for l in range(k, n):
                        u0 = current[k:l+1].count('0')
                        u1 = (l - k + 1) - u0
                        if t0 == u0 and t1 == u1:
                            # Swap t and u
                            new_str = current[:i] + current[k:l+1] + current[j+1:k] + current[i:j+1] + current[l+1:]
                            if new_str not in visited:
                                visited.add(new_str)
                                q.append(new_str)
    return len(visited)

s = input().strip()
print(count_valid_swaps(s))
0