zoukankan      html  css  js  c++  java
  • HDU-1238-Substrings(KMP, 暴力枚举)

    链接:

    https://vjudge.net/problem/HDU-1238

    题意:

    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.

    思路:

    n个字符串的最长子串, 反着的也可以.
    枚举第一个的子串, 对剩下的挨个匹配.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int MAXN = 200;
    const int MOD = 1e4+7;
    
    string ori[MAXN], rev[MAXN];
    int Next[MAXN];
    int n;
    
    void GetNext(string t)
    {
        int i = 0, k = -1;
        int len = t.length();
        Next[0] = -1;
        while (i < len-1)
        {
            if (k == -1 || t[i] == t[k])
            {
                ++i;
                ++k;
                Next[i] = k;
            }
            else
                k = Next[k];
        }
    }
    
    
    bool Kmp(string s, string t)
    {
        int i = 0, j = 0;
        int lens = s.length(), lent = t.length();
        while (i < lens && j < lent)
        {
            if (j == -1 || s[i] == t[j])
            {
                ++i;
                ++j;
            }
            else
                j = Next[j];
        }
        return j >= lent;
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t;
        cin >> t;
        while (t--)
        {
            cin >> n;
            for (int i = 1;i <= n;i++)
            {
                cin >> ori[i];
                rev[i].assign(ori[i].rbegin(), ori[i].rend());
            }
            int len = ori[1].length();
            int ans = 0;
            for (int i = 0;i < len;i++)
            {
                for (int j = i;j < len;j++)
                {
                    string p(ori[1], i, j-i+1);
                    GetNext(p);
                    bool flag = true;
                    for (int k = 2;k <= n;k++)
                    {
                        if (!Kmp(ori[k], p) && !Kmp(rev[k], p))
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag && (int)p.length() > ans)
                        ans = (int)p.length();
                }
            }
            cout << ans << endl;
        }
    
        return 0;
    }
    
  • 相关阅读:
    某个牛人做WINDOWS系统文件详解
    常用ASP脚本程序集锦
    LINUX基础:文件安全与权限
    proftpd+mysql+quota
    apache2.0.49tomcat5.0.19jk2建立virtualHost
    URL Redirection(转) Anny
    顶级域名后缀列表(转) Anny
    \u4E00\u9FA5意义 Anny
    How to POST Form Data Using Ruby(转) Anny
    How to get rid of 'Enter password to unlock your login keyring' in Ubuntu(转) Anny
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11594230.html
Copyright © 2011-2022 走看看