zoukankan      html  css  js  c++  java
  • POJ 3450 Corporate Identity kmp+最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配。
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include<iostream>
      5 #include<cstdlib>
      6 #include<string>
      7 #include<cmath>
      8 #include<vector>
      9 using namespace std;
     10 const int maxn=1e5+7;
     11 const double eps=1e-8;
     12 const double pi=acos(-1);
     13 const int inf = 0x3f3f3f3f;
     14 #define ll long long
     15 #define clc(a,b) memset(a,b,sizeof(a))
     16 
     17 int next[220];
     18 char str[4010][220];
     19 
     20 void getnext(char *t)
     21 {
     22     int i=0,j=-1;
     23     int len=strlen(t);
     24     next[0]=-1;
     25     while(i<len)
     26     {
     27         if(t[i]==t[j]||j==-1)
     28         {
     29             i++;
     30             j++;
     31             next[i]=j;
     32         }
     33         else
     34             j=next[j];
     35     }
     36 }
     37 
     38 int kmp(char *s,char *t)
     39 {
     40     int lens=strlen(s);
     41     int lena=strlen(t);
     42     int i=0,j=0;
     43     while(i<lens&&j<lena)
     44     {
     45         if(s[i]==t[j]||j==-1)
     46         {
     47             i++;
     48             j++;
     49         }
     50         else
     51             j=next[j];
     52     }
     53     if(j<lena)
     54         return -1;
     55     return i-lena;
     56 }
     57 
     58 int main()
     59 {
     60     int n,len;
     61     while(cin>>n&&n)
     62     {
     63         char tmp[220];
     64         int minn=inf;
     65         for(int i=0;i<n;i++)
     66         {
     67             scanf("%s",str[i]);
     68             len=strlen(str[i]);
     69             if(minn>len)
     70             {
     71                 minn=len;
     72                 strcpy(tmp,str[i]);
     73             }
     74         }
     75         len=strlen(tmp);
     76         char p[220];
     77         char f[220]={0};
     78         int ans=0;
     79         for(int i=1;i<=len;i++)
     80         {
     81             int cnt;
     82             for(int j=0;j+i<=len;j++)
     83             {
     84                 cnt=0;
     85                 strncpy(p,tmp+j,i);
     86                 p[i]='';
     87                 getnext(p);
     88                 for(int k=0;k<n;k++)
     89                 {
     90                     if(kmp(str[k],p)!=-1)
     91                     {
     92                         cnt++;
     93                     }
     94                     else
     95                         break;
     96                 }
     97                 if(cnt==n)
     98                 {
     99                     ans++;
    100                     if(strlen(f)<strlen(p))
    101                         strcpy(f,p);
    102                     else if(strcmp(f,p)>0)
    103                         strcpy(f,p);
    104                 }
    105             }
    106         }
    107         if(ans==0)
    108             printf("IDENTITY LOST
    ");
    109         else
    110             printf("%s
    ",f);
    111     }
    112 }
    View Code
  • 相关阅读:
    多级部署下的SuperMap iServer 2.0 JS 聚合功能(一)
    Kubernetes&Docker集群部署
    股票数据存储系统(KeyValue存储)设计与实现
    Ajax+Tornado模拟长、短轮询
    REST架构网站改写:前端MVC Angular.js,Web框架 Express.js, 数据库 MongoDB
    SQLite数据库C++ API封装
    一致性哈希(Consistent Hashing)算法的C++实现
    数据结构——排序
    数据结构——折半查找
    索引学习笔记
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5243795.html
Copyright © 2011-2022 走看看