LeetCode: Anagrams [048]

Background

Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions
and analyzing the topology of an n-dimensional hypercube. The former is much more complicated than its one dimensional relative while the latter bears a remarkable resemblance to its ``lower-class‘‘ cousin.

The Problem

Consider an n-dimensional ``box‘‘ given by its dimensions. In two dimensions the box (2,3) might represent a box with length 2 units and width 3 units. In three dimensions the box (4,8,9) can represent
a box (length, width, and height). In 6 dimensions it is, perhaps, unclear what the box (4,5,6,7,8,9) represents; but we can analyze
properties of the box such as the sum of its dimensions.

In this problem you will analyze a property of a group of n-dimensional boxes. You are to determine the longest nesting string of boxes, that is a sequence of boxes  such
that each box  nests in box (  .

A box D = (  ) nests in a box E = (  )
if there is some rearrangement of the  such that when rearranged each dimension is less than the corresponding dimension in box
E. This loosely corresponds to turning box D to see if it will fit in box E. However, since any rearrangement suffices, box D can be contorted, not just turned (see examples below).

For example, the box D = (2,6) nests in the box E = (7,3) since D can be rearranged as (6,2) so that each dimension is less than the corresponding dimension in E. The box D = (9,5,7,3) does NOT nest in the box E
= (2,10,6,8) since no rearrangement of D results in a box that satisfies the nesting property, but F = (9,5,7,1) does nest in box E since F can be rearranged as (1,9,5,7) which nests in E.

Formally, we define nesting as follows: box D = (  ) nests in box E = (  )
if there is a permutation  of  such
that (  ) ``fits‘‘ in (  )
i.e., if for all  .

The Input

The input consists of a series of box sequences. Each box sequence begins with a line consisting of the the number of boxes k in the sequence followed by the dimensionality of the boxes, n (on
the same line.)

This line is followed by k lines, one line per box with the n measurements of each box on one line separated by one or more spaces. The  line
in the sequence (  ) gives the measurements for the  box.

There may be several box sequences in the input file. Your program should process all of them and determine, for each sequence, which of the k boxes determine the longest nesting string and the length of
that nesting string (the number of boxes in the string).

In this problem the maximum dimensionality is 10 and the minimum dimensionality is 1. The maximum number of boxes in a sequence is 30.

The Output

For each box sequence in the input file, output the length of the longest nesting string on one line followed on the next line by a list of the boxes that comprise this string in order. The ``smallest‘‘ or ``innermost‘‘
box of the nesting string should be listed first, the next box (if there is one) should be listed second, etc.

The boxes should be numbered according to the order in which they appeared in the input file (first box is box 1, etc.).

If there is more than one longest nesting string then any one of them can be output.

Sample Input

5 2
3 7
8 10
5 2
9 11
21 18
8 6
5 2 20 1 30 10
23 15 7 9 11 3
40 50 34 24 14 4
9 10 11 12 13 14
31 4 18 8 27 17
44 32 13 19 41 19
1 2 3 4 5 6
80 37 47 18 21 9

Sample Output

5
3 1 2 4 5
4
7 2 5 6

这道题有两种解决办法。第一种可以看做是最长增子串问题的变形,第二种情况这是DAG上的最短路径。我采用的是第一种方法。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
#define MAXSIZE 1000

int k,n;   //k表示有多少组
//int a[MAXSIZE][MAXSIZE];
vector<vector<int> > vec;
vector<vector<int> > old_vec;  //用来记录没排序的串

int d[MAXSIZE];  //用来描述状态的长度
int result[MAXSIZE][MAXSIZE];   //用来描述结果包含哪几项

int match[MAXSIZE];  //用来记录新串在旧串中的位子

bool compare(vector<int> vec1,vector<int> vec2)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(vec1[i]==vec2[i])
            continue;
        return vec1[i]<vec2[i];
    }
    return  false;

}

bool IFTRUE(vector<int> vec1,vector<int> vec2)  //用来判断vec1嵌套在vec2中
{
    int i;
    for(i=0;i<n;i++)
        if(vec1[i]>=vec2[i])
            return false;
    return true;
}
bool operator==(vector<int> vec1,vector<int> vec2)
{
    unsigned int _size=vec1.size();
    for(unsigned int i=0;i<_size;i++)
    {
        if(vec1[i]!=vec2[i])
            return false;
    }
    return true;
}

int main()
{
    //freopen("a.txt","r",stdin);
    //fstream fcout("b.txt");
    int i,j,temp,max_size,flag,m;
    while(cin>>k)
    {
        cin>>n;
        for(i=0;i<k;i++)   //a[0]应当去掉,题目要求从1开始计算
        {
            vector<int> it;
            for(j=0;j<n;j++)
            {
                cin>>temp;
                it.push_back(temp);
            }
            sort(it.begin(),it.end());  //将box内元素升序排列
            old_vec.push_back(it);
            vec.push_back(it);
        }
        //将整个box串重新排序,这样有利于转化为最长增子串问题,同时还要记录新串与原串之间的对应关系
        sort(vec.begin(),vec.end(),compare);
        for(i=0;i<k;i++)
            for(j=0;j<k;j++)
                if(vec[i]==old_vec[j])
                {
                     match[i]=j;
                     break;
                }
        //准备工作已做完,现在开始模拟最长增子序列
        d[0]=1;
        result[0][0]=0;
        max_size=1;
        flag=0;
        for(i=1;i<k;i++)
        {
            d[i]=1;
            for(j=0;j<i;j++)
            {
                if(IFTRUE(vec[j],vec[i])&&d[j]+1>d[i])
                {
                    d[i]=d[j]+1;
                    for(m=0;m<d[j];m++)
                        result[i][m]=result[j][m];
                    result[i][m]=i;
                }
            }
            if(d[i]>max_size)
            {
                max_size=d[i];
                flag=i;
            }
        }
        cout<<max_size<<endl;
        for(i=0;i<max_size;i++)
        {
            if(i!=max_size-1)
                cout<<match[result[flag][i]]+1<<"  ";
            else
                cout<<match[result[flag][i]]+1<<endl;

        }
        vec.clear();
        old_vec.clear(); //必须删除掉,不然会影响下一组数据
    }
    return 0;
}

 Stacking Boxes 

Background

Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions
and analyzing the topology of an n-dimensional hypercube. The former is much more complicated than its one dimensional relative while the latter bears a remarkable resemblance to its ``lower-class‘‘ cousin.

The Problem

Consider an n-dimensional ``box‘‘ given by its dimensions. In two dimensions the box (2,3) might represent a box with length 2 units and width 3 units. In three dimensions the box (4,8,9) can represent
a box (length, width, and height). In 6 dimensions it is, perhaps, unclear what the box (4,5,6,7,8,9) represents; but we can analyze
properties of the box such as the sum of its dimensions.

In this problem you will analyze a property of a group of n-dimensional boxes. You are to determine the longest nesting string of boxes, that is a sequence of boxes  such
that each box  nests in box (  .

A box D = (  ) nests in a box E = (  )
if there is some rearrangement of the  such that when rearranged each dimension is less than the corresponding dimension in box
E. This loosely corresponds to turning box D to see if it will fit in box E. However, since any rearrangement suffices, box D can be contorted, not just turned (see examples below).

For example, the box D = (2,6) nests in the box E = (7,3) since D can be rearranged as (6,2) so that each dimension is less than the corresponding dimension in E. The box D = (9,5,7,3) does NOT nest in the box E
= (2,10,6,8) since no rearrangement of D results in a box that satisfies the nesting property, but F = (9,5,7,1) does nest in box E since F can be rearranged as (1,9,5,7) which nests in E.

Formally, we define nesting as follows: box D = (  ) nests in box E = (  )
if there is a permutation  of  such
that (  ) ``fits‘‘ in (  )
i.e., if for all  .

The Input

The input consists of a series of box sequences. Each box sequence begins with a line consisting of the the number of boxes k in the sequence followed by the dimensionality of the boxes, n (on
the same line.)

This line is followed by k lines, one line per box with the n measurements of each box on one line separated by one or more spaces. The  line
in the sequence (  ) gives the measurements for the  box.

There may be several box sequences in the input file. Your program should process all of them and determine, for each sequence, which of the k boxes determine the longest nesting string and the length of
that nesting string (the number of boxes in the string).

In this problem the maximum dimensionality is 10 and the minimum dimensionality is 1. The maximum number of boxes in a sequence is 30.

The Output

For each box sequence in the input file, output the length of the longest nesting string on one line followed on the next line by a list of the boxes that comprise this string in order. The ``smallest‘‘ or ``innermost‘‘
box of the nesting string should be listed first, the next box (if there is one) should be listed second, etc.

The boxes should be numbered according to the order in which they appeared in the input file (first box is box 1, etc.).

If there is more than one longest nesting string then any one of them can be output.

Sample Input

5 2
3 7
8 10
5 2
9 11
21 18
8 6
5 2 20 1 30 10
23 15 7 9 11 3
40 50 34 24 14 4
9 10 11 12 13 14
31 4 18 8 27 17
44 32 13 19 41 19
1 2 3 4 5 6
80 37 47 18 21 9

Sample Output

5
3 1 2 4 5
4
7 2 5 6

LeetCode: Anagrams [048],布布扣,bubuko.com

时间: 2025-01-30 04:32:41

LeetCode: Anagrams [048]的相关文章

[leetcode]Anagrams @ Python

原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:anagram的意思是:abc,bac,acb就是anagram.即同一段字符串的字母的不同排序.将这些都找出来.这里使用了哈希表,即Python中的dic

LeetCode ---Anagrams() 详解

Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. For example: Input: ["tea","and","ate","eat","den"] Output:   ["tea",&qu

LeetCode——Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 原题链接:https://oj.leetcode.com/problems/anagrams/ 易位构词游戏的英文词汇是 anagram,这个词来源于有"反向"或"再次"的含义的希腊语字根ana-和有"书写"."

[Leetcode] Anagrams 颠倒字母构成词

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意:anagrams的意思是回文构词法.回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序.如: Input: ["tea","and","ate","eat",&qu

[leetcode] Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路: 什么是anagrams?傻逼了,百度吧.一百度全是别人的方法,看到了有map存在,正是这个提示导致此题并没有用很长的时间. 方法很简单,将vector中的每个string先排序,然后在map中寻找是否已经有排序后的string,如果有则说明找到

[LeetCode] Anagrams 错位词

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. http://www.cnblogs.com/AnnieKim/archive/2013/04/25/3041982.html http://bangbingsyb.blogspot.com/2014/11/leetcode-anagrams.html

LeetCode: Anagrams 题解

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 判断字符串是否为回文构词法生成的.找出所有由同一回文构词法生成的字符串对. 使用map用于散列. 将strs中的字符串strs[i],在串内进行字典排序,生成key,原始s[i]不变. 将该字符串s[i]映射到key所对应位置.map[key].push_bac

Leetcode:Anagrams 回文构词法

戳我去解题 Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Anagram(回文构词法)是指打乱字母顺序从而得到新的单词 回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序.因此,将几个单词按照字母顺序排序后,若它们相等,则它们属于同一组anagrams class Solution {

LeetCode Anagrams My solution

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 我的解题思路是这种:1.事实上所谓的anagrams就是字母同样就ok了 2.把每一个数组里面的数字以字典序进行又一次排序形成fingerprint 3.推断排序过得串是否在hashmap里面,假设在里面就加一,说明这个fingerprint 有了 有