求求超时了
查看原帖
求求超时了
1060961
Kxzzy_n楼主2024/3/30 21:54
import java.util.*;
public class Main {
	static char[] crr;
    public static void main(String[] args) {    	
    	Scanner scanner=new Scanner(System.in);
    	crr=scanner.next().toCharArray();
    	boolean loop=true;
    	while(loop) {
    		loop=false;
    		for(int i=0;i<crr.length;i++) {
        		int l=0;
        		boolean lopp=false;
        		if(crr[i]=='(') {
        			loop=true;
        			l=i;
        			for(int j=i+1;j<crr.length;j++) {
        				if(crr[j]=='(') l=j;
        				if(crr[j]==')') {
        					check(l,j);
        					lopp=true;
        					break;
        				}
        			}
        		}
        		if(lopp) break;
        	}
    	}
    // 	System.out.println(Arrays.toString(crr));
    	int res=0;
    	int max=0;
    	for(char c:crr) {
    		if(c=='|') {
    			max=Math.max(max, res);
    			res=0;
    		}
    		if(c=='x') {
    			res++;
    		}
    	}
    	System.out.println(Math.max(max, res));
    	
    	
    }
    private static void check(int l,int r) {
		int qian=0;
		int hou=0;
		int ll=l;
		int rr=r;
		while(crr[ll]!='|'&&ll!=r) {
			ll++;
			qian++;
		}
		while(crr[rr]!='|'&&rr!=l) {
			rr--;
			hou++;
		}
		if(r-l==hou&&r-l==qian) {
			crr[l]='-';
			crr[r]='-';
			return;
		}
		if(qian>hou) Arrays.fill(crr, ll,r+1,'-');
		else Arrays.fill(crr,l,ll+1,'-');
		crr[l]='-';
		crr[r]='-';
	}
}

2024/3/30 21:54
加载中...