Poj 3080 Blue Jeans KMP

Source: Internet
Author: User

Find the longest and longest string in all strings

Solution: enumeration of KMP

Solution code:

1 // file name: getnext. CPP 2 // Author: darkdream 3 // created time: tuesday, September 09, 2014, 02 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # I Nclude <cmath> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long 25 26 using namespace STD; 27 char str1 [100]; 28 char STR [20] [100]; 29 int next [105]; 30 void split (int x, int y) 31 {32 int t =-1; 33 for (INT I = x; I <= y; I ++) 34 {35 str1 [++ T] = STR [1] [I]; 36} 37 str1 [++ T] = '\ 0'; 38} 39 void getnext () 40 {41 int Len = strlen (str1); 42 next [0] =-1; 43 in T K =-1; 44 Int J = 0; 45 while (j <= len-1) 46 {47 If (k =-1 | str1 [J] = str1 [k]) 48 {49 + J; 50 + + K; 51 next [J] = K; 52} 53 else {54 K = next [k]; 55} 56} 57} 58 int KMP (int K) 59 {60 Int J = 0; 61 int I = 0; 62 int Len = strlen (str1); 63 64 While (j <60) 65 {66 if (I =-1 | str1 [I] = STR [k] [J]) 67 {68 I ++; 69 J ++; 70} else {71 I = next [I]; 72} 73 if (I = Len) 74 {75 re Turn 1; 76} 77} 78 return 0; 79} 80 int N; 81 char ans [100]; 82 int maxlen; 83 int solve (int x, int y) 84 {85 split (x, y); 86 // puts (str1); 87 getnext (); 88 for (INT I = 2; I <= N; I ++) 89 {90 if (! KMP (I) 91 {92 return 0; 93} 94} 95 // puts (str1); 96 If (Y-x + 1> maxlen) 97 {98 strcpy (ANS, str1); 99 maxlen = Y-x + 1; 100} 101 else if (Y-x + 1 = maxlen) 102 {103 If (strcmp (ANS, str1)> 0) 104 strcpy (ANS, str1); 105} 106 return 1; 107} 108 int main () {109 int T; 110 scanf ("% d", & T); 111 while (t --) 112 {113 scanf ("% d", & N); 114 maxlen = 0; 115 for (INT I = 1; I <= N; I ++) 116 {117 scanf ("% s", STR [I]); 118} 119 for (INT I = 0; I <60; I ++) 120 for (Int J = I; j <60; j ++) 121 {122 If (! Solve (I, j) 123 {124 break; 125} 126} 127 If (maxlen> = 3) 128 {129 puts (ANS ); 130} else {131 printf ("no significant commonalities \ n"); 132} 133 return 0; 134}
View code

 

Poj 3080 Blue Jeans KMP

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.