1238 Substrings

本文介绍了一种查找最长子串的算法,通过遍历给定字符串集合中的首个字符串的所有子串,并与剩余字符串进行匹配来确定最长子串。文章详细解释了算法流程,并提供了完整的C++实现代码。

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<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(const string s1, const string s2){
    return s1.length() < s2.length();
}
int main(){
    for (int case_num; scanf("%d", &case_num) != EOF;){
        while (case_num--){
            int num;
            scanf("%d", &num);
            vector<string>vec(num);
            for (int i = 0; i < num; i++){
                cin >> vec[i];
            }
            sort(vec.begin(), vec.end(), cmp);
            int max_len = 0;
            for (int i = 0; i < vec[0].length(); i++){
                for (int len = 1; len <= vec[0].length() - i; len++){
                    string sub_str = vec[0].substr(i, len);
                    int flag = 0;
                    for (int index1 = 1; index1 < vec.size(); index1++){
                        //cout << "sub_str:" << sub_str << " str:" << vec[index1] << " find:" << vec[index1].find(sub_str, 0) << endl;
                        if (vec[index1].find(sub_str, 0) >= vec[index1].length()){
                            flag = 1;
                            break;
                        }
                    }
                    if (!flag){
                        if (len >= max_len){
                            max_len = len;
                        }
                        continue;
                    }
                    reverse(sub_str.begin(), sub_str.end());
                    flag = 0;
                    for (int index1 = 1; index1 < vec.size(); index1++){
                        //cout << "sub_str:" << sub_str << " str:" << vec[index1] << " find:" << vec[index1].find(sub_str, 0) << endl;
                        if (vec[index1].find(sub_str, 0) >= vec[index1].length()){
                            flag = 1;
                            break;
                        }
                    }
                    if (!flag){
                        if (len >= max_len){
                            max_len = len;
                        }
                        continue;
                    }
                }
            }
            printf("%d\n", max_len);
        }
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值