26: Maximum string span, 26: String Span

Source: Internet
Author: User

26: Maximum string span, 26: String Span
26: maximum span of a string

  • View
  • Submit
  • Statistics
  • Question
Total time limit:
1000 ms
 
Memory limit:
65536kB
Description

There are three strings: S, S1, and S2. The length of S cannot exceed 300, and the length of S1 and S2 cannot exceed 10. Check whether S1 and S2 appear in S at the same time, and S1 is on the left of S2, and do not cross each other in S (that is, the right border of S1 is on the left of the Left Border of S2 ). Calculate the maximum span that meets the preceding conditions (that is, the maximum interval distance: the number of characters between the start point of the rightmost S2 and the end point of the leftmost S1 ). If no S1 or S2 meets the conditions,-1 is output.

For example, S = "abcd123ab888efghij45ef67kl", S1 = "AB", S2 = "ef", where S1 appears twice in S, s2 also appears twice in S, with a maximum span of 18.

Input
Three strings: S1, S2, and S3, separated by commas (Note: S1, S2, and S3 do not contain commas or spaces );
Output
S1 and S2 are in the maximum span of S. If S1 and S2 do not meet the conditions in S,-1 is output.
Sample Input
abcd123ab888efghij45ef67kl,ab,ef
Sample output
18
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 char s [305], s1 [15], s2 [15]; 6 int main () {7 char c; 8 int len = 0, len1 = 0, len2 = 0; 9 while (c = getchar () & c! = ',') 10 s [len ++] = c; // input character array s11 while (c = getchar () & c! = ',') 12 s1 [len1 ++] = c; // input character array s113 while (c = getchar () & c! = EOF) 14 s2 [len2 ++] = c; // input character array s215 int I, j = len2-2, k = 0, ans = 0; 16 for (I = 0; I <len; I ++) {17 if (s [I] = s1 [k]) 18 k ++; // when the characters in the character array s1 and in the character array s are both k ++ and if statement 19 else {20 k = 0 when k = len1; // otherwise, k is reset to 0 and 21 if (s [I] = s1 [k]) k ++ is continued; // if found, perform the if statement 22} 23 if (k = len1) {// if 24 ans = ans-(I + 1) is found ); // If the k length is equal to the s1 array length, convert-(I + 1) assign a value to ans, that is, the negative value of the first character position after s1 {a s d a d s1 (z) 25 26 break; // skip loop 27} 28} 29 for (I = len-1; I> = 0; I --) {30 // traverse 31 from the last character of character array s if (s [I] = s2 [j]) 32 j --; // calculate from the first valid character in the character array s2 if it is equal to the character in the character array s, then j -- 33 else {34 j = len2-2; // reset to l2-235 if (s [I] = s2 [j]) j --; 36} 37 if (j = 0) {// when found 38 ans = ans + (I-1); // If j = 0, assign t + (I-1) to t; 39 break; 40} 41} 42 if (ans> = 0) cout <ans; // if t is greater than 0, t43 else cout <"-1 "; // otherwise output-144 return 0; 45}

 

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.