poj3080 Blue Jeans

//题意:给定t组数据,每一组数据有m组字符串,寻找这m条字符串的最长字符串,若长度相等输出字典数最小的,即总头到位比较最小的字符串
//思路:暴力以第一条串为母串从3个枚举到60个,并与其他串进行比较,然后根据题目要求输出
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);    
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        int m;
        cin >> m;
        string s[15];
        for(int i = 0; i < m; i++){
            cin >> s[i];
        }
        string res = ""; //出当前测试最长串
        for(int i = 3; i <= 60; i++){
            for(int j = 0; j <= 60-i; j++){
                string test = s[0].substr(j, i);
                bool flag = true;
                for(int k = 1; k < m; k++){
                    if(s[k].find(test) == string :: npos){  //作为一个返回值表示没有匹配到的话
                        flag = false;
                        break;
                    }
                }
                if(flag && test.length() > res.length()){   //长度不等选长的
                    res = test;
                }
                else if(flag && test.length() == res.length() && test < res){   //长度相等选字典数小的
                    res = test;
                }
            }
        }
        if(res == "") cout << "no significant commonalities\n";
        else cout << res << '\n';
    }
    return 0;
}

ios::sync_with_stdio(false);   

 //cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输出顺序错乱。正因为这个兼容性的特性,导致cin有许多额外的开销,如何禁用这个特性呢?只需一个语句std::ios::sync_with_stdio(false);,这样就可以取消cin于stdin的同步了。

substr(a,b)表示从a下标开始截取长度为b的字符串

find函数会返回第一次碰到该串的第一个下标,string::npos作为返回值表示没有匹配到

猜你喜欢

转载自blog.csdn.net/small__snail__5/article/details/80157720