Pku3450 modify ate Identity & pku3080 blue jeans (enumeration + KMP)

Source: Internet
Author: User

Question: The longest public substring of multiple strings

KMP for pku3450Algorithm

# Include <iostream> # include <algorithm> # include <string. h> using namespace STD; int NXT [210]; char STR [4010] [210], s [210]; int n, m; bool KMP (char * t, char * P) {memset (NXT, 0, sizeof (NXT); NXT [0] =-1; int K =-1; for (INT q = 1; q <m; q ++) {While (k>-1 & P [k + 1]! = P [Q]) k = NXT [k]; If (P [k + 1] = P [Q]) K ++; NXT [Q] = K ;} int J =-1, I = 0; while (T [I]) {While (j>-1 & P [J + 1]! = T [I]) J = NXT [J]; If (P [J + 1] = T [I]) J ++; if (J + 1 = m) return true; I ++;} return false;} int main () {int t; while (scanf ("% d ", & T) = 1 & T) {int L, min1 = 10000; For (INT I = 0; I <t; I ++) {scanf ("% s", STR [I]); If (strlen (STR [I]) <min1) {min1 = strlen (STR [I]); L = I ;}} char OS [210], ANS = 0; For (INT I = 0; I <strlen (STR [l]); I ++) {for (Int J = I; j <strlen (STR [l]); j ++) // The variables I and j are used to enumerate the beginning and end of a string {int B = 0; For (int K = I; k <= J; k ++) // enumerate the length of the shortest string, positive string and reverse string OS [B ++] = STR [l] [k]; OS [B] = '\ 0'; M = B; int flag = 1; for (int K = 0; k <T; k ++) {If (! KMP (STR [K], OS) {flag = 0; break ;}}if (flag & Ans <strlen (OS) {strcpy (S, OS ); ans = strlen (OS);} else if (flag & Ans = strlen (OS) {If (strcmp (S, OS) = 1) strcpy (S, OS) ;}}if (ANS> 0) printf ("% s \ n", S); else puts ("identity lost") ;}return 0 ;}

pku3450 use strstr () function

# Include <iostream> # include <algorithm> # include <string. h> using namespace STD; char STR [4010] [210], s [210]; int n, m; int main () {int T; while (scanf ("% d", & T) = 1 & T) {int L, min1 = 10000; For (INT I = 0; I <t; I ++) {scanf ("% s", STR [I]); If (strlen (STR [I]) <min1) {min1 = strlen (STR [I]); L = I ;}} char OS [210], ANS = 0; For (INT I = 0; I <strlen (STR [l]); I ++) {for (Int J = I; j <strlen (STR [l]); j ++) // variables I and j are used to enumerate the beginning and end of a string {int B = 0; For (int K = I; k <= J; k ++) // enumerate the length of the shortest string, positive string and reverse string OS [B ++] = STR [l] [k]; OS [B] = '\ 0'; int flag = 1; for (int K = 0; k <T; k ++) {If (! Strstr (STR [K], OS) // both the positive string and the reverse string are not displayed in the string {flag = 0; break ;}} if (flag & Ans <strlen (OS) {strcpy (S, OS); ans = strlen (OS );} else if (flag & Ans = strlen (OS) {If (strcmp (S, OS) = 1) strcpy (S, OS );}}} if (ANS> 0) printf ("% s \ n", S); else puts ("identity lost");} return 0 ;}

pku3080

# Include <iostream> # include <algorithm> # include <string. h> using namespace STD; int NXT [100]; char STR [15] [110], s [110]; int n, m; bool KMP (char * t, char * P) {memset (NXT, 0, sizeof (NXT); NXT [0] =-1; int K =-1; for (INT q = 1; q <m; q ++) {While (k>-1 & P [k + 1]! = P [Q]) k = NXT [k]; If (P [k + 1] = P [Q]) K ++; NXT [Q] = K ;} int J =-1, I = 0; while (T [I]) {While (j>-1 & P [J + 1]! = T [I]) J = NXT [J]; If (P [J + 1] = T [I]) J ++; if (J + 1 = m) return true; I ++;} return false;} int main () {int cas, T; scanf ("% d ", & CAS); While (CAS --) {scanf ("% d", & N); int L, min1 = 10000; For (INT I = 0; I <N; I ++) {scanf ("% s", STR [I]); If (strlen (STR [I]) <min1) {min1 = strlen (STR [I]); L = I ;}} char OS [210], ANS = 0; For (INT I = 0; I <strlen (STR [l]); I ++) {for (Int J = I; j <strlen (STR [l]); j ++) // The variables I and j are used to enumerate the beginning and end of a string {int B = 0; For (int K = I; k <= J; k ++ )/ /Enumerate the length of the shortest string, namely, the positive string and the reverse string OS [B ++] = STR [l] [k]; OS [B] = '\ 0'; M = B; int flag = 1; for (int K = 0; k <n; k ++) {If (! KMP (STR [K], OS) {flag = 0; break ;}}if (flag & Ans <strlen (OS) {strcpy (S, OS ); ans = strlen (OS);} else if (flag & Ans = strlen (OS) {If (strcmp (S, OS) = 1) strcpy (S, OS) ;}}if (ANS >=3) printf ("% s \ n", S); else puts ("no significant commonalities");} return 0 ;}

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.