【BZOJ 4567】[Scoi2016] 背单词

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4567

这个题,就AC自动机搞一搞就可以啦!
省选时的我都能A,这是真简单啊!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define AC AC_Automata
#define LL long long
using namespace std;

LL vout = 0;
char pat[510000+9];

namespace AC_Automata{
	const int N = 510000+9;
	const int SIGMA_SIZE = 26;
	struct Node{int ch[SIGMA_SIZE],fail,last,vis;}p[N];
	int cnt,que[N],itr1,itr2;
	vector<int> G[N];
	
	int head[N],to[N],nxt[N];
	int TOT,tot,sz[N];
	
	inline void AddEdge(int u, int v){to[++TOT] = v; nxt[TOT] = head[u]; head[u] = TOT;}
	
	inline void insert(char *s){
		int len = strlen(s), w=0;
		for (int i=0;i<len;i++){
			int c = s[i] - 'a';
			if (!p[w].ch) w = p[w].ch = ++cnt;
			else w = p[w].ch;
		} p[w].vis++;
	}
	
	inline void GetFail(){
		itr1 = 0; itr2 = 1;
		for (int i=0;i<SIGMA_SIZE;i++)
			if (p[0].ch[i]) que[++itr1] = p[0].ch[i];
		
		while (itr2 <= itr1){
			int w = que[itr2++];
			if (p[w].vis) AddEdge(p[w].last, w);
			for (int i=0;i<SIGMA_SIZE;i++){
				if (p[w].ch[i]) {
					int nw = p[w].fail;
					while (nw && !p[nw].ch[i]) nw = p[nw].fail;
					p[p[w].ch[i]].fail = p[nw].ch[i];
					p[p[w].ch[i]].last = p[p[nw].ch[i]].vis ? p[nw].ch[i] : p[p[nw].ch[i]].last;
					que[++itr1] = p[w].ch[i];
				}
			} 
		}
	}
	
	void DFS(int w){
		sz[w] = 1;
		for (int i=head[w];i;i=nxt[i]) DFS(to[i]), sz[w] += sz[to[i]], G[w].push_back(sz[to[i]]);
		int len = G[w].size();
		sort(G[w].begin(), G[w].end());
		for (int i=0,sta=0;i<len;i++) vout += (LL)sta + 1LL, sta += (LL)G[w][i];
	}
};

int main(){
	int n; scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%s",pat), AC::insert(pat);
	AC::GetFail(); AC::DFS(0);
	printf("%lld",vout);
	return 0;
}

152 thoughts to “【BZOJ 4567】[Scoi2016] 背单词”

  1. Undeniably believe that which you stated. Your favorite reason seemed to be on the web the easiest thing
    to be aware of. I say to you, I definitely get annoyed while people think about worries
    that they plainly don’t know about. You managed to
    hit the nail upon the top and defined out the whole thing without having side-effects , people could take
    a signal. Will probably be back to get more. Thanks

  2. fantastic submit, very informative. I wonder why the opposite experts
    of this sector do not notice this. You should proceed your writing.
    I’m sure, you’ve a great readers’ base already!

  3. You can certainly see your skills in the article you write.
    The world hopes for more passionate writers such as
    you who are not afraid to say how they believe.
    Always follow your heart.

  4. Please let me know if you’re looking for a article writer for your weblog.
    You have some really great articles and I think I would be a good asset.
    If you ever want to take some of the load off, I’d love
    to write some content for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Regards!

  5. I absolutely love your blog.. Very nice colors & theme.
    Did you build this amazing site yourself? Please reply back as I’m trying to
    create my own site and want to know where you got this from or
    what the theme is named. Cheers!

  6. I have been browsing on-line greater than 3 hours
    lately, yet I by no means found any interesting article like
    yours. It is beautiful price sufficient for me. In my
    opinion, if all web owners and bloggers made excellent content as you probably did, the net will likely be much more helpful than ever before.
    natalielise plenty of fish

  7. It’s a pity you don’t have a donate button! I’d without a doubt donate to this fantastic blog!
    I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will talk about this site with my Facebook group.

    Chat soon!

  8. Hi! I understand this is sort of off-topic but I needed to ask.
    Does running a well-established website such as yours require a lot of
    work? I’m brand new to running a blog but I
    do write in my diary on a daily basis. I’d like to start a blog so I can share my experience
    and feelings online. Please let me know if you have any suggestions
    or tips for brand new aspiring bloggers. Appreciate it!
    pof natalielise

  9. Having read this I thought it was really enlightening. I appreciate you finding the time
    and energy to put this content together. I once again find
    myself personally spending a lot of time both reading and
    leaving comments. But so what, it was still worth it!

  10. Hi, i read your blog from time to time and i own a similar one and i was
    just wondering if you get a lot of spam remarks?
    If so how do you reduce it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any help is very
    much appreciated.

  11. Heya i’m for the first time here. I came across this board and I find It truly useful &
    it helped me out much. I am hoping to give something
    back and aid others like you aided me.

  12. Hi there, i read your blog occasionally and i own a similar one
    and i was just wondering if you get a lot of
    spam comments? If so how do you reduce it, any plugin or
    anything you can advise? I get so much lately it’s driving me insane
    so any support is very much appreciated.

  13. I am no longer certain where you are getting your info, however great topic.
    I needs to spend a while learning much more or understanding more.

    Thank you for fantastic info I was looking for this info
    for my mission.

  14. Hello there, I think your web site may be having web browser compatibility issues.
    When I take a look at your blog in Safari, it looks fine but when opening in I.E., it has some overlapping issues.

    I simply wanted to give you a quick heads up! Aside from that, excellent site!

  15. I don’t even know how I ended up here, but I
    thought this post was good. I don’t know who you are but definitely you
    are going to a famous blogger if you aren’t already 😉 Cheers!

  16. Thank you for every other informative website.
    The place else may just I get that type of information written in such an ideal way?
    I have a challenge that I am simply now working on, and I have been at the look out for such information.

  17. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now
    each time a comment is added I get three emails with the same comment.
    Is there any way you can remove people from that service?
    Thank you!

  18. Thanks for your personal marvelous posting! I truly enjoyed reading it,
    you happen to be a great author.I will be
    sure to bookmark your blog and will often come back at some point.
    I want to encourage one to continue your great writing, have a nice morning!

  19. Hey there, I think your site might be having browser compatibility issues.
    When I look at your blog site 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, amazing blog!

  20. I absolutely love your blog and find a lot of your post’s to be exactly I’m looking for.
    can you offer guest writers to write content
    for you personally? I wouldn’t mind composing a post or elaborating on some of the subjects you write concerning here.
    Again, awesome site!

  21. Someone essentially help to make critically posts
    I’d state. This is the very first time I frequented
    your website page and up to now? I surprised with the research you made to make this particular submit extraordinary.
    Wonderful job!

  22. This design is steller! You certainly 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!) Wonderful job.

    I really loved what you had to say, and more than that, how you
    presented it. Too cool!

  23. Good day! This is my first comment here so I just wanted to give a
    quick shout out and say I truly enjoy reading
    your blog posts. Can you suggest any other blogs/websites/forums that cover the same subjects?

    Thanks for your time!

  24. Excellent article. Keep posting such kind of info on your site.
    Im really impressed by your blog.
    Hello there, You’ve performed a fantastic job. I will definitely
    digg it and individually suggest to my friends.
    I am sure they’ll be benefited from this site.

  25. The other day, while I was at work, my cousin stole my apple ipad
    and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.

    I know this is completely off topic but I had to share it with someone!

  26. I’m curious to find out what blog system you’re using?
    I’m experiencing some minor security issues with my latest site and
    I would like to find something more risk-free.
    Do you have any suggestions?

  27. Definitely consider that which you stated. Your favourite justification seemed to be on the internet the easiest thing to take into account of.
    I say to you, I certainly get irked at the same time as people think
    about issues that they plainly do not realize
    about. You controlled to hit the nail upon the highest and also defined out
    the entire thing without having side effect , folks can take
    a signal. Will probably be again to get more. Thanks

  28. 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 complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

  29. Hi there! I know this is kind of off topic
    but I was wondering which blog platform are you using for this site?
    I’m getting tired of WordPress because I’ve had issues with hackers and I’m
    looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

  30. Hi my loved one! I want to say that this article is awesome, nice
    written and come with approximately all vital infos. I’d like to see more posts
    like this .

  31. Whats up very nice web site!! Guy .. Beautiful .. Amazing ..

    I will bookmark your web site and take the feeds also?

    I am happy to seek out numerous helpful info
    here within the submit, we’d like work out extra strategies on this regard, thank you for
    sharing. . . . . .

  32. Very nice post. I simply stumbled upon your weblog and wished to mention that
    I have really enjoyed surfing around your weblog posts. In any case I will be subscribing for your rss feed and I am hoping you write once more very soon!

  33. Pretty section of content. I just stumbled upon your web site and in accession capital to assert that I acquire actually enjoyed
    account your blog posts. Any way I’ll be subscribing to your
    feeds and even I achievement you access consistently quickly.

  34. Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t show
    up. Grrrr… well I’m not writing all that over
    again. Anyways, just wanted to say great blog!

  35. obviously like your web site but you have to test the spelling
    on several of your posts. Many of them are rife with spelling problems
    and I to find it very troublesome to tell the truth on the other hand
    I will certainly come again again.

  36. Woah! I’m really enjoying the template/theme of this
    site. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and appearance.
    I must say that you’ve done a very good job with this.
    Also, the blog loads extremely fast for me on Chrome.
    Outstanding Blog!

  37. Do an individual mind if I estimate a couple of your own posts as long since I provide credit and even sources back to your own blog? My blog will be in the same market as yours, and my personal users would benefit through some of the data you provide here. Remember to let me know when this ok with a person. Thank you.

  38. Does your website have a contact site? I’m having problems discovering it but, I’d such as to shoot you a good email. I’ve got a few recommendations for your blog site you will be interested in reading.

Leave a Reply

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