poj3450 Corporate Identity


    
    

Description

Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.  After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.  Your task is to find such a sequence.

Input

The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.  After the last trademark, the next task begins. The last task is followed by a line containing zero.

Output

For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.

Sample Input

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

Sample Output

abb
IDENTITY LOST

题意:

给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个。

 直接暴力枚举第一行的每一个字串,在下面的字符串中查找就行了,注意不符合就及时break。
本题用的strstr函数,下面简单介绍一下strstr函数


    
    

PHP语言函数

编辑
strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。
该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。

语法

1
strstr (string,search)
[1]  
参数
描述
string
必需。规定被搜索的字符串。
search
必需。规定所搜索的字符串。

提示

注释: search若是数字,所搜索的将是该数字(作为ASCII码)代表的字符。
注释:该函数是二进制安全的。
注释:该函数对大小写敏感。如需进行大小写不敏感的搜索,请使用 stristr()。

实例

1
<?php  echo  strstr ( "Helloworld!" , "world" );?>
输出:
1
world!

C语言函数

编辑
包含文件: string.h
函数名: strstr
函数原型:
1
extern  char  * strstr ( char  *str1,  const  char  *str2);
语法:
1
strstr (str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。
例子:
1
2
3
char  str[]= "1234xyz" ;
char  *str1= strstr (str, "34" );
cout << str1 << endl;
显示的是: 34xyz

函数实现

1.Copyright 1990 Software Development Systems, Inc.
1
2
3
4
5
6
7
8
9
10
11
12
char  * strstr ( const  char  *s1, const  char  *s2)
{
  int  len2;
  if (!(len2= strlen (s2))) //此种情况下s2不能指向空,否则strlen无法测出长度,这条语句错误
      return ( char *)s1;
  for (;*s1;++s1)
 {
       if (*s1==*s2 &&  strncmp (s1,s2,len2)==0)
       return ( char *)s1;
 }
  return  NULL;
}
2.Copyright 1986 - 1999 IAR Systems. All rights reserved
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char  * strstr (constchar*s1,constchar*s2)
{
     int  n;
     if (*s2)
     {
         while (*s1)
         {
             for (n=0;*(s1+n)==*(s2+n);n++)
             {
                 if (!*(s2+n+1))
                     return ( char *)s1;
             }
             s1++;
         }
         return  NULL;
     }
     else
         return  ( char *)s1;
}
3. GCC-4.8.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
char  * strstr ( const  char  *s1, const  char  *s2)
{
     const  char  *p=s1;
     const  size_tlen= strlen (s2);
     for (;(p= strchr (p,*s2))!=0;p++)
     {
         if ( strncmp (p,s2,size_tlen)==0)
             return  ( char *)p;
     }
     return (0);
}
4.常用经典实现方法
char  * strstr ( const  char  *str1,  const  char  *str2)
{
     char  *cp = ( char *)str1;
     char  *s1, *s2;
 
     if  (!*str2)
         return (( char  *)str1);
 
     while  (*cp)
     {
         s1 = cp;
         s2 = ( char  *)str2;
 
         while  (*s1 && *s2 && !(*s1 - *s2))
             s1++, s2++;
 
         if  (!*s2)
             return (cp);
 
         cp++;
     }
     return (NULL);
}
 

应用举例

// strstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <syslib.h>
#include <string.h>
main()
{
     char  *s= "GoldenGlobalView" ;
     char  *l= "lob" ;
     char  *p;
     clrscr();
     p= strstr (s,l);
     if (p)
         printf ( "%s" ,p);
     else
         printf ( "NotFound!" );
     getchar ();
     return0;
}
//功能:从字串” string1 onexxx string2 oneyyy”中寻找”yyy”
(假设xxx和yyy都是一个未知的字串)
1
2
3
4
5
6
7
char  *s=”string1onexxxstring2oneyyy”;
char  *p;
p= strstr (s,”yyy”);
if (p!=NULL)
     printf (“%s”,p);
else
     printf ( "notfound\n" );
说明:如果直接写语句p=strstr(s,”one”),找到的是onexxxstring2oneyyy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char  *mystrstr( char *s1, char *s2)
{
     if (*s1==0)
     {
         if (*s2)
             return  ( char *)NULL;
         return  ( char *)s1;
     }
     while (*s1)
     {
         int  i=0;
         while (1)
         {
             if (s2[i]==0)
                 return  s1;
             if (s2[i]!=s1[i])
                 break ;
             i++;
         }
         s1++;
     }
     return  ( char *)NULL;
}
 典例博客地址:http://blog.csdn.net/hgj125073/article/details/8444162


#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

char a[4005][205];
int n;

bool cmp(char *x,char *y)
{
    return strlen(x) < strlen(y);
}

int main()
{
    while(scanf("%d",&n) && n)
    {
        for(int i = 1;i <= n;i++)    scanf("%s",a[i]);
        int len = strlen(a[1]);
        char t[205],ans[205] = "";
        for(int i = 0;i < len;i++)
        {
            int cnt = 0;
            for(int j = i;j < len;j++)
            {
                t[cnt++] = a[1][j];
                t[cnt] = 0;
                int flag = 1;
                for(int k = 2;k <= n;k++)
                {
                    if(!(a[k],t))
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    if(strlen(t) > strlen(ans) || strlen(t) == strlen(ans) && strcmp(t,ans) < 0)
                    {
                        memcpy(ans,t,sizeof(t));
                    }
                }
            }
        }
        if(strlen(ans) == 0)    printf("IDENTITY LOST\n");
        else    puts(ans);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值