Xdu 1154 rhubarb vote (KMP)

Source: Internet
Author: User

Link:

Http://acm.xidian.edu.cn/land/problem/detail? Problem_id = 1154

Question:

Description

The reason why the school wants to drive stray dogs out is that there are too many electives in the election that gave everyone a "Solemn" vote...

In order to avoid this situation from happening again, the school decided to adopt the new voting method: 1. Each person can only write one letter on the ballot paper! 2. in a certain order (such as the ID card number), the votes are arranged into a string s.3. each elected person corresponds to two strings S1 and s24. when counting the votes, each substring of S is traversed ', if "S" starts with "S1" and ends with "S2" (S1 and S2 can overlap), the corresponding person to be elected has a vote of 5. if a substring is valid for multiple elected members at the same time, all of them are regarded as one vote. the more votes you get, the better... in fact, there is no need to know too deeply. We only care about how many votes rhubarb can get in the new voting method...

Input Multiple groups of data.
There are three rows of data in each group. Each row is S, S1, and S2. The meaning is shown in the preceding figure.
0 <| S |, | S1 |, | S2 | <= 10000; all strings only contain lowercase letters. output each group outputs one row. An integer indicates the final number of votes. Sample inputdahuangg
D
G
Ggnauhad
D
Gsample output2
0

Analysis and Summary:

1. First find S1 in all the matching positions in the original string I, record it in DP [I], and then calculate the number of S1 before all I according to the DP array. Status transfer: DP [I] = DP [I] + dp [I-1].

2. then match S2 with KMP. When a S2 is found, if the position is I, j = m (matching S2) is found. This is the tail position of S2, in this case, you need to find the number of S1 before I, that is, add DP [I. But there is a problem here to note!S 'starts with S1 and ends with S2 (S1 and S2 can overlap ),That is to say, if S1 is shorter than S2, some S1 in DP [I] may be included in S2. This is not starting with S1! This step should be discussed separately. (WA for one night because of this problem ...).

Code:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef long int64; const int maxn = 20005; char s [maxn]; char S1 [maxn], S2 [maxn]; int f [maxn]; int len1; int64 DP [maxn]; void getfail (char * P, int * F) {int n = strlen (p); F [0] = f [1] = 0; For (INT I = 1; I <n; ++ I) {Int J = f [I]; while (J & P [I]! = P [J]) J = f [J]; F [I + 1] = P [I] = P [J]? 1 + J: 0 ;}} int64 find (char * s, char * t, int * F, int flag) {getfail (T, F ); int n = strlen (s); int M = strlen (t); Int J = 0; int64 CNT = 0; For (INT I = 0; I <N; ++ I) {While (J & S [I]! = T [J]) J = f [J]; If (s [I] = T [J]) + + J; if (flag = 1 & J = m) {++ DP [I];} else if (flag = 2 & J = m) {If (len1> = m) // pay attention to the positional relationship between the prefix and suffix CNT + = DP [I]; else {CNT + = DP [I-(m-len1)] ;}}return CNT ;}int main () {While (scanf ("% S % s ", s, S1, S2 )! = EOF) {memset (DP, 0, sizeof (DP); find (S, S1, F, 1); For (INT I = 1; s [I]; + + I) DP [I] + = DP [I-1]; len1 = strlen (S1); // The length of S1 printf ("% LLD \ n", find (S, s2, F, 2);} return 0 ;}


 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.