POJ 1238 Substrings

Problem 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

题意:n个字符串,找出这n个字符串中连续相同的最大子序列,子串的逆序也可以。

#include<stdio.h>
#include<string.h>

const int N=110;

char str[N][N], resouce[N];
int n;

void Init() ///在输入数据时找到最短的那条字符串,只有这样才能保证找到的子串有可能是公共子串
{
    int i;

    scanf("%d", &n);
    scanf("%s", str[0]);

    strcpy(resouce, str[0]);

    for (i = 1; i < n; i++)
    {
        scanf("%s", str[i]);
        if (strlen(str[i]) < strlen(resouce))
            strcpy(resouce, str[i]);
    }
}

int Judge(char sub[], char resub[]) ///判断找到的子串和其逆序是否是其它字符串的子串
{
    int i;

    for (i = 0; i < n; i++)
    {
        if (strstr(str[i], sub) == NULL && strstr(str[i], resub) == NULL)
            return 0; ///只要在一个字符串中没有找到该子串和其逆序,则该子串不是公共子序列
    }

    return 1;
}

int Sub()
{
    int i, j, len1 = strlen(resouce), k, t;
    char sub[N], resub[N];

    for (i = len1; i >= 1; i--)
    {
        for (j = 0; i+j <= len1; j++) ///从最长的子串查找,只要找到了属于所有字符串的子串,最长公共子序列就是该子串,便可停止查找
        {
            memset(sub, 0, sizeof(sub)); ///最短字符串的子串
            memset(resub, 0, sizeof(resub)); ///子串的逆序
            k = 0;

            strncpy(sub, resouce+j, i);
            for (t = strlen(sub)-1; t >= 0; t--)
                resub[k++] = sub[t];
            resub[k] = ‘\0‘;

            if (Judge(sub, resub))
                return strlen(sub);
        }
    }

    return 0;
}

int main ()
{
    int T, len;

    scanf("%d", &T);

    while(T--)
    {
        memset(resouce, 0, sizeof(resouce));
        Init();

        len = Sub();

        printf("%d\n", len);
    }

    return 0;
}
时间: 2024-11-20 04:47:32

POJ 1238 Substrings的相关文章

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]都标记为一个数字(这个数字意思是第几个读入的字符

POJ 1226 Substrings (后缀数组)

题目大意: 问的是m个字符串里,都出现过的子串.子串也可以出现在这个串的逆序串中. 思路分析: 居然wa在全5个 "a" 的数据上. 二分的时候下界不能为0.. 思路大致上是把原串和逆序串全部处理出来,放入str中,然后在每个串中间加一个没有出现过的. 此处注意输入不仅仅是字母. 然后跑一遍后缀数组. 然后用标记计数就好了. #include <iostream> #include <cstdio> #include <algorithm> #inc

hdu 1238 Substrings (暴搜,枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8391    Accepted Submission(s): 3862 Problem Description You are given a number of

HDU 1238 Substrings (水)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238 枚举最短字符串的的每个子串然后暴力....我能怎么办,我也很无奈啊 代码: 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <ca

poj Common Substrings(后缀数组&amp;单调队列)

Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7082   Accepted: 2355 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

POJ 1225 Substrings

http://poj.org/problem?id=1226 题意:给定n个串.求一个最长的串,使得这个串或者其反串在每个串中都出现过? 思路:先在大串里面加入正反串,然后二分,判定即可. 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 int num[200005],ws[200005],w

POJ 1226 Substrings

Substrings Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 122664-bit integer IO format: %lld      Java class name: Main You are given a number of case-sensitive strings of alphabetic characters, find the lar

poj 3261 后缀数组 找重复出现k次的子串(子串可以重叠)

题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 但是大部分时间是在调试代码,因为模板的全局变量用混了,而自己又忘了,,,等西安邀请赛还有四省赛结束之后,该冷静反思下尝试拜托模板了 错误   :1.k用错,题目的k和模板的k用混; 2.还是二分的C()函数,这个其实跟前一篇<poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题>的C函数

poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)

题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自己又忘了.,,等西安邀请赛还有四省赛结束之后,该冷静反思下尝试拜托模板了 错误   :1.k用错,题目的k和模板的k用混; 2.还是二分的C()函数,这个事实上跟前一篇<poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题>的C函