zoukankan      html  css  js  c++  java
  • HDU 5510 Bazinga KMP

    Bazinga

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 4518    Accepted Submission(s): 1442


    Problem Description
    Ladies and gentlemen, please sit up straight.
    Don't tilt your head. I'm serious.

    For n given strings S1,S2,,Sn, labelled from 1 to n, you should find the largest i (1in) such that there exists an integer j (1j<i) and Sj is not a substring of Si.

    A substring of a string Si is another string that occurs in Si. For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".
     
    Input
    The first line contains an integer t (1t50) which is the number of test cases.
    For each test case, the first line is the positive integer n (1n500) and in the following n lines list are the strings S1,S2,,Sn.
    All strings are given in lower-case letters and strings are no longer than 2000 letters.
     
    Output
    For each test case, output the largest label you get. If it does not exist, output 1.
     
    Sample Input
    4 5 ab abc zabc abcd zabcd 4 you lovinyou aboutlovinyou allaboutlovinyou 5 de def abcd abcde abcdef 3 a ba ccc
     
    Sample Output
    Case #1: 4 Case #2: -1 Case #3: 4 Case #4: 3
     
    Source
     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INFLL   0x3f3f3f3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    typedef pair<double, double> PII;
    
    const int MX = 2000 + 5;
    const int MX2 = 500 + 5;
    
    /*
    void GetNext() {
       Next[0] = 0;
       for(int i = 1; i < n; i++) {
           int j = Next[i - 1];
           while(j && S[i] != S[j]) j = Next[j - 1];
           Next[i] = S[i] == S[j] ? j + 1 : 0;
       }
    }
    */
    int Next[MX], n;
    int KMP(char *A, char *B) {
        int m = strlen(A), n = strlen(B);
        Next[0] = 0;
        for(int i = 1; i < n; i++) {
            int k = Next[i - 1];
            while(B[i] != B[k] && k) k = Next[k - 1];
            Next[i] = B[i] == B[k] ? k + 1 : 0;
        }
        int ans = 0, j = 0;
        for(int i = 0; i < m; i++) {
            while(A[i] != B[j] && j) j = Next[j - 1];
            if(A[i] == B[j]) j++;
            if(j == n) ans++;
        }
        return ans;
    }
    
    char ch[MX2][MX];
    bool vis[MX2];
    
    int main() {
        //FIN
        int T;
        int cnt = 1;
        scanf("%d", &T);
        while(T--) {
            int n;
            scanf("%d", &n);
            memset(vis, 0, sizeof(vis));
            for(int i = 1; i <= n; i++) scanf("%s", ch[i]);
            for(int i = 2; i <= n ;i++)
                if(KMP(ch[i], ch[i-1])) vis[i-1] = 1;
    
            int flag = 0;
            for(int i = n; i >= 1; i--) {
                for(int j = 1; j < i; j++) {
                    if(!vis[j])
                    if(KMP(ch[i], ch[j]) == 0) {
                        flag = 1;
                        printf("Case #%d: %d
    ", cnt++, i);
                        break;
                    }
                }
                if(flag) break;
            }
            if(!flag) printf("Case #%d: -1
    ", cnt++);
        }
        return 0;
    }
    

      

  • 相关阅读:
    SwiftyUserDefaults对NSUserDefaults的封装扩展
    干货-iOS、mac开源项目及库,以后我也会持续更新。
    swift
    swift 集成使用最新版百度地图_v2.10.2(一)
    Xcode git 忽略user interface state文件
    ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)
    swift--字符串替换/过滤/切割
    干货分享--iOS及Mac开源项目和学习资料【超级全面】
    zookeeper和Eureka的区别
    Date工具遇到的一个坑
  • 原文地址:https://www.cnblogs.com/Hyouka/p/7450359.html
Copyright © 2011-2022 走看看