HDU 1238 Substing
思路:
1.找出n个字符串中最短的字符串Str[N]
2.从长到短找Str[N]的子子串 subStr[N],以及subStr[N]的反转字符串strrev(subStr[N]);(从长到短是做剪枝处理)
3.用strstr()函数遍历所有的字符串,看是否含有此子子串subStr[N]或strrev(subStr[N]);只要有一个字符串不包含subStr[N]或strrev(subStr[N])就放弃这个子串,尝试下一个;
4.找到第一个满足要求的就输出strlen(subStr[N])或者strlen(strrev(subStr[N])); 这里可以用string.h头问件中的一个函数strncpy(str1, str2, n);功能:将字符串2中的最多n个字符复制到字符数组1中;
这里可以直接返回n;
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 110
struct MAZE
{
char str[N];
int len;
} maze[N]; bool cmp(MAZE x, MAZE y)
{
return x.len<y.len;
} int solve(int n);
int Find(char str[], int n);
int main()
{
int t, n; scanf("%d", &t);
while(t--)
{
memset(maze, 0, sizeof(maze));
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%s", maze[i].str);
maze[i].len=strlen(maze[i].str);
}
sort(maze, maze+n,cmp);
int ans=solve(n);
printf("%d\n", ans);
}
return 0;
} int solve(int n)
{
char Str[N];
for(int i=maze[0].len; i>0; i--)
{
strcpy(Str, maze[0].str);
strcat(Str, maze[0].str);
for(int j=0; j<maze[0].len; j++)
{
char subStr[N];
memset(subStr, 0, sizeof(subStr));//必须初始化
strncpy(subStr, Str+j, i); if(Find(subStr, n))
return i;
if(Find(strrev(subStr), n))
return i;
}
}
return 0;
} int Find(char s[], int n)
{
for(int i=1; i<n; i++)
{
if(!(strstr(maze[i].str, s)))
return 0;
}
return 1;
}
HDU 1238 Substing的更多相关文章
- (KMP 字符串处理)Substrings -- hdu -- 1238
http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit:1000MS Memory Limit:32768KB ...
- Substrings - HDU 1238(最大共同子串)
题目大意:给你N个串,求出来他们的最大公共子串的长度(子串反过来也算他们的子串). 分析:很久以前就做过这道题,当时是用的strstr做的,不过相同的都是枚举了子串......还是很暴力,希望下次 ...
- hdu 1238 Substrings(kmp+暴力枚举)
Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...
- HDU 1238
好吧,这题直接搜索就可以了,不过要按照长度最短的来搜,很容易想得到. 记得ACM比赛上有这道题,呃..不过,直接搜..呵呵了,真不敢想. #include <iostream> #incl ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- HDU - 2328 Corporate Identity(kmp+暴力)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
- Kuangbin 带你飞 KMP扩展KMP Manacher
首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...
- [kuangbin带你飞]专题1-23题目清单总结
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...
随机推荐
- HTML-CSS文件链接HTML的三种方式
<!--css文本的链接方式有三种:分别是内联定义.链入内部css.和链入外部css--> <!--1.代码为:--> <!--<html> <head ...
- HDU 5892 Resident Evil
题目链接:传送门 题目大意:有50种动物,给你n*n的矩阵,m次操作,P代表加入操作,在左上角 x1,y1 到右下角 x2,y2,的矩形范围内加入 种类为x,数量为y的动物. Q代表询问操作,在左上角 ...
- App Store App申请审核加速
有没有遇到上线后发现很严重的bug这种情况,修复bug后提交审核又是漫长的等待,那样会把人逼疯的. 估计是为了对应这样的情况,Apple提供有一个加速审核的通道: https://developer. ...
- VMware虚拟机Host-Only(仅主机模式)
转载于:https://www.linuxidc.com/Linux/2016-09/135521p3.htm 三.Host-Only(仅主机模式) Host-Only模式其实就是NAT模式去除了虚拟 ...
- 在java中public void与public static void有什么区别 ?
public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...
- JavaScript-烂笔头
JavaScript 对大小写敏感 注释单行用:// 注释多汗用:/* */ 声明变量:var 变量名 (未使用值来声明的变量,值为undefined) JavaScript 变量均为对象 可以使用关 ...
- 关于 Intellij IDEA Ultimate Edition 14.1控制台中文乱码 解决
经过尝试,我发现,乱码主要是跟控制台右下角的编码有关 如下图 当然IDE Encoding 和 Project Encoding 你可以都设置位UTF-8 或者都设置为GBK 如下图:
- WCF引用 代码
方法1: using (ChannelFactory<ICommonService> channelFactory = new ChannelFactory<ICommonServi ...
- shell脚本杂
1.sh -x 跟踪shell脚本中的每个命令 [root@master shellexer]# cat bash.sh #!/bin/bash var=$ echo $var [root@maste ...
- pandas 修改指定列中所有内容
如下图: 读取出来的 DataFrame “code” 列内容格式为:“浪潮信息(000977.XSHE)” 格式,目标效果是:000977.XSHE 代码: df["code"] ...