Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
求测试数据--刚开始超时,修改后就wa了//代码如下 #include<stdio.h> #include<stdlib.h> #define Maxnum 30001 int Num=0; int LargeSerial(char serial[],int flag); int binary(char cserial[],char b[],int i,int k); int main() { char serial[Maxnum]; int i=0,leni,lend,flag=1,len; scanf("%d",&Num); for(i=0;i<Num;i++) { getchar(); serial[i]=getchar(); } serial[Num]='\0'; leni=LargeSerial(serial,flag); flag=0; lend=LargeSerial(serial,flag); len= leni > lend ? leni:lend; printf("%d\n",Num-len); system("pause"); return 1; } int LargeSerial(char serial[],int flag)//flag=1最长递增子序列长度,反之将原字符串逆序后求 { //最长单调递增子序列,也即相当于原字符串的降序 int i,j,k,max=0; char *cserial,b[Maxnum]; if(flag==0) { j=0; cserial=(char*)malloc((Num+2)*sizeof(char)); for(i=Num-1;i>=0;i--) cserial[i]=serial[j++]; cserial[Num]='\0'; } else cserial=serial; b[1]=cserial[0];//b[k]存放长度为K的子序列的最后一个元素 for(i=1,k=1;i<Num;i++) { if(cserial[i]>=b[k]) b[++k]=cserial[i]; else b[binary(cserial,b,i,k)]=cserial[i]; } return k; } int binary(char cserial[],char b[],int i,int k) { if(cserial[i]<b[1]) return 1; else { int high,low,mid; high=k,low=1; while(low<high) { mid=(high+low)/2; if(cserial[i]>=b[mid]) low=mid+1; else high=mid-1; } return low; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator