【POJ 3693】Maximum repetition substring

相关链接

题目传送门:http://poj.org/problem?id=3693
数据生成器:http://paste.ubuntu.com/18156993/

题目大意

给定长度为$n(n \le 10^5)$,问循环次数最多的子串循环多少次
请输出字典序最小的解

解题报告

这是SA的 论文题 + 神题
而且SAM貌似不可捉的样子 (╯-_-)╯╧╧
求符合要求的串已经是身心具疲,这题还要字典序最小,TM恶心到不行 ( ▼-▼ )

还是说一说怎么求符合要求的串吧:
枚举符合要求的串的长度L,那么符合要求的串一定在s[1],s[1+L],s[1+2*L]…..等位置出现(跟今年APIO的交互题有点像)
于是我们就直接height[] + ST表搞定整节循环的部分,这个地方和KMP差不多
只与前面有多少相同的,看起来不好求,但实际上如果对于答案有贡献,那么一定是补足循环节

然后是如何让字典序最小:
一般的题目没这题这么恶心,这题尤其恶心! MD再掀一次桌子都无法压抑内心的悲伤 (╯-_-)╯╧╧
这个好像只能用SA数组从小到大枚举,然后判断
哎呀,考场上要是遇到这个题,一定做不对QAQ

Code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN = 200000+9;
const int SIGMA_SIZE = 26;

char pat[MAXN];

namespace Suffix_Array{
	#define SA Suffix_Array
	int sa[MAXN],height[MAXN],t1[MAXN],t2[MAXN];
	int bot[MAXN],*tmp,*rank,m,n;
	int Tm,len[MAXN],TT,cnt;
	
	namespace Sparse_Table{
		#define ST Sparse_Table
		int MN[MAXN][20];
		
		inline void build(){
			for (int i=1;i<=n;i++) memset(MN[i],0,sizeof(MN[i]));
			for (int i=1;i<=n;i++) MN[i][0] = height[i];
			for (int i=1;i<18;i++){
				for (int j=1;j<=n;j++)
					MN[j][i] = min(MN[j][i-1],MN[j+(1<<(i-1))][i-1]);
			}
		}
		
		inline int query(int l, int r){
			if (r < l) swap(l, r); l++;
			if (l < 1) return -1;
			else {
				int mid=(l+r)/2,k=0;
				while (l+(1<<k)<=mid) k++;
				return min(MN[l][k],MN[r-(1<<k)+1][k]);
			}
		}
	};
	
	inline void GetHeight(){
		for (int i=1;i<=n;i++) if (rank[i] > 1){
			int sta = max(0, height[rank[i-1]]-1);
			int p1 = i+sta, p2 = sa[rank[i]-1]+sta;
			while (pat[p1++]==pat[p2++]) sta++;
			height[rank[i]] = sta;
		}
	}	
	
	inline void build(){
		memset(bot,0,sizeof(bot));
		memset(height,0,sizeof(height));
		tmp = t1; rank = t2;
		n = strlen(pat+1); m = SIGMA_SIZE;
		
		for (int i=1;i<=n;i++) bot[tmp[i]=pat[i]-'a'+1]++;
		for (int i=2;i<=m;i++) bot[i] += bot[i-1];
		for (int i=1;i<=n;i++) sa[bot[tmp[i]]--] = i;
		rank[sa[1]] = m = 1;
		for (int i=2;i<=n;i++) rank[sa[i]]=(tmp[sa[i]]==tmp[sa[i-1]])?m:++m;
		
		for (int k=1;k<=n;k*=2){ int T=0;
			for (int i=n-k+1;i<=n;i++) tmp[++T] = i;
			for (int i=1;i<=n;i++) if (sa[i]>k) tmp[++T] = sa[i]-k;
			for (int i=1;i<=m;i++) bot[i] = 0;
			for (int i=1;i<=n;i++) bot[rank[i]]++;
			for (int i=2;i<=m;i++) bot[i] += bot[i-1];
			for (int i=n;i;i--) sa[bot[rank[tmp[i]]]--] = tmp[i];
			
			swap(rank, tmp);
			rank[sa[1]] = m = 1;
			for (int i=2;i<=n;i++) if (tmp[sa[i-1]]==tmp[sa[i]] && tmp[sa[i-1]+k]==tmp[sa[i]+k]) rank[sa[i]] = m; else rank[sa[i]] = ++m; if (m >= n) break;
		}
		
		GetHeight();
		ST::build();
	}
	
	inline void solve(){
        Tm = 1; 
        for (int k=1;k<=n;k++){
            for (int i=1;i<=n;i+=k){ 
				int sta = ST::query(rank[i],rank[i+k]),T = sta / k + 1; 
				if (sta % k && ST::query(rank[i-k+sta%k],rank[i+sta%k]) >= k-sta%k) T++;
                if (T > Tm){Tm = T; len[cnt=1] = k;}
                if (T == Tm) {len[++cnt] = k;}
            } 
        }
        if (Tm==1) printf("Case %d: %c\n",++TT,pat[sa[1]]);
        else {
            for (int i=1;i<=n;i++) for (int j=1;j<=cnt;j++) {
				if (ST::query(i,rank[sa[i]+len[j]]) >= (Tm-1)*len[j]) {
                    pat[sa[i]+Tm*len[j]] = 0;
                    printf("Case %d: %s\n",++TT,pat+sa[i]); 
                    return;
                }   
			}
        }
    }
};

int main(){
	while (scanf("%s",pat+1) && pat[1] != '#'){
		SA::build();
		SA::solve();
	}
	return 0;
} 

95 thoughts to “【POJ 3693】Maximum repetition substring”

  1. I do trust all the concepts you’ve offered for your post.

    They’re really convincing and can definitely work. Still, the posts
    are too short for starters. Could you please prolong them a bit from next
    time? Thanks for the post.

  2. This design is spectacular! You definitely know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Excellent job. I really enjoyed what you had to say, and more than that, how you
    presented it. Too cool!

  3. Hi there are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog?
    Any help would be greatly appreciated!

  4. Heya just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and
    both show the same results.

  5. Good day! This is my 1st comment here so I just wanted to give a quick shout
    out and tell you I genuinely enjoy reading your blog posts.
    Can you recommend any other blogs/websites/forums that deal with the same topics?
    Thank you so much!

  6. Undeniably imagine that which you said. Your favourite justification seemed to be on the web the easiest thing to take into
    account of. I say to you, I certainly get annoyed even as other people consider
    issues that they just do not understand about. You managed to hit the
    nail upon the top and defined out the entire thing with no need
    side effect , folks could take a signal. Will likely be back to
    get more. Thanks

  7. You actually make it seem so easy with your presentation but
    I find this matter to be really something that I think I would never understand.
    It seems too complex and extremely broad for me. I’m looking forward for your next post,
    I’ll try to get the hang of it!

  8. Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point. You clearly know what youre talking
    about, why throw away your intelligence on just posting videos to your site when you
    could be giving us something informative to read?

  9. I like the helpful info you provide for your articles. I’ll bookmark your weblog and take
    a look at again here frequently. I’m somewhat sure I will learn lots of new stuff right
    right here! Good luck for the following!

  10. This design is wicked! You definitely know how to keep a reader amused.

    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Excellent job. I really loved what you had to say,
    and more than that, how you presented it. Too cool!
    plenty of fish natalielise

  11. Hmm it looks like your site ate my first comment (it
    was extremely long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new
    to the whole thing. Do you have any points for newbie blog writers?
    I’d really appreciate it.

  12. Ahaa, its fastidious dialogue regarding this piece of writing at this place at this web site,
    I have read all that, so at this time me also commenting at this place.

  13. Hi, I think your blog might be having browser compatibility issues.

    When I look at your website in Opera, it looks fine but when opening
    in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up!
    Other then that, terrific blog!

  14. Superb blog you have here but I was wanting to know if you knew of any
    forums that cover the same topics talked about in this article?
    I’d really like to be a part of community where I can get responses from other experienced individuals that share the same interest.
    If you have any recommendations, please let me know.
    Thanks!

  15. Good day very nice site!! Guy .. Beautiful .. Wonderful
    .. I’ll bookmark your web site and take the feeds also?

    I’m happy to find numerous helpful information right
    here within the publish, we’d like develop more techniques on this regard, thanks for sharing.
    . . . . .

  16. May I simply say what a relief to find somebody that
    genuinely understands what they’re discussing on the internet.

    You actually realize how to bring a problem to light and make it important.
    A lot more people should read this and understand this side of the story.
    I was surprised that you’re not more popular given that you
    surely possess the gift.

  17. You can certainly see your skills in the work you write. The world hopes for
    even more passionate writers such as you who aren’t afraid to say how they believe.
    At all times follow your heart.

  18. Hi there! Someone in my Myspace group shared this website with us so I came to check it out.

    I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my
    followers! Great blog and superb style and design.

  19. I’m truly enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more pleasant
    for me to come here and visit more often. Did you hire out a designer
    to create your theme? Outstanding work!

  20. I’m really impressed with your writing skills as well as with the
    layout on your weblog. Is this a paid theme or did you customize it yourself?
    Anyway keep up the excellent quality writing,
    it’s rare to see a great blog like this one today.

  21. Excellent post. Keep writing such kind of info on your blog.
    Im really impressed by it.
    Hi there, You have performed an incredible job.

    I’ll certainly digg it and in my opinion suggest to my friends.
    I’m confident they’ll be benefited from this web site.

  22. My programmer is trying to persuade me to move to .net
    from PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am concerned
    about switching to another platform. I have heard very good things about blogengine.net.
    Is there a way I can transfer all my wordpress content
    into it? Any kind of help would be really appreciated!

  23. Hi would you mind sharing which blog platform you’re working
    with? I’m going to start my own blog in the near future but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different
    then most blogs and I’m looking for something completely unique.
    P.S Apologies for being off-topic but I
    had to ask!

  24. Hi, I do believe this is a great blog. I stumbledupon it 😉 I am going to come back yet again since I book marked it.
    Money and freedom is the greatest way to change, may you be rich and continue to guide other people.

  25. excellent put up, very informative. I wonder
    why the opposite specialists of this sector don’t understand this.

    You should proceed your writing. I’m confident, you’ve a great readers’
    base already!

  26. Hi there just wanted to give you a quick heads up.
    The words in your post seem to be running off the screen in Chrome.

    I’m not sure if this is a formatting issue
    or something to do with internet browser compatibility but I thought
    I’d post to let you know. The layout look great though! Hope you get the problem solved soon.
    Many thanks

  27. Hmm it looks like your site ate my first comment (it was
    extremely long) so I guess I’ll just sum it up what
    I submitted and say, I’m thoroughly enjoying your
    blog. I as well am an aspiring blog writer but I’m still new to everything.
    Do you have any recommendations for inexperienced blog writers?
    I’d definitely appreciate it.

  28. Howdy I am so thrilled I found your blog, I really found you by error, while I was researching on Askjeeve for
    something else, Nonetheless I am here now and would just
    like to say cheers for a remarkable post and a all round entertaining blog (I also love the theme/design), I don’t have time
    to go through it all at the minute but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do
    keep up the great jo.

  29. I’m not that much of a internet reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come
    back in the future. Many thanks

  30. Undeniably believe that that you said. Your favourite reason seemed to be
    on the web the easiest factor to be mindful of. I say to you, I definitely get
    irked even as other folks consider worries that they
    plainly do not know about. You controlled to hit the
    nail upon the top and defined out the entire thing with no need side-effects ,
    other people could take a signal. Will likely be back to get more.
    Thanks

  31. Attractive section of content. I just stumbled upon your site and in accession capital to assert that I
    acquire in fact enjoyed account your blog posts. Anyway I’ll be subscribing
    to your feeds and even I achievement you access consistently fast.

  32. I’ve been surfing online more than three hours today,
    yet I never found any interesting article like yours. It is pretty worth enough for me.
    In my opinion, if all site owners and bloggers made good content as you
    did, the net will be a lot more useful than ever before.

  33. Hello, Neat post. There is a problem along with your website in internet explorer,
    might check this? IE still is the marketplace leader
    and a large portion of other folks will leave out your wonderful writing because of this problem.

  34. You really make it seem so easy with your presentation but I find this topic to be really something
    that I think I would never understand. It seems too
    complex and extremely broad for me. I’m looking forward for your next post, I’ll try to get the
    hang of it!

  35. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  36. We’re a group of volunteers and opening a new scheme in our community.
    Your website offered us with valuable info to work on. You’ve done an impressive
    job and our entire community will be thankful to you.

  37. Excellent beat ! I would like to apprentice while you amend your site,
    how could i subscribe for a blog web site? The account helped me a acceptable deal.
    I had been tiny bit acquainted of this your broadcast offered bright clear concept

  38. Hi, I do think this is an excellent site. I stumbledupon it 😉 I will revisit once again since i have book marked it.
    Money and freedom is the greatest way to change,
    may you be rich and continue to help other people.

  39. I’m extremely inspired with your writing abilities as neatly as with the structure to your weblog.
    Is this a paid topic or did you customize it yourself?
    Either way stay up the nice quality writing, it is rare to look a nice blog
    like this one these days..

  40. Good post. I learn something new and challenging on websites I stumbleupon on a daily basis.
    It will always be helpful to read articles from other writers
    and practice a little something from their web sites.

  41. I’m extremely impressed together with your writing abilities and also with the structure to your weblog.
    Is this a paid theme or did you customize it your self?
    Anyway stay up the nice high quality writing, it’s
    uncommon to peer a nice blog like this one nowadays..

  42. hi!,I love your writing very a lot! percentage we be in contact more approximately your post on AOL? I require an expert on this house to solve my problem. May be that is you! Having a look ahead to look you.

  43. Fantastic web site. Plenty of helpful information here.

    I’m sending it to some friends ans additionally sharing in delicious.
    And certainly, thanks for your effort!

  44. all the time i used to read smaller content that also clear
    their motive, and that is also happening
    with this article which I am reading at this place.

  45. Hey there! Someone in my Facebook group shared this website
    with us so I came to check it out. I’m definitely enjoying the information. I’m book-marking
    and will be tweeting this to my followers! Fantastic blog and outstanding design and style.

Leave a Reply

Your email address will not be published. Required fields are marked *