Gym 100703A Tea-drinking 最小生成树

A. Tea-drinking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Castles Valley was lit by the noonday sun. One could descry two figures on a balcony of one of Castles — a big and a small ones. Dragon and Princess were drinking tea. Dragon was fond of preparing tea compositions, and he was very glad that Princess was drinking tea with great pleasure and listened with interest to his narrations on how one or another tea was composed.

Dragon always puts into a teapot m teaspoons of ingredients, which he calls portions. If it is written "put two portions of Black Ceylon Tea, add one portion of currant leaves, one portion of cornflowers, and after put another portion of Black Ceylon Tea and a portion of calendula petals" in a recipe, Dragon puts the portions to the teapot exactly in the described order: he is absolutely sure that the taste of a tea depends on the order in which ingredients are put.

Dragon told Princess that once upon a time his friend Knight imparted to him a recipe of a delicious tea, and since then he, Dragon, got into making tea compositions. He experimented with replacing some ingredients with some another ones in this recipe, and when he liked the taste of a brand new tea, he wrote down the resulting recipe. And he has done it so many times, choosing one of the written recipes as a basic one for a new recipe each time.

Dragon has an original system of composing teas and writing their recipes. He denotes all of used ingredients by lowercase Latin letters (each letter corresponds to some ingredient, the same ingredients denoted by the same letters, and different ingredients denoted by different letters). So, a tea description is a string of m letters in the same order of ingredients as in the recipe.

When Dragon was inventing the notation, he supposed that more similar (in his viewpoint) ingredients should be denoted by more closely spaced (in alphabetical order) symbols. Indeed, it is much less radical solution to replace currant leaves with blackberry leaves, than to replace calendula petals with a cinnamon. For this reason, Dragon defined the distance between recipes as a maximum absolute value of a distances between ingredients at the corresponding positions in the recipes.

Dragon writes down each recipe on a separate sheet of paper and keeps all these sheets in a special box. Since that, we cannot know the maximum amongst all of the distances between the recipes. However, Dragon assures that maximum distance is the minimum possible.

You are given n descriptions of teas composed by Dragon. Your task is to calculate the minimum value of maximal distance between the recipes.

Input

The first line contains integers n and m (2 ≤ n ≤ 1000, 1 ≤ m ≤ 30) — the number of recipes and the number of ingredients in each recipe.

Each of next n lines contains one recipe — the string of m lowercase latin letters. It is guaranteed that all recipes are different.

Output

Print the single integer — minimum value of maximal distance between the recipes.

Sample test(s)
input
5 2 fb ga ef dc fd 
output
2

题目链接:http://codeforces.com/gym/100703/problem/A

题目大意:给定n个字符串,每个串的长度为m,对于两个串来说,定义两个串的距离为所有对应位置字符的差值的绝对值的最大值。问如何排列,可使从上到下,所有相邻的字符串间的距离最大值最小。

思路:将每个字符串看作一个点,两点间的距离可以O(m)的求出来,因为有n个点,所以,求出一个完全图所需要的时间复杂度就是O(n^2*m),然后因为需要的是一个生成树,对该完全图求最小生成树即可,答案即为生成树最后所选择的一条边的权值。

AC代码:

 #include <vector>

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
#define MAXN 1010
#define MAXM 50
vector<pair<int, pair<intint> > >edge;
int n, m;
char str[MAXN][MAXM];
int father[MAXN];
int find_set(int x) {
    if(father[x] != x) {
        father[x] = find_set(father[x]);
    }
    return father[x];
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        father[i] = i;
    for(int i = 1; i <= n; i++) {
        scanf("%s", str[i]);
        for(int j = 1; j < i; j++) {
            int value = 0;
            for(int k = 0; k < m; k++) {
                value = max(value, abs(str[i][k] - str[j][k]));
            }
            edge.push_back(make_pair(value, make_pair(i, j)));
        }
    }
    sort(edge.begin(), edge.end());
    int num = edge.size();
    int ans = 0;
    for(int i = 0; i < num; i++) {
        int x = find_set(edge[i].second.first);
        int y = find_set(edge[i].second.second);
        if(x != y) {
            father[y] = x;
            ans = edge[i].first;
        }
    }
    printf("%d\n", ans);
    return 0;
}
posted @ 2015-07-27 19:13  gaoxiang36999  阅读(150)  评论(0编辑  收藏  举报