zoukankan      html  css  js  c++  java
  • POJ 1226 Substrings

    Substrings
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 10262   Accepted: 3525

    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 
    题目大意:找出一组字符串中的最长公共子串或逆序子串的长度
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    using namespace std;
    
    char srcstr[110][110];//输入字符串
    char str[110];//比较字符串
    char strrev1[110];//翻转后的比较字符串
    char strshort[110];//最短的字符串
    int n;
    int ncase;
    
    void StrRev(char* pstr)
    {
        int nLen = strlen(pstr);
        for (int i = nLen - 1; i >= 0; i--)
        {
            strrev1[nLen - i - 1] = pstr[i];
        }
        strrev1[nLen] = '\0';
    }
    
    int Find()
    {
        int nLenshort = strlen(strshort);
        int nLen = nLenshort;
        while(nLen)
        {
            for (int j = 0; j  + nLen <= nLenshort; j++)
            {
                strncpy(str, strshort + j, nLen);
                str[nLen] = '\0';
                StrRev(str);
                for (int k = 0; k < n; k++)
                {
                    if (!strstr(srcstr[k], str) && !strstr(srcstr[k], strrev1))
                    {
                        break;
                    }
                    if (k == n - 1)
                    {
                        return nLen;
                    }
                }
            }
            nLen--;
        }
        return nLen;
    }
    
    int main()
    {
        scanf("%d", &ncase);
        int MinLen = 1000;
        while(ncase--)
        {
            MinLen = 1000;
            scanf("%d", &n);
            for (int i = 0; i < n; i++)
            {
                scanf("%s", srcstr[i]);
                if (MinLen > strlen(srcstr[i]))
                {
                    MinLen = strlen(srcstr[i]);
                    strcpy(strshort, srcstr[i]);
                }
            }
            printf("%d\n", Find());
        }
        return 0;
    }
  • 相关阅读:
    mac xcode 编辑 plist 文件 value列展示不全
    React Native 打包异常:Connect to maven.google.com failed: connect timed out(原创)
    React Native 打包异常:Android resource linking failed(原创)
    面向交易的日内高频量化交易平台笔记
    wpf 样式继承
    wpf staticresource 是不允许向前引用(forward reference)的
    C# 32位程序访问64位注册表
    wpf 可视化树的注意点
    关于64位操作系统使用C#访问注册表失败的问题
    关于 vs 2012 键盘无法输入的问题
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3115696.html
Copyright © 2011-2022 走看看