Data Structure Course Design-Longest Common substring

Source: Internet
Author: User

Data Structure Course Design: the user inputs two string strings X and Y, and then the user inputs an arbitrary string Z to implement the following functions:

① If string Z is a substring of string X, the position of string Z in string X is displayed and recorded. If string Z is a substring of string y, the position of Z in Y is displayed and recorded. If Z is neither a substring of X nor a substring of Y, It is not matched.

② Find the longest common substring of X and Y.

③ Replacement: the user enters any string to replace the substring Z in X and Y.

Idea: first, use the KMP algorithm to quickly locate the matching position of the substring in the main string. Use the dynamic programming idea to find the longest common substring, and then replace the string in the main string with a new string of the same length as the substring.

The complete code is as follows:

# Include "stdio. H "<br/> # include" string. H "<br/> # include" stdlib. H "<br/> const int n= 200; <br/> int C [N] [N]; <br/> char STR [N]; <br/> # define maxsize 100 <br/> typedef struct <br/> {<br/> char data [maxsize]; <br/> int length; // String Length <br/>} sqstring; <br/> void strassign (sqstring & STR, char CSTR []) // STR is a reference parameter <br/>{< br/> int I; <br/> for (I = 0; CSTR [I]! = '/0'; I ++) <br/> Str. data [I] = CSTR [I]; <br/> Str. data [I] = '/0'; <br/> Str. length = I; <br/>}</P> <p> void getnextval (sqstring T, int nextval []) // obtain the nextval value from the mode string T <br/>{< br/> Int J = 0, K =-1; <br/> nextval [0] =-1; <br/> while (j <t. length) <br/>{< br/> If (k =-1 | T. data [J] = T. data [k]) <br/>{< br/> J ++; <br/> K ++; <br/> If (T. data [J]! = T. data [k]) <br/> nextval [J] = K; <br/> else <br/> nextval [J] = nextval [k]; <br/>}< br/> else K = nextval [k]; <br/>}</P> <p >}< br/> int kmpindex1 (sqstring s, sqstring T, int start) // corrected KMP algorithm. Start is the starting position of each matching. <br/>{< br/> int nextval [maxsize], I = 0, j = 0; <br/> getnextval (T, nextval); <br/> I = start; <br/> while (I <S. length & J <t. length) <br/>{</P> <p> If (j =-1 | S. data [I] = T. data [J]) <br/>{< br/> I = I + 1; <br/> J ++; <br/>}< br /> Else <br/> J = nextval [J]; <br/>}< br/> If (j> = T. length) <br/> return (i-t.length); <br/> else <br/> return-1; <br/>}< br/> int lcs_len (char a [], char B [], int C [] [N]) <br/>{< br/> int SA = strlen (a); <br/> int sb = strlen (B); <br/> int I, J; <br/> for (I = 0; I <= sa; I ++) <br/> C [I] [0] = 0; <br/> for (j = 0; j <= Sb; j ++) <br/> C [0] [J] = 0; <br/> for (I = 1; I <= sa; I ++) <br/> {<br/> for (j = 1; j <= Sb; j ++) <br/>{< br/> if (a [I-1] = B [J-1]) <br/> C [I] [J] = C [I-1] [J-1] + 1; <br/> else if (C [I] [J-1]> C [I-1] [J]) <br/> C [I] [J] = C [I] [J-1]; <br/> else C [I] [J] = C [I-1] [J]; <br/>}< br/> return C [SA] [SB]; <br/>}< br/> char * bu1__lcs (char a [], char B []) <br/>{< br/> int K = 0, I, j; <br/> I = strlen (a); <br/> J = strlen (B); <br/> K = lcs_len (A, B, C ); <br/> STR [k] = '/0'; <br/> while (k> 0) <br/> {<br/> If (C [I] [J] = C [I-1] [J]) <br/> I --; <br/> else if (C [I] [J] = C [I] [J-1]) <br/> j --; <Br/> else <br/> {<br/> STR [-- K] = A [I-1]; <br/> I --; <br/> j --; <br/>}< br/> return STR; <br/>}< br/> void main () <br/> {<br/> char a [n], B [N], Z [N], F [N], change [N]; <br/> sqstring S, T, St; <br/> int P [N], Q [N], M, I = 1, J, J1, J2, K, h, start, flag1, flag2; <br/> printf ("Please enter three strings! /N "); <br/> printf (" Please enter first string X: "); <br/> gets (); <br/> printf ("Please enter second string Y:"); <br/> gets (B); <br/> printf ("please the new string Z: "); <br/> gets (z); <br/> strassign (s, A); <br/> strassign (T, B ); <br/> strassign (St, Z); <br/> Start = 0, J1 = 0; <br/> flag1 = flag2 =-1; <br/> while (1) // obtain all positions of the sub-string Z in the main string X, and record <br/>{< br/> If (Start> = S. length) <br/> break; <br/> M = kmpindex1 (S, St, start); <br /> If (M! =-1) <br/>{< br/> flag1 = 1; <br/> printf ("the position of string Z in string x matching at % d is: % d/N ", I ++, m); <br/> P [J1 ++] = m; <br/> Start = m + st. length; <br/>}< br/> else <br/> break; <br/>}< br/> Start = 0, J2 = 0, I = 1; <br/> while (1) // obtain all positions of the substring Z in the main string y, and record <br/>{< br/> If (Start> = T. length) <br/> break; <br/> M = kmpindex1 (T, St, start); <br/> If (M! =-1) <br/>{< br/> flag2 = 1; <br/> printf ("the position of string Z in string y matching at % d is: % d/N ", I ++, m); <br/> q [J2 ++] = m; <br/> Start = m + st. length; <br/>}< br/> else <br/> break; <br/>}< br/> If (flag1 =-1 & flag2 =-1) <br/> printf ("unmatched! /N "); // <br/> bu1__lcs (a, B); <br/> If (STR [0] = '') <br/> printf ("These two strings do not have public strings! /N "); <br/> else <br/> {<br/> printf (" the max length common subsequence is :"); <br/> puts (STR ); // find the longest common substring of X and Y <br/>}< br/> If (flag1 = 1 | flag2 = 1) <br/> {<br/> printf ("enter any string to replace the substring Z:/N in X and Y "); <br/> gets (Change); <br/> If (flag1 = 1) // Replace the string Z in x <br/>{< br/> printf ("the substring Z in X is replaced by % s with:", change ); <br/> H = k = 0; <br/> for (I = 0; I <J1; I ++) <br/>{< br/> (; k <p [I]; k ++) <br/> F [H ++] = A [k]; <br/> for (j = 0; j <strlen (Change); j ++) <br/> F [H ++] = change [J]; <br/> K + = ST. length; <br/>}< br/> for (; k <S. length; k ++) <br/> F [H ++] = A [k]; <br/> F [H] = '/0 '; <br/> strcpy (A, F); <br/> puts (a); <br/>}< br/> If (flag2 = 1) // Replace the string Z in Y <br/>{< br/> printf ("the substring Z in Y is replaced by % s with:", change ); <br/> H = k = 0; <br/> for (I = 0; I <J2; I ++) <br/>{< br/> (; k <q [I]; k ++) <br/> F [H ++] = B [k]; <br/> for (j = 0; j <strlen (Change); j ++) <br/> F [H ++] = change [J]; <br/> K + = ST. length; <br/>}< br/> for (; k <t. length; k ++) <br/> F [H ++] = B [k]; <br/> F [H] = '/0 '; <br/> strcpy (B, F); <br/> puts (B ); <br/>}< br/> system ("pause"); <br/>}

 

The running result is as follows:

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.