POJ 3080 Blue Jeans KMP

题意:找出所有字符串中最长相同字串

解题思路:枚举KMP

解题代码:

  1 // File Name: getnext.cpp
  2 // Author: darkdream
  3 // Created Time: 2014年09月09日 星期二 22时35分02秒
  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 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long 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     int 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           return 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     }
134     return 0;
135 }

时间: 2024-08-02 21:40:55

POJ 3080 Blue Jeans KMP的相关文章

POJ 3080 Blue Jeans(KMP 最长公共子串)

Blue Jeans Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you ha

poj 3080 Blue Jeans (kmp暴力)

# include <stdio.h> # include <algorithm> # include <cstring> using namespace std; int next[100]; char pat[100]; char a[100][100]; int ma; int lenp; int n; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=lenp) { if(j==-1||pat[j]==

POJ 3080 Blue Jeans KMP解法

使用KMP寻找最长的前缀的方法,比一般的暴力法有快了很多. 本题一般的暴力法需要的是O(m*n*n*n),其中m是有多少字符串,而n是字符串长度,而使用KMP就可以把时间效率提高到O(m*n*n),减少了一个n,提高了一个档次啦. 速度快很多. 准确来说应该是利用KMP寻找一个字符串A,在另一个字符串B任意位置出现的A的最长的前缀字符串. 理解好KMP的next table就好办了.每次查找到相等字符的时候,保存好最长的前缀. 注意本题的条件:选取最前的字典顺序输出.老害我错的条件. #incl

POJ 3080 Blue Jeans Trie后缀树解法

题目是牛仔裤的意思,不过看不出题意和Blue Jeans有什么关系. 本题的数据是很水的,数据量小,故此可以使用非常暴力的方法过,也可以使用不那么暴力的KMP过. 这里使用更加不暴力的Trie后缀树过,这种解法就一点都不水了,呵呵. 思路: 1 建立所有字符串的后缀Trie树 2 增加额外信息,看每过路径是否是所有的字符串都经过了,如果是,那么就是合法的字符串了,查找最长的这样的字符串 3 优化一下:如果不是所有字符串的经过的路径,那么就可以直接返回,不往下搜索了 最后,我发现删除Trie都是很

POJ 3080 Blue Jeans 三种暴力法

本题可以使用暴力法直接求解,思路也挺简单的,不过实现起来也挺麻烦的. 本题最暴力直接使用strstr过. 这里使用hash表的方法过,这种方法好像有个学名的,主要思路就是把一个需要查找的字符串赋予一个数值,那么就可以把一串字符串的比较转换为一个值的比较了,那么就可以加速字符串的查找了. #include <stdio.h> #include <string.h> #include <stdlib.h> const long long MOD = (int)(1E9+7)

poj 3080 Blue Jeans (KMP)

http://poj.org/problem?id=3080 Blue Jeans Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populate

poj 3080 Blue Jeans 暴力

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14283   Accepted: 6356 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

poj 3080 Blue Jeans(kmp)

Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tas

(kmp)poj 3080 ——Blue Jeans

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tasked with wri