题解— blue jeans (KMP算法)

题目链接: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;
}

 

posted @ 2020-08-22 11:13  章楠雨  阅读(144)  评论(0编辑  收藏  举报