poj 1226 Substrings(c风格字符串的库函数的使用)

Substrings
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9527   Accepted: 3272

Description

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.

Output

There should be one line per test case containing the length of the largest string found.

Sample Input

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2 

Source

注意:
(1)函数原型:extern char *strstr(char *str1, char *str2);    功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。判断一个子串是否在一个字符串中出现
(2)原型:char *strcpy(char *dest, char *src);   功能:把src所指由'\0'结束的字符串复制到dest所指的数组中。
 ( 3) 原型:char * strncpy(char *dest, char *src, size_t n);   功能:将字符串src中最多n个字符复制到字符数组dest中(它并不像strcpy一样只有遇到NULL才停止复制,而是多了一个条件停止,就是说如果复制到第n个字符还未遇到NULL,也一样停止),返回指向dest的指针。
(4) strlen(char *str) 计算c风格字符串的长度。
(5)题意解析:
  a) 这里首先从所有的字符串中找出最短的那个字符串作为源字符串。
  b) 然后按照从长到短的枚举方法一个一个的查找是否其他的字符串是否均含有该源字符串的子字符串。这里借助于库函数strstr
  c) 遍历方法,首先是整个源字符串,然后是源字符串长度减少1的所有字符串,然后是减少2的所有字符串,如果找到就跳出来输出。
(6)另外注意的是这里还要求了也可以是某个字符串的翻转字符串也可以。
 
 
#include <stdio.h>
#include <string.h>

int main()
{
    char string[105][105],str[105],pos[105],inv[105];
    int i,j,t,n,min_len,index;
    int flag;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        min_len = 105;
        for(i = 0; i < n; i++)
        {
            scanf("%s",string[i]);
            //找到最短的字符串
            if(strlen(string[i])<min_len)
            {
                min_len = strlen(string[i]);
                index = i;
            }
        }
        int len;
        len = min_len;
        strcpy(str,string[index]);//将字符串拷贝出来
        while(len>0)
        {
            flag = 0;
            for(i = 0; i <= min_len-len;i++)
            {
                flag = 1;//标记为符合
                strncpy(pos,str+i,len);//正向字符串
                for(j = 0;j<len;j++)
                inv[j] = pos[len-j-1];//逆向字符串
                pos[len] = inv[len] ='\0';
                for(j = 0;j<n;j++)
                {
                    /**
                    包含文件:string.h
                  函数名: strstr
                  函数原型:extern char *strstr(char *str1, char *str2);
                  功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。
                    */

                    if(strstr(string[j],pos)==NULL&&strstr(string[j],inv)==NULL)
                    {
                        flag = 0;//该字符串不符合
                        break;
                    }
                }
                if(flag)break;//符合则说明该字符串在每个字符串中都有,若没找到继续循环
            }
            if(flag)break;//同上
            len--;
        }
        printf("%d\n",len);
    }
    return 0;
}

 

posted on 2012-10-03 19:29  NewPanderKing  阅读(1314)  评论(0编辑  收藏  举报

导航