hdu-1238-Substrings-c语言

【代码超详解】HDU 1238 Substrings(暴力枚举 + KMP) 一、题目描述 二、算法分析说明与代码编写指导 KMP 算法 最多 10 个样例,每个样例最多 100 个字串,每个字串最大长度为 100,数据规模足够暴力匹配。 从短到长枚举任意一个字符串(这里取第一个)的全部子串,用这个子串去匹配其它字符串。匹配成功就把该子串的长度记为答案。注意在原子串匹配失败时,还要尝试用这个子串的反串去匹配。 头文件 algorithm 里面有比较方便的 std::cop... 阅读详情


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


#include<stdio.h>
#include<string.h>
#include<limits.h>
int getmin(char str[][110],int n)
{
    int min=INT_MAX,a;
    int i;
    for(i=0;i<n;i++)
    {
        if(min>strlen(str[i]))
        {
            min=strlen(str[i]);
            a=i;
        }
    }
    return a;
}
int main()
{
    char s1[110],s2[110];
    char str[110][110];
    int t,n,min,len,max;
    int i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }
        min=getmin(str,n);
        max=0;
        len=strlen(str[min]);
        int flag=0;
        for(i=0;i<len;i++)
        {
            for(j=i;j<len;j++)
            {
                for(k=i;k<=j;k++)
                {
                    s1[k-i]=str[min][k];
                    s2[j-k]=str[min][k];
                }
                s2[j-i+1]=s1[j-i+1]='\0';
                int len1=strlen(s1);
                for(k=0;k<n;k++)
                {
                    if(strstr(str[k],s1)==NULL&&strstr(str[k],s2)==NULL)
                    {
                        flag=0;
                        break;
                    }
                }
                if(len1>max&&flag==1)
                max=len1;
                flag=1;
            }
        }
        printf("%d\n",max);
    }
    return 0;
}


【STM32实物】基于STM32的扫地机器人/小车控制系统设计 扫地机器人系统采用分层结构设计,主要包括底层硬件控制层、中间数据处理层和上层用户交互层。底层硬件控制层负责对各个硬件模块进行控制和数据采集,中间数据处理层负责对采集到的数据进行处理和解算,上层用户交互层负责与用户进行交互并显示系统状态信息。 阅读详情

相关推荐

【嵌入式原理系列-第七篇】ld脚本实战:从零构建MCU内存布局

本文是一篇针对嵌入式开发者的实战指南,详细讲解了如何从零开始为MCU编写ld链接器脚本以构建自定义内存布局。文章以STM32F103C8T6为例,深入剖析了MEMORY和SECTIONS指令的用法,并提供了OTA分区、非易失数据存储等高级定制案例,帮助开发者解决实际工程中的Flash与RAM管理难题,实现对程序运行的深度掌控。

weixin_29271263的博客 246

HDU1238 最长公共子序列

这题是个搜索题。主要思想就是:首先,找到输入字符串中最短的串;然后,枚举其子串和子串的反串。这里求子串要从长度最大到最小,               因为要求的是最大子串。最后,判断枚举的子串是否是公共子串。若是,输出其长度,并结束;反之,继续求解。                       在做题过程中遇到的问题就是,把bool flag1=false,flag2=false写到了for

ON THE WAY TO GEEK 2167

HFSS实战:手把手教你设计一款小型化圆极化微带天线(附仿真文件)

本文详细介绍了使用HFSS设计小型化圆极化微带天线的完整流程,包括工程初始化、辐射贴片设计、仿真设置与优化等关键步骤。通过实战案例,帮助工程师掌握圆极化天线设计技巧,解决轴比控制和耦合馈电匹配等核心问题,适用于物联网终端和便携式设备应用。

weixin_32770423的博客 337

hdu 1238 字符串的简单运用

#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define inf 0x3f3f3f3f #define eps 1e-10 #define maxl 3001

yellowjianokok的博客 398

迈德威视相机调用( 基于 Linux系统+ OpenCV )

迈德威视相机调用( 基于 Linux系统+ OpenCV )前言一、登录迈德威视官网,下载官方SDK二、代码调用与视频帧处理( 基于 OpenCV )总结 前言     此博客记录了迈德威视工业相机在 Linux 系统下的代码调用及其环境配置,通过 OpenCV 对视频帧进行处理。   一、登录迈德威视官网,下载官方SDK      迈德威视官网 : http://www.mindvision.com.cn/     1. 进入官网后,在服务支持中选择软件下载。    2. 选择Linux系统S.

kkkkkk0729的博客 5674

【vivado PG学习】1 PG168:7 Series FPGAs Transceivers官方配置方法学习笔记

介绍: The 7 series FPGAs Transceivers Wizard LogiCORE™ IP 自动创建配置7系列fpga 收发器的HDL封装。向导可以配置一个或多个支持行业主流标准的高速串行收发器。或者从零开始支持各种自定义协议。 功能: 创建配置7系列fpga 收发器的HDL封装。 自动对模拟设置进行配置。 预定义了提供Aurora 8B/10B, Aurora 64B/66B, CEI-6G,DisplayPort, Interlaken, Open Base StationAr..

w0shishabi的博客 5万+

hdu 1238(最长公共子序列变形)

1、http://acm.hdu.edu.cn/showproblem.php?pid=1238 2、题目大意: 给定n个字符串,求出最长的公共子序列,包括反转的也可以 本来觉得是动态规划解决,不过没对,后来看别人的代码,才发现其实就是一道暴搜的题目: 3、代码: #include #include #include using namespace std; string str[105

sdjzping的专栏 1821

数学建模美赛O奖论文研读启示录——从模仿开始

数学建模美赛O奖论文哪些地方比较出彩?我们可以从中学到什么东西?可以从哪些地方进行模仿?我总结了一些O奖论文里我认为比较好的地方,跟大家一起学习交流!

小宇的修炼之路 5429

HDU - 1238——Substrings (查找所有串中最长子串)

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.InputTh...

creator平 784

Android性能优化之Android Profiler

在移动应用开发中,打造一个流畅、省电且不卡顿的用户体验是至关重要的。然而,随着应用功能的日益复杂,内存泄漏、CPU暴涨、网络卡顿以及电量消耗过快等问题常常化身为隐形杀手,悄悄吞噬着用户体验。为了帮助开发者精准定位并解决这些性能瓶颈,谷歌在Android Studio中集成了一款强大的一站式性能分析工具Android Profiler。Android Profiler作为Android Studio内置的实时数据可视化工具,支持运行在Android 5.0及更高版本的设备上。

xiangzhihong8的专栏 197

【Mode】多模干涉仪的设计 Multi-mode interferometer(MMI)

与 Splitter 器件一样,MMI 器件也是一种常见的分光器件,可以根据用户的需求将光束分成不同的比例。本文,我们将介绍如何仿真 MMI 器件。这里我们使用的是 1x2 MMI,其本质上是由四个部分组成的,中间的矩形多模式干涉区域以及左右两侧的三个 taper 模式转换区域。之前的教程中我们也提及了,为了获取稳定的模式场分布,通常,我们需要进行模式收敛性扫描。这里以官网给出的案例为例。如何提取 S 参数我们先暂时不做介绍,后续有时间了我们再做介绍。

u011699626的博客 1212

HDU - 1238 Substrings (kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238点击打开链接SubstringsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11429    Accepted Submission(...

xuejye的博客 309

【ArcGIS微课1000例】0149:Geographic Imager6.6下载安装教程

首先保证电脑上安装对应版本的PS软件,笔者安装的是PS2023(V24.7)版本,然后安装本软件。保持以上窗口运行状态,然后打开PS,自动弹出如下窗口。勾选【Automatically checkout LIcense】,点击Close。双击setup开始安装,点击【确认】,一路默认安装到底。驱动器:DVD-ROM驱动器(用于DVD版本)处理器:2GHz或更快的英特尔或AMD处理器。安装完成后,将Crack中的所有文件拷贝到。硬盘:600 MB的可用硬盘空间用于安装。内存:4GB内存(建议8GB或更高)。

智慧空间实验室 1217

hdu 1238 Substrings(KMP)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238 求个公共字串,正逆序其中一个满足即可。 KMP暴力即可。判断的时候正逆序都去判断即可 代码: #include #include const int N = 105; #define max(a,b) ((a)>(b)?(a):(b)) char s[N][N], rs[N][N];

Remilia's 985

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

HDU1238: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

ACM!荣耀之路! 6972

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Substrings HDU - 1238

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.

码农的一亩三分地 314

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

hdu 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

I wonder how , I wonder why !! 310

Hdu 1238 Substrings

题目大意:给你一系列的字符串,让你从中找寻最大子序列,反向相同也可以。思路:暴力枚举长度最小的字符串的所有情况,这里相当于剪枝。然后根据strstr(char *sz1, char *sz2)判断是否存在于其子串中。CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<...

weixin_30256901的博客 114

hdu1238Substrings

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238 找多个串的最长公共子串。 枚举最短串的子串temp,求子串的反串vs,用其他串与vs或temp匹配(一个成功即可),其他串都存在该子串则更新答案。 用KMP 31ms,用strstr 0ms。 KMP: 1 #include<cstdio> 2 #include&...

weixin_30266885的博客 93

hdu1238 Substrings 暴力过 注意剪枝就好了

#include #include #include using namespace std; #define max 101 int main() { int i,j,k,t,n,m,min; char str[max][max],str1[max],str

408

HDU 1238 Substrings(KMP+暴力枚举)

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.

风的记忆 341

HDU-1238 Substrings

Substrings Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6238Accepted Submission(s): 2767 Problem Descr...

weixin_30800987的博客 76

Substrings - HDU 1238(最大共同子串)

题目大意:给你N个串,求出来他们的最大公共子串的长度(子串反过来也算他们的子串)。 分析:很久以前就做过这道题,当时是用的strstr做的,不过相同的都是枚举了子串......还是很暴力,希望下次遇到类似的题目我已经掌握高效的方法了。 =============================================================================...

weixin_30699235的博客 85

HDU 1238 Substrings(KMP)

该题可以用KMP,但是看网上很多人都是直接暴过去的,好像也差不了多少,可见数据之水。 找到所给字符串中对短的串,枚举其子串的首尾下标,然后取出来作为子串然后对每一个串做KMP,因为其逆序也可以,所以也要将其逆序单独取出来。 细节参见代码: #include #include #include #include #include #include #include #include #incl

AC_Arthur的专栏 813

HDU1238(模拟+枚举)

Substrings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5707    Accepted Submission(s): 2513 Problem Description You are given a num

晓风残月xj 849

hdu1238 字符串应用

题目地址:    http://acm.hdu.edu.cn/showproblem.php?pid=1238 首先将给定的字符串按照长度进行排列;         枚举最短的字符串;         对枚举的每个字符串在其它字符串中进行查找,查看是否是其它字符串的子串;         统计符合条件的字符串的长度,选取最大的长度. PS:不仅要比较子串,还要比较子串的反串. #i

计科菜鸟的天地. 869
上一篇: 字符串的(暴力)枚举连续子串2
下一篇: hdu-1010-Tempter of the Bone
Bigdz
博客等级 码龄10年 10粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值