Blue jeans-poj 3080 (multiple strings of common substrings)

Source: Internet
Author: User
Tags first string

The main topic: There are m strings, each string length is 60, find the M string of the longest common substring (continuous), the length can not be less than 3, if the same length has multiple output dictionary order the smallest one.   Analysis: Because the string is not many, and relatively short, resulting in direct violence enumeration of the first string of all substrings, compared to the practice of violence, if the length of the string is more than can not play. The code is as follows: ====================================================================================
#include <stdio.h>#include<string.h>Const intMAXN =107;Const intOO = 1e9+7;CharS[MAXN][MAXN];intNEXT[MAXN];voidGetNext (Chars[]) {    intI=0, j=-1, n=strlen (s); next[0] = -1;  while(I <N) {if(j==-1|| s[i]==S[j]) next[++i] = + +J; ElseJ=Next[j]; }}BOOLKMP (CharA[],Chars[]) {    intI=0, j=0; intNa=strlen (a), Ns =strlen (s);  while(I <Na) {         while(j==-1|| (A[i]==s[j] && i<Na)) I+ +, J + +; if(j = = Ns)return true; J=Next[j]; }    return false;}intMain () {intT; scanf ("%d", &T);  while(t--)    {        intI, J, Len, M, maxlen= -; CharANS[MAXN] ="Z"; scanf ("%d", &l);  for(i=0; i<m; i++) scanf ("%s", S[i]);  for(len= -; len>=3; len--)         for(i=0; i<=maxlen-len; i++)        {///enumerates all substrings of the first string            Charb[maxn]={0}; strncpy (b, s[0]+I, Len);            GetNext (b);  for(j=1; j<m; J + +)            {                if(KMP (S[j], b) = =false)                     Break; }            if(J==m && strcmp (ans, b) >0) strcpy (ans, b); if(ans[0] !='Z'&& i==maxlen-len) I= -, Len =0;///Jump out of the loop        }        if(ans[0] =='Z') printf ("no significant commonalities\n"); Elseprintf ("%s\n", ans); }    return 0;}

Blue jeans-poj 3080 (multiple strings of common substrings)

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.