Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3183
RMQ (Range minimum/maximum Query) The question is: For series A of length n, answer a number of questions RMQ (A,I,J) (i,j<=n), and return to column A and subscript in I, The smallest (large) value in J, that is, the RMQ problem is the problem of finding the maximum interval.
ST (essentially dynamic planning), O (Nlogn)-O (q) online.
St Algorithm (Sparse Table), in order to maximize the value for example, set d[i,j] for [i,i+2^j-1] The maximum value in this interval, then asked to the [a, b] interval maximum value when the answer is Max (D[a,k], d[b-2^k+1,k]), where K is satisfied 2 ^k<=b-a+1 (i.e. the length) of the largest k, i.e. K=[ln (b-a+1)/LN (2)]. The method of D can be used in dynamic programming, D[i, J]=max (D[i, j-1],d[i+2^ (j-1), j-1]).
The first time to write this type of topic, here is a good blog recommendation, respectively write out the return subscript and return the value of the template, you can watch to learn, Link: http://blog.csdn.net/allenjy123/article/details/6629272
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <cmath> #include <stack> #include <set> #include <vector># Include<algorithm> #define LL Long long#define inf 1<<30using namespace std;const int n=1035;int n,m;int Dp[N] [25]; This array is written in char type, WA to die ... Orz...char S1[n];char ans[n];int Min (int a,int b)//return subscript; {return s1[a]<=s1[b]?a:b;} void Make_rmq ()//returns the subscript rmq;{for (int i=0;i<n;i++) dp[i][0]=i; The initial value of length 0; for (int j=1, (1<<j) <n;j++) {for (int i=0;i+ (1<<J) -1<n;i++) {dp[i][j]=min (dp[i][j-1],dp[i + (1<<j-1)][j-1]); }}}int query_rmq (int s,int v)//query; {int k= (int) ((log ((v-s+1) *1.0))/(log (2.0)));//logarithmic commutation formula for K length; return Min (dp[s][k],dp[v-(1<<k) +1][k]);} int main () {while (~SCANF ("%s%d", s1,&m)) {N=strlen (S1); Stores the length of the input string; make_rmq (); int t=n-m; The length to be output; int q=0,p=0; while (t--) {q=query_rmq (q,m++); The query is labeled as the minimum value of m from subscript Q; (it needs to be well understood here) ans[p++]=s1[q++]; } for (q=0;q<p;q++) if (ans[q]!= ' 0 ') break; if (p==q) printf ("0"); else while (q<p) printf ("%c", ans[q++]); printf ("\ n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu-3183-a Magic lamp-rmq+st (template)