hdu1238水题

按题目描述的数据量,暴力是不可能过的,居然过了,太假了。。。

/*

 * hdu1238/win.cpp

 * Created on: 2012-7-31

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

const int MAXN = 105;

char str[MAXN][MAXN];

int minindex, N;



bool judge(const char *s) {

    char s2[MAXN];

    int length = strlen(s);

    for(int i = 0; i < length; i++) {

        s2[i] = s[length - i - 1];

    }

    s2[length] = 0;

    for(int i = 0; i < N; i++) {

        if(i == minindex) {

            continue;

        }

        if(strstr(str[i], s) == NULL && strstr(str[i], s2) == NULL) {

            return false;

        }

    }

    return true;

}



int work() {

    int len = strlen(str[minindex]);

    for(int i = len; i > 0; i--) {

        for(int s = 0; s <= len - i; s++) {

            string temp(&str[minindex][s], i);

            if(judge(temp.c_str())) {

                return i;

            }

        }

    }

    return 0;

}



int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int T;

    scanf("%d", &T);

    while(T--) {

        scanf("%d", &N);

        int minlen = -1;

        for(int i = 0; i < N; i++) {

            scanf("%s", str[i]);

            int temp = strlen(str[i]);

            if(temp > minlen) {

                minlen = temp;

                minindex = i;

            }

        }

        int ans = work();

        printf("%d\n", ans);

    }

    return 0;

}

你可能感兴趣的:(HDU)