ACM-字符串处理之Substrings——hdu1238

Substrings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6823    Accepted Submission(s): 3053

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

Author

Asia 2002, Tehran (Iran), Preliminary

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1238

这道题目,可以用搜索来做。

我用的时string.h几个函数。

strcpy:

char * strcpy(char * strDest,const char * strSrc);

就是将后面的数组赋给前面。如果前面数组长度不够大,运行崩溃。

strncpy:

char*strncpy(char*dest,char*src,size_tnum);

这个与strcpy差别在于,它可以控制长度,当然这两者都可以通过控制
第二个数组的头位置来控制赋值起点。

strlen:

extern unsigned int strlen(char *s);

这个就是返回s数组长度(不包括‘\0‘)

strstr:

extern char *strstr(const char *str1, const char *str2);

这个函数就是查找str2在str1中的位置,若不存在返回NULL。

仅这四个函数,即可解这道题。

/**************************************
***************************************
*        Author:Tree                  *
*From :http://blog.csdn.net/lttree    *
* Title : Substrings                  *
*Source: hdu 1238                     *
* Hint  : 串的处理                   *
***************************************
**************************************/

#include <stdio.h>
#include <string.h>
char str[101][101];
int n;
int find_str(int len,int index)
{
    char s[101],pos[101],inv[101];
    bool flag;
    int i,j,l;
    //将字符串拷贝出来
    strcpy(s,str[index]);
    l=len;
    // 舍弃后面的
    while(l>0)
    {
        flag = 0;
        for(i = 0; i <= len-l; ++i)
        {
            flag=1;//flag初始化为1
            //正向字符串,将str中第i个位置,取l长度(舍弃前面的)
            strncpy(pos,s+i,l);
            //inv 为pos的逆序
            for(j=0; j<l; j++)
                inv[j]=pos[l-j-1];
            // 字符数组,最后\0,防止某些不必要的冲突
            pos[l]=inv[l] =‘\0‘;
            for(j=0; j<n; j++)
            {
                if(strstr(str[j],pos)==NULL&&strstr(str[j],inv)==NULL)
                {flag=0;break;}
            }
            if(flag)    break;
        }
        if(flag)    break;
        --l;
    }
    return l;
}

int main()
{

    int i,t,min_len,index;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        min_len = 101;
        for(i = 0; i < n; i++)
        {
            scanf("%s",str[i]);
            //找到最短的字符串
            if(strlen(str[i])<min_len)
            {
                min_len = strlen(str[i]);//记录最短字串的长度
                index = i;//记录最短字串的位置
            }
        }
        printf("%d\n",find_str(min_len,index));
    }
    return 0;
}
时间: 2024-08-08 03:44:01

ACM-字符串处理之Substrings——hdu1238的相关文章

ACM 字符串 题目整理

AC自动机 UVa 11468  Substring AC自动机+概率DP. 注意要补全不存在的边. 为什么要补全不存在的边呢?补全以后可以直接找到状态的转移,即从所有子节点就可以实现所有状态转移. #include<iostream> #include<vector> #include<cmath> #include<map> #include<algorithm> #include<cstring> #include<cst

【好好补题,因为没准题目还会再出第三遍!!】ACM字符串-组合数学(官方题解是数位DP来写)

ACM字符串 1.长度不能超过n 2.字符串中仅包含大写字母 3.生成的字符串必须包含字符串“ACM”,ACM字符串要求连在一块! ok,是不是很简单?现在告诉你n的值,你来告诉我这样的字符串有多少个 输入 输入一个正整数T,代表有T组数据 接下来T行,每行一个正整数n,n<=10. 输出 输出符合条件的字符串的数目 样例输入 1 3 样例输出 1 做题过程: 熬了三四个小时,WA了无数次!最终推出了组合数的公式! 首先暴力打表,嘿嘿!这样极大地压缩计算时间! 打表如下: 一:生成连续的7位绝对

ACM字符串输入问题

坑死了..竟然被这个问题困扰了大半个学期,今天搜来翻去终于弄明白了一些,以后固定用这几种用法好了不然总出错QAQ实际测试例子就没放了,死记这里就够用了T-T 概念: gets()函数:用来从标准输入设备(键盘)读取字符串直到换行符结束. cin 可以连续从键盘读取想要的数据,以空格.tab或换行作为分隔符. scanf("%s",str) 可以连续从键盘读取想要的字符串(数组),以空格.tab或换行作为分隔符. 基本用法: C语言:读入一段带空格的字符串使用gets()        

ACM-ICPC 2014 Beijing Warm-up (北京赛区热身赛)

这次四道题好萌好萌--萌的裁判都一脸血-- 比赛是一次不错的经历,要好好把握锻炼的机会~ 这四题太萌了点反而觉得没有比赛的氛围 不过C题做不出来还是深深感受到不足Q^Q 明天要加油呢-- 我们比赛的时候问了这么个问题--然后被AllTeam广播回复了-- 紧接着就有人问 也被公共频道回复了-- 简述题意: A题 ACM 请数出整本题册中出现的所有"ACM"字符串(大小写均可)的个数. 即数ACM在这本题册里出现了多少次,封面里的要算,封面贴图里的acm也要算,页眉页脚的也要算,题目里出

Substrings(hdu1238)字符串匹配

Substrings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7205 Accepted Submission(s): 3255 Problem Description You are given a number of case-sensitive strings of alphabetic characters, find the

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

ACM中常用算法----字符串

ACM中常用算法--字符串 ACM中常用的字符串算法不多,主要有以下几种: Hash 字典树 KMP AC自动机 manacher 后缀数组 EX_KMP SAM(后缀自动机) 回文串自动机 下面来分别介绍一下: 0. Hash 字符串的hash是最简单也最常用的算法,通过某种hash函数将不同的字符串分别对应到不同的数字.进而配合其他数据结构或STL可以做到判重,统计,查询等操作. #### 字符串的hash函数: 一个很简单的hash函数代码如下: ull xp[maxn],hash[max

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

本文出处:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411 题意:给你一个串,让你依据那个串来输出ppi.坑特别多.ppi的计算方法是dp / inches; dp = sqrt(wp*wp + hp * hp); 现在我来说说这个题目有多坑: 给你的串的格式是这样: name + inches+ "inches"

[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