SPOJ_SUBLEX
生活随笔
收集整理的這篇文章主要介紹了
SPOJ_SUBLEX
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
經(jīng)典題目:給一個(gè)字符串,求字典序第k小的子串是什么。
涉及子串問(wèn)題,上自動(dòng)機(jī)。
首先我們可以用記憶化搜索的方法,求出到達(dá)某一個(gè)狀態(tài)后,能產(chǎn)生多少個(gè)新?tīng)顟B(tài)。
首先,到達(dá)這個(gè)狀態(tài)就不走了,這肯定是一種狀態(tài),然后分別考慮后面的26個(gè)指針就好了。
不過(guò)如果不記憶化肯定是要T的,而且如果用dp好像會(huì)有一點(diǎn)問(wèn)題,因?yàn)闋顟B(tài)轉(zhuǎn)移不是嚴(yán)格的滿足小號(hào)點(diǎn)到大號(hào)點(diǎn)(nq點(diǎn)啦)。
然后就是赤果果的dfs就可以啦。
對(duì)了還有一個(gè)有趣的事情,一開(kāi)始我輸出字符的地方T了,后來(lái)改變字符串的輸出方式,然后就A了。spoj真是奇葩呀。能買個(gè)好點(diǎn)服務(wù)器嗎?
?
召喚代碼君:
?
#include <iostream> #include <cstdio> #include <cstring> #define maxn 222222 using namespace std;int next[maxn][26],pre[maxn],step[maxn],f[maxn]; char s[maxn]; int N,last,n,k; int p,q,np,nq;void insert(int x,int m) {p=last,np=++N,step[np]=m;while (p!=-1 && next[p][x]==0) next[p][x]=np,p=pre[p];last=np;if (p==-1) return;q=next[p][x];if (step[q]==step[p]+1) { pre[np]=q; return; }nq=++N,step[nq]=step[p]+1,pre[nq]=pre[q];for (int i=0; i<26; i++) next[nq][i]=next[q][i];pre[np]=pre[q]=nq;for (;p!=-1 && next[p][x]==q; p=pre[p]) next[p][x]=nq; }int get(int x) {if (f[x]!=0) return f[x];f[x]=1;for (int i=0; i<26; i++) if (next[x][i]) f[x]+=get(next[x][i]);return f[x]; }void output(int pos,int num,int L) {num--;if (num==0){s[L]='\0';printf("%s\n",s+1);return;}for (int i=0; i<26; i++){if (next[pos][i]==0) continue;if (f[next[pos][i]]<num) num-=f[next[pos][i]];else {s[L]='a'+i;output(next[pos][i],num,L+1);return;}} }int main() {pre[0]=-1;scanf("%s",s);for (int i=0; s[i]; i++) insert(s[i]-'a',i+1);f[0]=get(0);scanf("%d",&n);while (n--){scanf("%d",&k);output(0,k+1,1);}return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/lochan/p/3796405.html
總結(jié)
以上是生活随笔為你收集整理的SPOJ_SUBLEX的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql的安装真不爽
- 下一篇: Jsonp 跨域请求实例