题目传送门:http://codeforces.com/contest/696/problem/D
官方题解:http://codeforces.com/blog/entry/46031
这个题目,看一眼数据范围就知道是AC自动机+矩阵快速幂
所以剩下的唯一问题就是,我们重定义矩阵运算之后为何其仍然满足结合律?
#include<bits/stdc++.h> #define LL long long using namespace std; const int N = 200+9; const int SGZ = 27; const int MX = 201; inline int read(){ char c=getchar(); int ret=0,f=1; 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; } struct Matrix{ LL a[N][N]; inline Matrix() {memset(a,-1,sizeof(a));} inline Matrix(Matrix &v) {memcpy(this,&v,sizeof(v));} inline Matrix(int bas, int v) {memset(a,bas,sizeof(a));for (int i=1;i<=MX;i++) a[i][i]=v;} inline Matrix operator * (const Matrix &B) { Matrix ret; for (int i=1;i<=MX;i++) for (int j=1;j<=MX;j++) for (int k=1;k<=MX;k++) if (~a[k][j] && ~B.a[i][k]) ret.a[i][j] = max(ret.a[i][j], a[k][j] + B.a[i][k]); return ret; } inline Matrix operator ^ (LL t) { Matrix ret(-1,0), tmp(*this); while (t) { if (t & 1) ret = ret * tmp; tmp = tmp * tmp; t >>= 1; } return ret; } }tran,ans; namespace AC_Automaton{ #define ATM AC_Automaton int ch[N][SGZ],fail[N],val[N],cnt=1,vis[N]; queue<int> que; inline void Add(char *s, int v){ int w = 1, n = strlen(s+1); for (int i=1;i<=n;i++) { int c = s[i] - 'a' + 1; if (!ch[w]) ch[w] = ++cnt; w = ch[w]; } val[w] += v; } inline void Get_Fail(){ que.push(1); fail[0] = fail[1] = 1; for (int i=1;i<SGZ;i++) ch[1][i] = ch[1][i]?ch[1][i]:1; while (!que.empty()) { int w = que.front(); que.pop(); val[w] += val[fail[w]]; vis[w] = 1; for (int c=1;c<SGZ;c++) if (ch[w]) { int nw = fail[w]; while (nw > 1 && !ch[nw]) nw = fail[nw]; fail[ch[w]] = nw!=w?ch[nw]:1; if (!vis[ch[w]]) que.push(ch[w]); } else ch[w] = ch[fail[w]]; } } inline void Build_Matrix(){ ans.a[1][1] = 0; for (int i=1;i<=cnt;i++) for (int c=1;c<SGZ;c++) tran.a[ch[i]][i] = val[ch[i]]; } }; int main(){ int n,val[N]; LL T; char pat[N]; cin>>n>>T; for (int i=1;i<=n;i++) cin>>val[i]; for (int i=1;i<=n;i++) scanf("%s",pat+1), ATM::Add(pat,val[i]); ATM::Get_Fail(); ATM::Build_Matrix(); tran = tran^T; ans = ans * tran; LL vout = 0; for (int i=2;i<=MX;i++) vout = max(vout, ans.a[i][1]); printf("%I64d\n",vout); return 0; }
—– UPD 2016.10.2 —–
今天问了一下鏼爷,鏼爷给了一个非常简洁的证明:
最短路问题可以写成矩阵形式(比如倍增Floyd)
然后最短路显然满足结合律
然后这题对于矩乘的定义岂不是和最短路一样?
—————————— UPD 2017.2.1 ——————————
这题后来问了问YYY,结果YYY暴力展开矩乘
似乎这样就可以知道是否满足结合律了
不过如果在考场上,直接拍一拍吧
如果不出错,那就假装他满足结合律吧?
I am regular reader, how are you everybody?
This paragraph posted at this website is genuinely nice.
I was wondering if you ever thought of changing the structure of your website?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could connect with it better.
Youve got an awful lot of text for only having 1
or 2 pictures. Maybe you could space it out better?
It is actually a nice and useful piece of info.
I am glad that you just shared this helpful information with us.
Please keep us informed like this. Thanks for sharing.
Thanks for your marvelous posting! I actually enjoyed reading it, you might be
a great author. I will make sure to bookmark your blog and definitely will come back very soon.
I want to encourage you to continue your great work, have a nice morning!
I know this if off topic but I’m looking into starting my own weblog and was curious what all is needed to get set up?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web savvy so I’m not 100% sure.
Any recommendations or advice would be greatly appreciated.
Kudos
Hello to every single one, it’s in fact a pleasant for me
to pay a quick visit this web site, it includes valuable
Information.
Wow, this paragraph is fastidious, my younger sister is analyzing such
things, therefore I am going to let know her.
Great beat ! I would like to apprentice while you amend your site,
how could i subscribe for a blog website? The account aided me a acceptable deal.
I had been tiny bit acquainted of this your broadcast provided bright clear concept
Undeniably believe that which you said. Your favorite
justification 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 do not know about. You managed to hit the
nail upon the top as well as defined out the whole thing without having side-effects , people could take a signal.
Will likely be back to get more. Thanks
Howdy! Quick question that’s completely off topic. Do you know
how to make your site mobile friendly? My blog looks weird when browsing from my iphone
4. I’m trying to find a theme or plugin that might be able to
correct this problem. If you have any recommendations, please share.
Thank you!
What’s up Dear, are you actually visiting this site on a regular basis, if so afterward you will absolutely
obtain fastidious experience.
This paragraph will assist the internet visitors for
setting up new blog or even a weblog from start to end.
Sweet blog! I found it while surfing around on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers
Yes! Finally something about how to get help in windows 10.
of course like your web-site however you have to take
a look at the spelling on quite a few of your posts.
Several of them are rife with spelling issues and I find it very troublesome to
inform the truth nevertheless I will surely come back again.
Great article! We will be linking to this particularly great content on our
website. Keep up the good writing.
Everything is very open with a very clear clarification of the challenges.
It was really informative. Your site is very useful.
Thanks for sharing! natalielise plenty of fish
I think this is among the most important info for me.
And i’m glad reading your article. But want to remark on some general things,
The site style is wonderful, the articles is really excellent : D.
Good job, cheers
Hi, I do think this is a great site. I stumbledupon it 😉 I’m going to
return once again since i have book-marked it.
Money and freedom is the best way to change, may you be rich and continue to guide
other people.
I’d like to find out more? I’d want to find out some additional information.
An outstanding share! I’ve just forwarded this onto a colleague who has been doing a little homework on this.
And he actually ordered me lunch because I discovered it for him…
lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending the time to
talk about this issue here on your site.
I have learn a few good stuff here. Certainly worth bookmarking for revisiting.
I surprise how much effort you set to create any such fantastic informative website.
natalielise pof
Wonderful, what a web site it is! This blog gives useful data to us, keep it up.
This info is worth everyone’s attention. When can I find out more?
This is really attention-grabbing, You’re a very professional blogger.
I’ve joined your feed and stay up for in search of more of your wonderful post.
Additionally, I have shared your web site in my social networks
You need to take part in a contest for one of the highest quality sites on the internet.
I will recommend this blog!
You really make it appear so easy with your presentation however I in finding this topic to be actually something which I think I’d
by no means understand. It sort of feels too complex and extremely huge for
me. I am looking ahead on your next submit, I’ll attempt to get
the cling of it!
Howdy! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate!
He always kept talking about this. I will forward this post to him.
Fairly certain he will have a good read. Many thanks for sharing!
I loved as much as you’ll receive carried out right here.
The sketch is tasteful, your authored subject matter
stylish. nonetheless, you command get bought an nervousness
over that you wish be delivering the following. unwell unquestionably come further
formerly again as exactly the same nearly a lot often inside
case you shield this hike.
Hi there, yup this piece of writing is actually fastidious and I have learned lot of things from it about blogging.
thanks.
Hi there i am kavin, its my first occasion to commenting anyplace,
when i read this post i thought i could also make comment due to this sensible article.
Asking questions are actually pleasant thing if you are
not understanding something totally, except this post presents
pleasant understanding yet.
When I initially 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 me from that service? Appreciate it!
Great article.
Post writing is also a fun, if you be familiar with after that you can write or else it is difficult to write.
It’s actually very complicated in this full of activity life to listen news on Television, therefore I only use web for that reason, and take the latest
information.
Spot on with this write-up, I really believe that
this amazing site needs much more attention. I’ll probably be returning to read through more, thanks for the advice!
I every time emailed this website post page to all
my contacts, as if like to read it then my friends will too.
I really like what you guys are usually up too. This kind of clever work and coverage!
Keep up the fantastic works guys I’ve incorporated you guys to my blogroll.
I’m extremely 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 nowadays.
Hi there every one, here every person is sharing these familiarity,
thus it’s good to read this web site, and I used to visit this web
site every day.
I think this is one of the most vital info for me. And i’m glad reading your article.
But should remark on few general things, The web site style is perfect, the articles is really nice : D.
Good job, cheers
If some one wants to be updated with hottest technologies afterward he
must be pay a quick visit this web page and be
up to date every day.
Hi there, all is going well here and ofcourse every one is sharing data, that’s truly
good, keep up writing.
I think the admin of this web site is truly working hard for his website,
as here every material is quality based material.
I needed to thank you for this great read!! I certainly loved every little bit
of it. I’ve got you book-marked to check out new stuff you
post…
Enjoyed every bit of your blog article.Really thank you!
Outstanding quest there. What happened after? Thanks!
Really enjoyed this blog article.Much thanks again. Great.
Im thankful for the post.Thanks Again. Keep writing.
A big thank you for your blog.Thanks Again. Much obliged.
It is appropriate time to make a few plans for the future and
it is time to be happy. I’ve learn this post and if I could I wish to counsel you few
interesting issues or tips. Perhaps you could write subsequent articles
regarding this article. I desire to read even more
issues approximately it!
Thank you for your article.Really thank you! Really Great.
I really enjoy the article.Much thanks again. Great.
I like what you guys tend to be up too. Such clever work and exposure!
Keep up the wonderful works guys I’ve added you guys to my
personal blogroll.
I value the blog.Much thanks again. Keep writing.
I am so grateful for your blog post.Thanks Again. Great.
Very neat blog post.Really thank you! Really Cool.
I think this is a real great blog post.Really thank you! Really Great.
A round of applause for your post.Thanks Again. Cool.
Hi there! Someone in my Facebook group shared this site with us so I came to
look it over. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!
Great blog and amazing design.
I think the admin of this web page is truly working hard for
his website, since here every material is quality based material.
I appreciate you sharing this article post.Really thank you! Cool.
Major thankies for the blog post.Really looking forward to read more. Really Great.
Fantastic blog.Really looking forward to read more.
My partner and I absolutely love your blog and find many of
your post’s to be just what I’m looking for.
can you offer guest writers to write content for yourself?
I wouldn’t mind creating a post or elaborating
on a number of the subjects you write regarding
here. Again, awesome blog!
Im obliged for the article post.Really thank you! Awesome.
I’m gone to tell my little brother, that he should also go to see this webpage on regular
basis to get updated from most recent news update.
Hello there! This article couldn’t be written any better!
Looking at this post reminds me of my previous roommate!
He constantly kept talking about this. I most certainly will send
this article to him. Fairly certain he’s going to have a very good read.
I appreciate you for sharing!
I cannot thank you enough for the post. Awesome.
Thanks so much for the blog.
Thank you ever so for you article post.Thanks Again. Will read on…
I value the post.Really thank you! Cool.
Thanks again for the blog post.Much thanks again. Keep writing.
Say, you got a nice blog post.Really thank you! Great.
Thanks so much for the article.Really thank you! Will read on…
Appreciating the persistence you put into your blog and in depth information you offer.
It’s nice to come across a blog every once in a while that isn’t the same unwanted rehashed material.
Wonderful read! I’ve bookmarked your site and I’m including
your RSS feeds to my Google account.
Thanks for sharing, this is a fantastic article post.Thanks Again. Really Great.
Awesome blog post.
Really enjoyed this blog.Really thank you! Fantastic.
Appreciate you sharing, great blog.Thanks Again.
Thanks so much for the blog article.Much thanks again. Really Great.
I could not resist commenting. Perfectly written!
I really like and appreciate your blog. Really Great.
Great, thanks for sharing this blog.Much thanks again. Awesome.
I really like and appreciate your blog post.Really looking forward to read more. Really Great.
A round of applause for your blog.Really looking forward to read more. Awesome.
I just like the valuable information you supply to your articles.
I will bookmark your blog and test once more here frequently.
I am relatively sure I’ll learn lots of new stuff proper here!
Best of luck for the following!
Awesome blog post.Really looking forward to read more. Will read on…
Thanks for sharing, this is a fantastic article.Really looking forward to read more. Great.
Thanks a lot for the blog article.Thanks Again. Keep writing.
I couldn’t refrain from commenting. Perfectly written!
Im grateful for the post.Really looking forward to read more. Awesome.
Major thanks for the blog.Thanks Again. Will read on…
Thanks-a-mundo for the post.Thanks Again. Much obliged.
Great article post.Really looking forward to read more. Much obliged.
I absolutely love your site.. Very nice colors & theme.
Did you build this web site yourself? Please reply back as I’m trying
to create my own personal blog and want to find out where you got this from or just what
the theme is called. Many thanks!
Attractive component to content. I simply stumbled upon your web
site and in accession capital to say that I acquire
in fact enjoyed account your blog posts. Anyway I will be
subscribing for your feeds and even I fulfillment you get admission to persistently fast.
Thanks for the good writeup. It actually was a
enjoyment account it. Look complicated to far brought agreeable from you!
By the way, how could we be in contact?
Im grateful for the blog.Much thanks again. Great.
Very informative blog post.Much thanks again.
Say, you got a nice blog article.Really thank you! Keep writing.
Major thanks for the blog. Awesome.
Thanks for every other magnificent post. Where else could anybody get that type
of info in such an ideal manner of writing? I’ve a
presentation subsequent week, and I am on the search for such info.
Looking forward to reading more. Great article post.Much thanks again. Really Great.
This is one awesome blog post.Much thanks again. Cool.
Thanks a lot for the post.
Today, I went to the beach with my children. I
found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.”
She put the shell to her ear and screamed. There was a hermit crab
inside and it pinched her ear. She never wants to go back!
LoL I know this is entirely off topic but I had to tell someone!
I truly appreciate this blog. Will read on…
Very neat blog article.Thanks Again. Awesome.
Today, I went to the beach front with my kids.
I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell
to her ear and screamed. There was a hermit crab inside and it pinched her
ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!
Hey, thanks for the blog. Really Great.
Simply wanna comment on few general things, The website pattern is perfect, the articles is real wonderful. “If a man does his best, what else is there” by George Smith Patton, Jr..
Fantastic blog.Really thank you!
Enjoyed every bit of your post. Really Cool.
I cannot thank you enough for the blog.Really looking forward to read more. Will read on…
Muchos Gracias for your blog. Want more.
Thanks again for the article post.Thanks Again. Really Great.
Thanks a lot for the blog article.Really looking forward to read more. Great.
I truly appreciate this blog.Much thanks again. Much obliged.
I really like and appreciate your post.Thanks Again. Cool.
Thanks for the blog post.Really thank you! Really Great.
I truly appreciate this blog.Much thanks again.
Im thankful for the article.Thanks Again.
Very good blog post.Really thank you! Much obliged.
This is a really good tip especially to those new to the blogosphere.
Short but very accurate information… Many thanks for sharing
this one. A must read post!
Thank you ever so for you blog. Much obliged.
Thank you ever so for you blog post. Really Great.
A round of applause for your blog.Really looking forward to read more. Keep writing.
I really enjoy the article.Much thanks again. Much obliged.
I have read a few excellent stuff here. Definitely price
bookmarking for revisiting. I wonder how a lot effort you put to create the sort of excellent informative
website.
I truly appreciate this blog article.Really looking forward to read more. Fantastic.
I am really impressed with your writing skills as well as with the layout on your blog.
Is this a paid theme or did you modify it yourself?
Anyway keep up the nice quality writing, it is rare to see a
great blog like this one nowadays.
Hi, i believe that i saw you visited my site thus i came to return the choose?.I’m attempting to to find issues to enhance my web site!I assume its
adequate to use some of your concepts!!
I love what you guys are usually up too. Such clever work and exposure!
Keep up the superb works guys I’ve incorporated you guys to blogroll.
Fantastic blog article. Cool.
Thanks again for the blog.Really looking forward to read more. Cool.
Awesome blog article.Much thanks again. Awesome.
Very informative blog.Really looking forward to read more. Cool.
Thank you for your article post.Really looking forward to read more.
Awesome article post.Really thank you! Fantastic.
Major thankies for the blog article.Much thanks again. Cool.
Thanks for sharing, this is a fantastic blog post. Will read on…
I cannot thank you enough for the article post.Thanks Again.
I really enjoy the blog post.Thanks Again. Keep writing.
Thank you ever so for you blog article.Thanks Again. Will read on…
Very informative blog post.Really looking forward to read more. Want more.
Major thanks for the blog.Thanks Again. Fantastic.
I truly appreciate this post. Really Great.
Spot on with this write-up, I honestly believe this web site needs a lot more attention. I’ll probably be returning to see more, thanks for the advice!
Heya i am for the first time here. I found this board and I in finding It truly useful & it helped me
out a lot. I hope to give one thing back and help others
such as you aided me.
A big thank you for your post. Much obliged.
Thank you for your blog post.Much thanks again. Great.
Muchos Gracias for your blog article.Much thanks again. Much obliged.
I think this is a real great post.Really looking forward to read more. Cool.
Thanks-a-mundo for the blog post.Really looking forward to read more. Much obliged.
Really enjoyed this article post.Thanks Again. Awesome.
Wow, great article.Much thanks again. Great.
I cannot thank you enough for the article post. Keep writing.
Awesome blog.Thanks Again. Want more.
I’m impressed, I must say. Rarely do I encounter a blog that’s equally educative and engaging,
and without a doubt, you have hit the nail on the head.
The issue is an issue that not enough men and women are speaking intelligently about.
I’m very happy that I found this during my search
for something concerning this.
Im thankful for the article post.Much thanks again. Cool.
Thanks for the post.Much thanks again. Awesome.
I really enjoy the blog article.Thanks Again. Really Great.
A big thank you for your blog. Want more.
I really liked your blog post.Thanks Again. Fantastic.
Fantastic article post.Thanks Again. Will read on…
I think this is a real great post. Much obliged.
I appreciate you sharing this article.Really thank you! Cool.
Muchos Gracias for your blog article.Really looking forward to read more. Keep writing.
I think this is a real great post.Really thank you! Keep writing.
Really appreciate you sharing this post. Keep writing.
Thanks for sharing, this is a fantastic post.Really looking forward to read more.
Great article post.Really looking forward to read more. Really Great.
Major thankies for the blog article.Thanks Again. Great.
I was recommended this web site by my cousin. I’m not sure whether this post is written by
him as nobody else know such detailed about my trouble. You’re wonderful!
Thanks!
Very good blog post. Keep writing.
Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how
to get listed in Yahoo News? I’ve been trying for a while but I never seem to get
there! Cheers
Thanks for the blog.Much thanks again. Keep writing.
I loved your article. Will read on…
Great blog post.Really thank you! Keep writing.
Major thankies for the post.Thanks Again. Cool.
I really enjoy the post.Really thank you! Really Great.
Awesome article post.Thanks Again. Fantastic.
I value the post.Much thanks again. Really Cool.
Very informative blog post.Really thank you! Keep writing.
Great, thanks for sharing this blog post.
Hey, thanks for the blog.Much thanks again. Really Cool.
Thank you for your post.Really thank you! Really Great.
I really enjoy the article post.Much thanks again. Cool.
I really like and appreciate your article.Really looking forward to read more. Really Great.
wow, awesome blog article.Thanks Again. Want more.
Thank you for your blog article.Really thank you! Really Great.
I really liked your blog article.Much thanks again. Much obliged.
I appreciate you sharing this blog post.Really thank you! Much obliged.
I really enjoy the article.Really thank you! Will read on…
Really enjoyed this blog article.Really thank you! Want more.
Thanks a lot for the article post.Thanks Again. Really Cool.
I cannot thank you enough for the article post.Really thank you! Will read on…
Enjoyed every bit of your article post.Much thanks again. Cool.
Thank you ever so for you blog.Really thank you! Keep writing.
Thanks for sharing, this is a fantastic blog post.Really thank you! Keep writing.
Well I sincerely liked reading it. This subject offered by you is very useful for proper planning.
I appreciate you sharing this article post.
I really like and appreciate your post.Much thanks again. Great.
Im obliged for the blog. Keep writing.
Thank you ever so for you article post. Much obliged.
wow, awesome blog post.Really looking forward to read more. Really Cool.
Very neat blog article.Thanks Again. Really Cool.
I loved your post. Really Cool.
I really like and appreciate your blog article.Thanks Again. Want more.
This is one awesome article.Really looking forward to read more. Want more.
Great, thanks for sharing this article.Thanks Again.
Great blog post.Really looking forward to read more.
Fantastic post.Really thank you! Much obliged.
I really enjoy the post.Really thank you! Great.
Wow, great post.Really thank you! Cool.
Muchos Gracias for your blog post. Great.
Thank you for your post.Much thanks again. Cool.
Thank you for your article post.Thanks Again. Really Cool.
Hey, thanks for the post.Really looking forward to read more.
Thanks again for the blog. Much obliged.
I really liked your blog.Thanks Again. Fantastic.
Really appreciate you sharing this blog post.Much thanks again. Awesome.
Major thankies for the post. Much obliged.
Im obliged for the blog article.Really thank you! Will read on…
Im thankful for the blog article. Fantastic.
Great, thanks for sharing this post.Really looking forward to read more. Awesome.
Thank you for your article post.Thanks Again. Awesome.
Very informative post. Will read on…
I really like and appreciate your post.Really looking forward to read more. Much obliged.
Say, you got a nice article post.Thanks Again. Cool.
I appreciate you sharing this blog.Really thank you! Will read on…
Thanks for the blog post.Really looking forward to read more. Much obliged.
I value the blog post.Really thank you! Want more.
Say, you got a nice blog article. Cool.
Appreciate you sharing, great post. Really Great.
Fantastic article.Really looking forward to read more. Want more.
Hey, thanks for the article post.Thanks Again. Awesome.
Thanks so much for the article.Really thank you! Great.
I really liked your blog article.Really thank you! Keep writing.
Thanks for sharing, this is a fantastic article.Much thanks again.
[url=https://atorvastatinlipitor.com/]lipitor 10mg price australia[/url] [url=https://zoloftgen.com/]zoloft 10[/url] [url=https://wellbutrinlab.com/]wellbutrin drug[/url]
Great post.Really looking forward to read more. Really Great.
Enjoyed every bit of your article.Really looking forward to read more. Really Great.