poj 3080 Blue Jeans

本文介绍了一个使用C++ STL解决字符串匹配问题的例子。通过PoJ 3080题目的解答展示了如何利用STL中的字符串操作简化代码实现,提高程序效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c++的stl等东西在很多时候的确比C语言方便,减少代码又提高效率。

/*
Poj: 3080 Blue Jeans
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>

#define StrLen 60

using namespace std;

string str[11];
int n;

int main()
{
    //freopen("data.in", "rb", stdin);
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            cin >> str[i];
        
        string res = "";
        for(int i = 3; i <= StrLen; i++) {
            for(int j = 0; j <= StrLen - i; j++) {
                string tmp = str[0].substr(j, i);
                bool flag = true;
                for(int k = 1; k < n; k++) {
                    if(str[k].find(tmp) == string::npos) {
                        flag = false;
                        break;
                    }
                }
                if(flag && res.size() < tmp.size())
                    res = tmp;
                else if(flag && tmp.size() == res.size() && tmp < res)
                    res = tmp;
            }
        }
        
        if(res == "")
            printf("no significant commonalities\n");
        else
            cout << res << endl;
    }
    
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值