HDU 1238 Substrings 字符串水题,STL String 的应用
来源:互联网 发布:贵州省大数据局 编辑:程序博客网 时间:2024/05/16 18:38
SubstringsTime Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 19 Accepted Submission(s) : 10Problem DescriptionYou 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.InputThe 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.OutputThere should be one line per test case containing the length of the largest string found.Sample Input23ABCDBCDFFBRCD2roseorchidSample Output22
来源: http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1000&ojid=0&cid=10646&hide=0
详解在注释中
#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;#define MAXN 100*2#define MAXLen 100*2struct String{ string S; int len;}Str[MAXN];int Cal(int n){ int Max_Size = INT_MIN; for(int i=0;i<Str[1].len;i++) { for(int j=i;j<Str[1].len;j++) //枚举最小串的每个子串 { string S(&Str[1].S[i],&Str[1].S[j]+1); //将S初始化为该子串 int flag = 1; for(int m=2;m<=n;m++) //对其他串及其倒置串暴力枚举查找 { int Loc1 = Str[m].S.find(S); reverse(Str[m].S.begin(),Str[m].S.end()); int Loc2 = Str[m].S.find(S); if(Loc1<0&& Loc2<0) flag = 0; } int LENS = S.size(); if(flag) Max_Size = max(Max_Size,LENS); //更新长度 } } return Max_Size>=0?Max_Size:0; //防止没有公共串}int cmp(const void *a,const void *b){ return (*(String *)a).len - (*(String *)b).len;}int main(void){ // freopen("F:\\test.txt","r",stdin); int T;scanf("%d",&T); for(int t=1,n;t<=T&&scanf("%d",&n);++t) { for(int i=1;i<=n;++i) { char temp[MAXLen]; scanf("%s",temp); //定义结构体 内存放Str和它的长度,便于排序 Str[i].S = temp; Str[i].len = Str[i].S.size(); } qsort(Str+1,n,sizeof Str[0],cmp); //找出最小的边 printf("%d\n",Cal(n)); }}
0 0
- HDU 1238 Substrings 字符串水题,STL String 的应用
- HDU 1238 Substrings 【c++ string】
- hdu 1238 Substrings 字符串暴力
- hdu 1238 字符串处理 Substrings
- HDU 1238 Substrings(暴力+stl)
- hdu Substrings(字符串)
- STL string 的应用
- HDU 1283 -- Substrings (string)
- hdu 1238 Substrings(求子串或者他的逆序子串出现在每个字符串中)
- STL string 型的应用
- HDU 1238 Substrings 细心的模拟
- HDU-1113-STL的map<string,string>
- hdu 1894 String Compare 还蛮不错的题目 STL 中的string应用
- 搜索专题(字符串搜索)HDU 1238-Substrings
- String字符串的应用
- hdu-1238 Substrings
- HDU 1238 Substrings
- HDU 1238 ( Substrings )
- Python机器学习与实战笔记之逻辑斯蒂回归Logistic Regression, LR
- Linux 下几款程序内存泄漏检查工具
- C++、Visual C++、MFC(编译和封装)之间的关系是什么?
- guava之比较器(1)
- 谈谈网站防盗链
- HDU 1238 Substrings 字符串水题,STL String 的应用
- 不容易系列之(3)—— LELE的RPG难题 hd 2045
- Java序列化与反序列化
- Java集合排序总结
- hdu5316 Magician (线段树+单点更新+区间查询+区间合并)
- Android的进程与线程
- Leetcode: N-Queens II
- viewpager学习笔记
- 缩小图片的像素减少内存压力