hdu 3183 RMQ
返回的是RMQ的下标,有个地方要注意,就是在rmq模板比较大小的地方要改成<=,要不然如果有一连串的相同数字的话本来是要取的,结果没取
#include<string.h> #include<stdio.h> #include<math.h> const int M=1010; int min(int a,int b){return a<b?a:b;} int dp[20][M],LOG[M]; void Make_Rmqindex(int n,char b[]) { int i,j; for(i=1;i<=n;i++) dp[0][i]=i; for(i=1;i<=LOG[n];i++) { int limit=n+1-(1<<i); for(j=1;j<=limit;j++) { int x=dp[i-1][j],y=dp[i-1][j+(1<<i>>1)]; dp[i][j]=b[x]<=b[y]?x:y; } } } int Rmq_Index(int l,int r,char b[]) { int k=LOG[r-l+1]; int x=dp[k][l]; int y=dp[k][r-(1<<k)+1]; return b[x]<=b[y]?x:y; } char s[M]; int m; char ans[M]; int main() { int i,j,len; LOG[0]=-1; for(i=1;i<M;i++) LOG[i]=LOG[i>>1]+1; s[0]='0'; while(scanf("%s%d",s+1,&m)!=EOF) { len=strlen(s)-1; Make_Rmqindex(len,s); memset(ans,0,sizeof(ans)); int n=len-m; int pos=0; int tot=0; while(n) { if(pos+1==len-n+1) { strcpy(ans+tot,s+len-n+1); break; } pos=Rmq_Index(pos+1,len-n+1,s); ans[tot++]=s[pos]; n--; } i=0;//printf("ans:::%s\n",ans); while(ans[i]=='0'&&len-m>i) i++; if(i==len-m) printf("0\n"); else printf("%s\n",ans+i); } return 0; }