Substrings--poj1226(字符串)

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 

题目大意:给你n个字符串  要你找这n个字符串中最大的相同字串(字符串可以逆置)

分析:先找到最小的母串  然后这个母串的所有字串从大到小 都和n个串循环一遍  只要找到符合的答案就跳出
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#define N 220
#define INF 0xffffffff
using namespace std;

int main()
{
    int T,n;
    char str[N][N],s1[N],s2[N];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int Min=INF,b;
        for(int i=0; i<n; i++)
        {
            scanf("%s",str[i]);
            if(strlen(str[i])<Min)
            {
                Min=strlen(str[i]);
                b=i;
            }
        }
        int len=Min,flag;
        while(len)
        {
            for(int i=0; i<=Min-len; i++)
            {
                flag=0;
                strncpy(s1,str[b]+i,len);
                int k=0;
                for(int i=len-1; i>=0; i--)
                {
                    s2[k++]=s1[i];
                }
                s1[len]=s2[len]=‘\0‘;
                for(int i=0; i<n; i++)
                {
                    if(strstr(str[i],s1)==NULL && strstr(str[i],s2)==NULL)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                {
                    break;
                }
            }
            if(flag==0)
                break;
            len--;
        }
        printf("%d\n",len);
    }
    return 0;
}
时间: 2024-12-11 11:36:11

Substrings--poj1226(字符串)的相关文章

hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

#1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empty substrin

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

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 conta

Ruby:字符串处理函数

字符串处理函数1.返回字符串的长度 str.length => integer 2.判断字符串中是否包含另一个串 str.include? other_str => true or false "hello".include? "lo" #=> true "hello".include? "ol" #=> false "hello".include? ?h #=> true 3

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题...每次操作,都会拿走一个横行,一个竖行. 所以一共会操作min(横行,竖行)次. #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> #include<algorithm> #include&l

解题报告 之 POJ1226 Substrings

解题报告 之 POJ1226 Substrings 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 li

[LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

字符串(后缀数组):POJ 3415 Common Substrings

Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given two strings A, B and one integer K, we define S, a set of triples (i, j, k): S = {(i, j, k) | k≥K, A(i, k)=B(j, k)}. You are to give

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符

Codeforces Round #166 (Div. 2)---D. Good Substrings(字符串)

You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring s[l-r] (1?≤?l?≤?r?≤?|s|) of string s??=??s1s2-s|s| (where |s| is the length of string s) is string ?slsl?+?1-sr. The substri

UVA 12718 Dromicpalin Substrings(寻找字符串连续子串的回文)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4456 题意:寻找给出字符串的字串中是回文的个数(注意:字串中字母换位置后能组成回文也要算在内,例如:aab之类的可以换位置为:aba 也是一个回文). 思路:只需统计每个字母出现的次数,再统计出现次数中