最长方连续方波信号
标题:最长方连续方波信号 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
输入一串方波信号,求取最长的完全连续交替方波信号,并将其输出,如果有相同长度的交替方波信号,输出任一即可,方波信号高位用1标识,低位用0标识,如图:
while True: try: signals = input() if "010" not in signals: print(-1) break sign = "010" for i in range(len(signals)): if sign + "11" in signals and sign + "10" not in signals: prinf(sign) break else: if sign in signals: sign += "10" else: print(sign[:-2]) break except: break // 只完成190
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int n = s.length(); s+="0"; String ans=""; int be=0; while(be<n && s.charAt(be)=='1') be++; if(be>=n){ System.out.println(-1); return; } for(int i=0; i<n; i++){ if(s.charAt(i)=='0'&&s.charAt(i+1)=='0'){ if(i!=be && (i-be+1)%2==1){ int t=0; boolean flag = true; for(int j=be; j<=i;j++){ if(s.charAt(j)-'0'!=t){ flag = false; break; } t=(t+1)%2; } if(flag){ if(ans.length()<(i-be+1)) ans=s.substring(be,i+1); } } be=i+1; } } if(ans.length()>0) System.out.println(ans); else System.out.println(-1); } } // 满分