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 ...
随机推荐
- GL 纹理 格式 资料备份
分别转载至:http://www.tuicool.com/articles/qAbYfq 和 http://www.verydemo.com/demo_c161_i114362.html 在手机 ...
- awk向脚本传递參数(二)
命令行參数的一个重要限制是它们在BEGIN过程中是不可用的. 也就是说,直到首行输入完毕以后它们才可用.为什么?这是一个easy混乱的部分.从命令行传递的參数就好像文件名称一样被处理.赋值操作知道这个 ...
- Linux下文件属性(drwxr-xr-x)详解以及(-rwxrwxrwx=777)(转)
权限的计算是除去第一位字母开始,权限都是三个符号为一组合,其中-表没有这个权限. drwxr-xr-x的意思解释: ls -al 得到如下列表: drwxr-xr-x 4 oracle dba 409 ...
- Android中Bitmap、Drawable、byte[]转换
public byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); ...
- boost::interprocess::shared_memory_object(1)(基本类型)
#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...
- hdu4328(经典dp用悬线法求最大子矩形)
http://wenku.baidu.com/view/728cd5126edb6f1aff001fbb.html 关于悬线法,这里面有详解. 我当时只想到了记录最大长度,却没有想到如果连最左边和最右 ...
- beetl模板的${!}用法
转自:http://ibeetl.com/guide/#beetl 2.20. 安全输出 安全输出是任何一个模板引擎必须重视的问题,否则,将极大困扰模板开发者.Beetl中,如果要输出的模板变量为nu ...
- Windows窗口的层次关系(转)
今天看到这篇文章,觉得蛮有用的,我之前也对这个不大了解,特转载此处. 转载地址:http://www.51testing.com/html/200804/n80848.html 在Window 的图形 ...
- Spring MVC 多语言化的实践和学习
一.主要参考: SpringMVC简单实现国际化/多语言 - CSDN博客 https://blog.csdn.net/u013360850/article/details/70860144/ 二.总 ...
- macro-name replacement-text 宏 调试开关可以使用一个宏来实现
C++ 预处理器_w3cschool https://www.w3cschool.cn/cpp/cpp-preprocessor.html C++ 预处理器 预处理器是一些指令,指示编译器在实际编译之 ...