zoukankan      html  css  js  c++  java
  • 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]='';
                    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
    ",len);
        }
        return 0;
    }
  • 相关阅读:
    定制可引导的iso镜像
    镜像封装
    nginx知识及nginx集群搭建
    注册系统服务
    安装pip
    获取硬件信息
    查看网卡信息
    python paramiko模块
    python pexpect模块
    python-nmap的使用
  • 原文地址:https://www.cnblogs.com/linliu/p/5162430.html
Copyright © 2011-2022 走看看