分享一下4point的错误
查看原帖
分享一下4point的错误
427288
ydcwp楼主2021/3/26 15:55

1.是相邻,所以不要取模 2.注意dp[][]为0的含意,是不纯在段数不是值为0

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

class InputReader{
	private final InputStream stream;
	private final byte[] buf=new byte[500000];
	private int curChar,snumChars;
	public InputReader(InputStream st) {
		this.stream=st;
	}
	public int read() {
		if(snumChars==-1)
			throw new InputMismatchException();
		if(curChar>=snumChars) {
			curChar=0;
			try {
				snumChars=stream.read(buf);
			}catch (IOException e) {
				// TODO: handle exception
				throw new InputMismatchException();
			}
			if(snumChars<=0)
				return -1;
		}
		return buf[curChar++];
	}
	public int nextInt() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		int sgn=1;
		if(c=='-') {
			sgn=-1;
			c=read();
		}
		int res=0;
		do {
			res*=10;
			res+=c-'0';
			c=read();
		}while(!isSpaceChar(c));
		return res*sgn;
	}
	public long nextLong() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		int sgn=1;
		if(c=='-') {
			sgn=-1;
			c=read();
		}
		int res=0;
		do {
			res*=10;
			res+=c-'0';
			c=read();
		}while(!isSpaceChar(c));
		return res*sgn;
	}
	public int[] nextIntArray(int n) {
		int a[]=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=nextInt();
		}
		return a;
	}
	public String readString() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isSpaceChar(c));
		return res.toString();
	}
	public String nextLine() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isEndOfLine(c));
		return res.toString();
	}
	private boolean isEndOfLine(int c) {
		// TODO Auto-generated method stub
		return c=='\n'||c=='\r'||c==-1;
	}
	private boolean isSpaceChar(int c) {
		// TODO Auto-generated method stub
		return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
	}
}

public class Main{
	static final int N=300;
	public static void main(String[] args){
		InputReader in=new InputReader(System.in);
		int[][] dp=new int[N][N];
		int n,k,ans;
		int s1,e1,s2,e2;
		
		n=in.nextInt();
		ans=0;
		for(k=0;k<n;k++) {
			dp[0][k]=in.nextInt();
			ans=Math.max(ans,dp[0][k]);
		}
		
		for(k=1;k<n;k++) {//枚举段
			for(s1=0;s1+k<n;s1++) {//枚举开始
				e2=s1+k;
				for(e1=s1;e1<e2;e1++){//枚举断开的位置,包含j
					s2=(e1+1);
					if(dp[e1-s1][s1]>0&&dp[e1-s1][s1]==dp[e2-s2][s2]){
						dp[k][s1]=Math.max(dp[k][s1],dp[e1-s1][s1]+1);
						ans=Math.max(ans,dp[k][s1]);
					}
				}
			}
		}
		System.out.println(ans);	
	}
}
2021/3/26 15:55
加载中...