bzoj 4319 cerc2008 Suffix reconstruction——贪心构造
生活随笔
收集整理的這篇文章主要介紹了
bzoj 4319 cerc2008 Suffix reconstruction——贪心构造
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:https://www.lydsy.com/JudgeOnline/problem.php?id=4319
如果字符集有 5e5 那么大的話,挨個填上去就行了。但只有26個字符,所以要貪心地盡量填和上一次一樣的字符。
按 sa[ ] 的順序從小到大填字符,判斷這個位置 x 能否填上一次在 y 填的字符的條件就是 rk[ x+1 ] > rk[ y+1 ]。
因為 x 的字典序比 y 大,而 x 位置要填和 y 一樣的字符,所以 x 的后面應該比 y 大。發現后面部分也是兩個后綴,所以利用“接下來會使它們合法”這一限制,就能判斷 x 位置填什么了。
自己的思路有待鍛煉!要嘗試歸納限制條件!
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=5e5+5; int n,sa[N],rk[N]; char a[N]; int rdn() {int ret=0;bool fx=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();return fx?ret:-ret; } int main() {n=rdn();for(int i=1;i<=n;i++)sa[i]=rdn(),rk[sa[i]]=i;int i,p=0;a[sa[1]]=0;for(i=2;i<=n;i++){if(rk[sa[i]+1]>rk[sa[i-1]+1])a[sa[i]]=p;else {a[sa[i]]=++p;if(p==26)break;}}if(i<=n)puts("-1");else {for(int i=1;i<=n;i++)putchar(a[i]+'a');puts("");}return 0; }?
轉載于:https://www.cnblogs.com/Narh/p/10079748.html
總結
以上是生活随笔為你收集整理的bzoj 4319 cerc2008 Suffix reconstruction——贪心构造的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring data jpa upd
- 下一篇: SSL-ZYC 溜冰