ACM-字符串处理之Substrings——hdu1238
来源:互联网 发布:mysql日期格式化 编辑:程序博客网 时间:2024/05/16 00:53
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
23ABCDBCDFFBRCD2roseorchid
Sample Output
22
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;}
3 0
- ACM-字符串处理之Substrings——hdu1238
- Substrings(hdu1238)字符串匹配
- HDU1238 Substrings
- hdu1238 Substrings
- HDU1238:Substrings
- HDU1238 Substrings
- hdu1238---Substrings
- hdu1238 Substrings
- HDU1238-Substrings
- hdu1238 Substrings (暴力)
- HDU1238:Substrings(暴力枚举)
- HDU1238 Substrings (暴力)
- hdu1238(Substrings)
- HDU1238 Substrings(KMP+穷举)
- ACM之字符串处理
- hdu1238——Substrings——————【求多个字串的最长公共子串】
- ACM--字符串--Two Substrings--水
- hdu 1238 字符串处理 Substrings
- HDU 1002.A + B Problem II
- HashMap实现缓存
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- 常用库函数摘要
- codeforces Sereja and Dima 题解
- ACM-字符串处理之Substrings——hdu1238
- nodejs详解
- 拆解报告:谷歌眼镜硬件成本约80美元
- mongodb使用笔记
- 设计模式(笔记)概述
- hdu 1045
- 美政府挑选三家私企开发商业月球着陆器
- 网站加载优化
- 《程序员面试宝典》读书笔记