首页 > 编程语言 > 详细

题解— blue jeans (KMP算法)

时间:2020-08-22 12:35:40      阅读:79      评论:0      收藏:0      [点我收藏+]

题目链接:https://cn.vjudge.net/contest/388654#problem/A

字符串匹配kmp模板题

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <string>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;
typedef long long ll;

string s[15], strmax;

int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        strmax = "";
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> s[i];
        }
        for (int i = 0; i <= 60; i++)
        {
            for (int j = 0; j <= 60; j++)
            {
                int flag = 0;
                string temp = s[0].substr(i, j);
                for (int k = 1; k < m; k++)
                {
                    if (s[k].find(temp) == string::npos)
                    {
                        flag = 1;
                        break;
                    }
                }
                if (!flag)
                {
                    if (temp.size() > strmax.size())
                    {
                        strmax = temp;
                    }
                    else if (temp.size() == strmax.size() && temp < strmax)
                    {
                        strmax = temp;
                    }
                }
            }
        }
        if (strmax.size() < 3) cout << "no significant commonalities" << endl;
        else cout << strmax << endl;
    }
    return 0;
}

 

题解— blue jeans (KMP算法)

原文:https://www.cnblogs.com/zny0222/p/13544801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!