首页 > 其他 > 详细

Substrings 子字符串-----搜索

时间:2015-01-20 20:26:46      阅读:231      评论:0      收藏:0      [点我收藏+]

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 file 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
 
 
 
 
 
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define maxn 105
char lina[maxn];
char a[maxn][maxn];
int x;
void linstr ()
{
    int i;
    int len = 10000, lens;
    lina[0]=\0;
    scanf("%d", &x);
    for (i = 0; i<x; i++)
    {
        scanf("%s", a[i]);
        int lens = strlen(a[i]);
        if (len>lens)
        {
            strcpy(lina, a[i]);
            len = lens;
        }
    }
	
}
int fin (char str[], char rts[])
{
    int i;
	
    for (i = 0; i<x; i++)
    {
        if (strstr(a[i], str)==0 && strstr(a[i], rts)==0)
            return 0;
    }
    return 1;
}

int fuck ()
{
    int i, len, j;
    len = strlen (lina);
    for (i = len; i>0; i--)
    {
        for (j = 0; j+i<=len; j++)
		{
			char str[maxn]= {0}, rts[maxn];
            strncpy(str, lina+j, i);
            strcpy(rts, str);
            strrev(rts);
            if (fin(str, rts)==1)
                return i;
		}
    }
    return 0;
}
int main ()
{
    int n, num;
	
    scanf("%d", &n);
    while (n--)
    {
        linstr();
        num = fuck();
        printf("%d\n", num);
    }
    return 0;
}

Substrings 子字符串-----搜索

原文:http://www.cnblogs.com/yishilin/p/4237107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!