1. 程式人生 > >hdu 5510 思維加暴力

hdu 5510 思維加暴力

題意 給你 n個字串 求出最大的i使得1-(i-1)中的字串符合不是第i個字串的子串

我們可以發現 i<j<k 如果 i是j的子串 那麼我們在統計k的子串的時候只要將k與j進行比較 不需要與i進行比較

所以我們利用這個性質可以給字串打上標記 找到第一個是子串的位置 否則就更新一遍 答案

時間複雜度是 n^2+n*len

/*
hdu 5510
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <cmath>
#include <stack>
#include <set>
#include <sstream>
#include <map>
using namespace std;
const int MAX_N = 2024;
char str[524][MAX_N];
int flag[MAX_N];
int main(){
    int t;
    scanf("%d",&t);
    for(int Case = 1;Case <= t;Case++){
        int n;
        scanf("%d",&n);
        for(int i = 1;i<=n;++i)
            scanf("%s",str[i]);
        memset(flag,0,sizeof(flag));
        int ans = -1;
        for(int i = 1;i<n;++i){
            for(int j = i+1 ; j<=n;j++){
                if(!flag[j]){
                    if(strstr(str[j],str[i])){
                        break;
                    }
                    else {
                        flag[j] = 1;
                        ans = max(ans,j);
                    }
                }
            }
        }
        printf("Case #%d: ",Case);
        printf("%d\n",ans);
    }
    return 0;
}