【BZOJ 4212】神牛的养成计划

相关链接

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4212
神犇题解:http://www.cnblogs.com/yousiki/p/6550484.html

解题报告

这题如果没有强制在线,那么我们可以用$Trie + bitset$在$O(\frac{2000000n}{64})$的时间复杂度过
如果强制在线并且$s1,s2$等长,那么我们可以在$O(2000000 \log 2000000)$的时间复杂度过

现在解决原问题,先考虑一个暴力:
先把前缀能匹配上的串找出来,然后我们在其中找出后缀能匹配的串
考虑一下后缀数组:按字典序排序后,前缀能匹配上的一定是一个区间
于是我们可以先建一个正串的$Trie$,用来找出前缀合法的字符串区间
然后我们将反串建成一个持久化$Trie$,每一次用前缀合法的区间再去查后缀即可

另外还有一点$Trick$就是字符串排序:我们可以先将正串建成$Trie$,然后贪心$DFS$
这样排序的复杂度就可以是线性的,总的时间复杂度也就是线性的了

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 2000009;
const int M = 2009;
const int SGZ = 26;

int n,m,tot,beg[M],sz[M],ord[M];
char s1[N];

inline int read() {
	char c=getchar(); int f=1,ret=0;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret * f;
}

class Trie{
	int cnt,ch[N][26],mn[N],mx[N];
	vector<int> id[N];
	public:
		inline void insert(char *s, int len, int ID) {
			int w = 0;
			for (int i=0;i<len;i++) {
				int c = s[i] - 'a';
				if (!ch[w]) ch[w] = ++cnt;
				w = ch[w];
			} 
			id[w].push_back(ID);
		}
		inline void sort(int w, int *arr, int &cur) {
			for (int i=0;i<id[w].size();i++) {
				arr[++cur] = id[w][i];
			}
			for (int i=0;i<SGZ;i++) {
				if (ch[w][i]) {
					sort(ch[w][i], arr, cur);
				}
			}
		}
		inline void mark(int val, char *s, int len) {
			int w = 0;
			for (int i=0;i<len;i++) {
				int c = s[i] - 'a';
				w = ch[w];
				if (!mn[w] || mn[w] > val) mn[w] = val;
				if (!mx[w] || mx[w] < val) mx[w] = val;
			}
		}
		inline void query(char *s, int len, int &l, int &r) {
			int w = 0;
			for (int i=0;i<len;i++) {
				int c = s[i] - 'a';
				if (!ch[w]) {
					l = 1; r = 0;
					return;
				} else {
					w = ch[w];
				}
			}
			l = mn[w]; 
			r = mx[w]; 
		}
	private:
}trie; 

class Persistent_Trie{
	int cnt,root[M],sum[N],ch[N][26];
	public:
		inline void insert(int p, int w, char *s, int len) {
			Insert(root[p], root[w], s, len); 
		}
		inline int query(int l, int r, char *s, int len) {
			if (l > r) return 0;
			int ret = 0, w = root[r]; 
			for (int i=0;i<len;i++) {
				w = ch[w][s[i]-'a']; 
			}
			ret += sum[w];
			w = root[l-1];
			for (int i=0;i<len;i++) {
				w = ch[w][s[i]-'a'];
			}
			ret -= sum[w];
			return ret;
		}
	private:
		void Insert(int p, int &w, char *s, int len) {
			w = ++cnt;
			sum[w] = sum[p] + 1;
			memcpy(ch[w], ch[p], sizeof(ch[w]));
			if (len <= 0) return;
			int c = s[len-1] - 'a'; 
			Insert(ch[p], ch[w], s, len - 1);
		}
}PTE; 

int main() {
	n = read();
	for (int i=1,last=0;i<=n;i++) {
		beg[i] = last;
		scanf("%s", s1+last);
		sz[i] = strlen(s1+last);
		trie.insert(s1+last, sz[i], i);
		last += sz[i];
	}
	tot = 0;
	trie.sort(0, ord, tot);
	for (int i=1;i<=n;i++) {
		trie.mark(i, s1+beg[ord[i]], sz[ord[i]]);
		PTE.insert(i-1, i, s1+beg[ord[i]], sz[ord[i]]);
	}
	m = read(); 
	for (int tt=1,last=0,l,r;tt<=m;tt++) {
		scanf("%s",s1);
		int len = strlen(s1);
		for (int i=0;i<len;i++) {
			s1[i] = (s1[i] - 'a' + last) % SGZ + 'a';
		} 
		trie.query(s1, len, l, r);
		scanf("%s",s1);
		len = strlen(s1);
		for (int i=0,j=len-1;i<j;i++,j--) {
			swap(s1[i], s1[j]);
		}
		for (int i=0;i<len;i++) {
			s1[i] = (s1[i] - 'a' + last) % SGZ + 'a';
		}
		last = PTE.query(l, r, s1, len);
		printf("%d\n",last);
	}
	return 0;
}

75 thoughts to “【BZOJ 4212】神牛的养成计划”

  1. What i don’t understood is in truth how you are now not really much more well-liked than you might be now.
    You’re very intelligent. You recognize thus significantly in terms of
    this topic, made me in my view consider it from so
    many numerous angles. Its like women and men are not interested until it’s
    something to accomplish with Lady gaga! Your own stuffs excellent.
    At all times take care of it up!

  2. I’m extremely inspired together with your writing talents as smartly as with the structure on your blog.
    Is this a paid subject matter or did you
    modify it your self? Either way keep up the nice quality writing, it is uncommon to see a great weblog
    like this one nowadays..

  3. Excellent goods from you, man. I have take note your stuff prior to
    and you are simply too wonderful. I actually like what you’ve bought right here,
    really like what you’re saying and the way in which during which you say it.
    You make it enjoyable and you continue to take care of to stay it wise.
    I can’t wait to read far more from you. That is really
    a terrific site.

  4. Simply want to say your article is as surprising.
    The clarity in your post is simply cool and i can assume you are an expert on this subject.
    Well with your permission let me to grab your feed to keep
    up to date with forthcoming post. Thanks a million and please carry on the gratifying
    work.

  5. I just like the valuable info you supply in your articles.
    I’ll bookmark your blog and test once more here regularly.

    I am rather certain I will be informed a lot of new stuff
    right here! Best of luck for the following!

  6. Excellent post. I was checking continuously this blog and I am impressed!
    Very helpful information specifically the last part 🙂 I care for
    such info much. I was seeking this particular information for a long time.
    Thank you and good luck.

  7. Whats up this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding knowledge so
    I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

  8. I do not know whether it’s just me or if everyone else experiencing issues with your site.
    It appears as if some of the written text on your content
    are running off the screen. Can someone else please provide feedback and
    let me know if this is happening to them as well?

    This may be a problem with my internet browser because I’ve had this happen previously.
    Many thanks

  9. Hello would you mind letting me know which webhost you’re working with?
    I’ve loaded your blog in 3 different web browsers and I must say this blog
    loads a lot quicker then most. Can you recommend a good hosting provider at a honest price?
    Cheers, I appreciate it!

  10. Howdy just wanted to give you a quick heads up.

    The words in your post seem to be running off the screen in Opera.
    I’m not sure if this is a format issue or something to do with browser compatibility
    but I thought I’d post to let you know. The design look great though!
    Hope you get the problem resolved soon. Many thanks

  11. Hey there, I think your website might be having browser compatibility issues.
    When I look at your website in Firefox, 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,
    excellent blog!

  12. Does your website have a contact page? I’m having problems locating it but,
    I’d like to send you an email. I’ve got some creative ideas for your
    blog you might be interested in hearing. Either way, great blog and I look forward to seeing
    it develop over time. natalielise plenty of fish

  13. Hi, i think that i saw you visited my blog so i came to
    “return the favor”.I am trying to find things to improve my site!I suppose its
    ok to use a few of your ideas!!

  14. Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m trying to find things to enhance my site!I suppose its ok to use
    some of your ideas!!

  15. I’m gone to inform my little brother, that he should also go
    to see this blog on regular basis to obtain updated from most recent news.

  16. You are so interesting! I don’t suppose I’ve truly read a single
    thing like that before. So nice to find another person with genuine thoughts on this subject.

    Seriously.. many thanks for starting this up. This website is something that is
    needed on the web, someone with a bit of originality! plenty of fish natalielise

  17. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You obviously know what youre talking about,
    why throw away your intelligence on just
    posting videos to your blog when you could be giving us something enlightening to read?
    natalielise plenty of fish

  18. Attractive portion of content. I simply stumbled upon your web site and in accession capital to say that
    I acquire in fact enjoyed account your blog posts. Any way I
    will be subscribing in your augment or even I success you get entry to
    consistently fast.

  19. Hey this is somewhat of off topic but I was wanting to know
    if blogs use WYSIWYG editors or if you have to manually code
    with HTML. I’m starting a blog soon but have no coding know-how
    so I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

  20. First of all I want to say excellent blog! I had a quick question that I’d like to
    ask if you don’t mind. I was curious to know how you center yourself
    and clear your thoughts before writing. I’ve had a
    tough time clearing my thoughts in getting my thoughts out.
    I do enjoy writing but it just seems like
    the first 10 to 15 minutes tend to be wasted just trying to figure out how
    to begin. Any suggestions or tips? Cheers!

  21. Hi there! This blog post couldn’t be written much better!

    Looking through this article reminds me of my
    previous roommate! He constantly kept talking about this.
    I’ll forward this article to him. Fairly certain he will have a good read.
    Thank you for sharing!

  22. I’m really loving the theme/design of your site. Do you ever run into
    any browser compatibility problems? A couple of my blog
    readers have complained about my website not operating correctly
    in Explorer but looks great in Firefox. Do you have any suggestions to
    help fix this problem?

  23. I’m not sure exactly why but this blog is loading very slow
    for me. Is anyone else having this issue or is it a issue on my end?
    I’ll check back later on and see if the problem still exists.

  24. Hmm is anyone else having problems with the images on this blog loading?
    I’m trying to find out if its a problem on my end or if it’s the
    blog. Any feed-back would be greatly appreciated.

  25. Normally I don’t learn article on blogs, however I wish to say that
    this write-up very pressured me to take a look at and do
    so! Your writing taste has been amazed me. Thank you, very great post.

  26. I blog quite often and I really appreciate your content.
    This great article has really peaked my interest.
    I am going to book mark your site and keep checking for new information about once per week.
    I opted in for your Feed too.

  27. We absolutely love your blog and find a lot
    of your post’s to be precisely what I’m looking for.
    Do you offer guest writers to write content available for you?

    I wouldn’t mind publishing a post or elaborating on most of
    the subjects you write related to here. Again, awesome blog!

  28. Hello! This is kind of off topic but I need some guidance
    from an established blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.

    I’m thinking about setting up my own but I’m not sure where to begin. Do you have any
    points or suggestions? Cheers

  29. Its like you read my mind! You seem to know
    so much about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the message
    home a little bit, but other than that, this is excellent blog.
    A great read. I’ll definitely be back.

  30. Heya i’m for the primary time here. I came across this board and I in finding It really helpful
    & it helped me out a lot. I hope to give something back and help others
    such as you aided me.

  31. Hi there, You have done a great job. I will definitely digg it and
    personally suggest to my friends. I’m confident they will be benefited from this website.

  32. Aw, this was an incredibly nice post. Taking the time and actual effort to make a great article… but what can I say…
    I put things off a whole lot and never seem to get nearly
    anything done.

  33. I’m not sure why but this weblog is loading very
    slow for me. Is anyone else having this issue or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  34. Great post. I was checking continuously this weblog and I’m inspired!

    Extremely helpful information specifically the remaining phase 🙂 I handle such information a
    lot. I used to be looking for this particular info
    for a very long time. Thank you and good luck.

  35. Wow, awesome weblog layout! How long have
    you been blogging for? you made blogging look easy.
    The full look of your website is fantastic, let alone the content material!

  36. Hey just wanted to give you a quick heads up and let you know a few
    of the pictures aren’t loading correctly. 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 outcome.

  37. I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You’re incredible! Thanks!

  38. Good day I am so glad I found your web site, I really found
    you by error, while I was browsing on Yahoo for something else, Anyways I am here now and would just like to say kudos for a fantastic post and a all round thrilling blog (I also love the
    theme/design), I don’t have time to look over it all at the minute but I have saved 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 awesome job.

  39. A person essentially help to make critically articles I would
    state. This is the first time I frequented your website page and
    so far? I surprised with the analysis you made to make this particular submit incredible.
    Great activity!

  40. I’m really loving the theme/design of your website.
    Do you ever run into any web browser compatibility problems?

    A few of my blog audience have complained about my site not working
    correctly in Explorer but looks great in Firefox.

    Do you have any recommendations to help fix this problem?

  41. Neat blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really
    make my blog shine. Please let me know where you got your design. Thank you

  42. Hey there I am so thrilled I found your web site, I really found you by
    mistake, while I was searching on Bing for something else,
    Anyways I am here now and would just like to say cheers for a marvelous post
    and a all round interesting blog (I also love the theme/design), I don’t
    have time to read 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 much more, Please do keep up the excellent work.

Leave a Reply

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