PAT刷题:
注意事项:
1,容易超时,用快输,快出
2,数组越界,提前设定范围
3,设置时间,一题不超过1小时
L2-004 这是二叉搜索树吗
前序转后序。
solution:
1:双指针判断 左,有
2:递归,存后序遍历
3.判断镜像
package 天梯赛l2;
//2021年4月10日下午9:49:31
//writer:apple
import java.util.*;
import java.io.*;
import java.math.*;
public class l2004_2 {
static int n;
static LinkedList<Integer> fron=new LinkedList<>();
static LinkedList<Integer> rear=new LinkedList<>();
static boolean mi=false;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=0;i<n;i++)
{
fron.add(s.nextInt());
}
// if(fron.get(1)>=fron.get(0)) mi=true;
check(0,fron.size()-1);
if(rear.size()!=fron.size())
{
mi=true;rear.clear();check(0,fron.size()-1);
}
if(fron.size()!=rear.size())
{
System.out.println("NO");
}
else {
System.out.println("YES");
for(int i=0;i<rear.size();i++) {
if(i!=rear.size()-1)
{
System.out.print(rear.get(i)+" ");
}
else {
System.out.println(rear.get(i));
}
}
}
}
private static void check(int i, int size) {
// TODO Auto-generated method stub
if(i>size) return;
int sta=i+1;
int end=size;
if(mi)
{
while(sta<=size&&fron.get(sta)>=fron.get(i))
sta++;
while(end>i&&fron.get(end)<fron.get(i))
end--;
}
else {
while(sta<=size&&fron.get(sta)<fron.get(i))
sta++;
while(end>i&&fron.get(end)>=fron.get(i))
end--;
}
if(sta!=end+1) return;
check(i+1,sta-1);//左
check(sta,size);//右
rear.add(fron.get(i));
}
}
最长对称子串
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const double eps = 1e-8; //const只是用来定义常量,常量在代码中一直存在,但define是预处理功能,有本质区别
const int maxx = 0x7f7f7f7f;//0x7f7f7f7f表示数据的无穷大
//常用的浮点数比较宏定义:
#define Equ(a,b) ((fabs((a)-(b)))<(eps)) //等于
#define More(a,b) (((a)-(b))>(esp)) //大于
#define Less(a,b) (((a)-(b))<(-esp))//小于
#define MoreEqu(a,b) (((a)-(b))>(-esp))//大于等于
#define LessEqu(a,b) (((a)-(b))<(esp))//小于等于
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )//
//使用了algorithm头文件就可以直接使用max函数;
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define ll long long
#define PI 3.1415926
#define eps 1e-8
#define Conn(x,y) x##y;
int ans=1;
int main()
{
string str="";
getline(cin,str);
int len=str.size();
int a[len][len];
memset(a,0,sizeof(a));
for(int i=0;i<len;i++)
a[i][i]=1;
for(int length=1;length<len;length++)
{
for(int l=0;l<len-length;l++)
{
int r=l+length;
if(str[l]==str[r])
{
if(a[l+1][r-1] == r - l - 1)
{
a[l][r]=a[l+1][r-1]+2;
ans=max(ans,a[l][r]);
}
}
}
}
cout<<ans;
return 0;
}