Algorithm 交换受限矩阵中的字典最小置换

Algorithm 交换受限矩阵中的字典最小置换,algorithm,Algorithm,以下问题是在Facebook求职面试能力倾向测试中提出的: A permutation is a list of K numbers, each between 1 and K (both inclusive), that has no duplicate elements. Permutation X is lexicographically smaller than Permutation Y iff for some i <= K: All of the first i-1 el

以下问题是在Facebook求职面试能力倾向测试中提出的:

A permutation is a list of K numbers, each between 1 and K (both inclusive),
that has no duplicate elements.

Permutation X is lexicographically smaller than Permutation Y iff for some
i <= K:

All of the first i-1 elements of X are equal to first i-1 elements of Y.
ith element of X is smaller than ith element of Y.
You are given a permutation P, you can exchange some of its elements as many 
times as you want in any order. You have to find the lexicographically smallest     
Permutation that you can obtain from P.

K is less than 101.

Input Format:
First line of input contains K being the size of permutation.
Next line contains K single spaced numbers indicating the permutation.
Each of next K lines contains K characters, character j of line i is equal to
'Y' if you can exchange ith and jth element of a permutation, and 
'N' otherwise.

Output Format:
Print K numbers with a space separating each of them indicating the 
permutation.

Sample Input
3
3 1 2
NNY
NNN
YNN

Sample Output
2 1 3
Sample Input 
3
3 2 1
NYN
YNY
NYN

Sample Output

1 2 3 

In the first example you can exchange first element with last element to
obtain 2 1 3 from 3 1 2.
排列是K个数字的列表,每个数字在1和K之间(包括1和K),
没有重复元素的。
在某些情况下,置换X在词典编纂上比置换Y iff小

i在这里,首先需要形成一个新的矩阵,其中如果
a(i,j)
Y
,那么这意味着
jth
位置的元素可以位于
的第i个位置

这项任务可以很容易地通过应用来完成,用1替换所有Y,用无穷大(或非常大的数字)替换所有N。因此,在应用Floyd Warshall后,如果任何元素
a(i,j)
小于
无穷大
,则
jth位置的元素可以放置在
i
位置


现在任务很简单。贪婪地选择元素,即为每个
i
提供一个可以交换位置的元素列表。因此,现在从位置1到末端依次进行,对于每个
i
,找到具有最小值(索引j)的元素,其中
a(i,j)
Y
(即小于无穷大),并交换
i
j

,您可以按位置进行交换


对于最左边的位置,找到可以找到的最小数字,并将其交换到位置1。这可以通过检查从位置1到第一个位置的路径是否存在来实现。依此类推。

我的解决方案对K非常有效
CC1: {0, 1, 2}
Smallest index in CC1 = 0
Smallest index in CC1 = 1
Smallest index in CC1 = 2
vector<int>TMP_IP;
char Adjacency[LIMIT][LIMIT];
vector<vector<int> >ADJ_vector(LIMIT);
int MARKED[LIMIT];
vector<int>connected_COMPONENTS;
vector<int>Positions_vector;
void DepthFirstTraversal(int u)
{
     MARKED[u]=1;
     connected_COMPONENTS.push_back(u);
     for(int j=0;j<ADJ_vector[u].size();++j)
          if(!MARKED[ADJ_vector[u][j]] )
             DepthFirstTraversal(ADJ_vector[u][j]);
}
//Print result
void lexo_smallest(int K)
{
     for(int i=0;i<K;++i)
                  cout<<TMP_IP[i]<<" ";
             cout<<endl;
}
int main()
{
    int K,ip;
    string rows[109];
    scanf("%d",&K);
    for(int i=0;i<K;++i)
    {
            scanf("%d",&ip);
            TMP_IP.push_back(ip);
    }

    for(int i=0;i<K;++i)
               cin>>rows[i];


    for(int i=0;i<K;++i)
         for(int j=0;j<rows[i].size();++j)
             Adjacency[i][j]=rows[i][j];


    for(int i=0;i<K;++i)
    for(int j=0;j<K;++j)
     if(Adjacency[i][j]=='Y')
         ADJ_vector[i].push_back(j);  

            for( int i = 0 ; i <K ; ++i )
            {   
                if( !MARKED[ i ] )
                { 

                    DepthFirstTraversal( i ); 
                    for(int x=0;x<connected_COMPONENTS.size();++x)
                    {
                            Positions_vector.push_back(TMP_IP[connected_COMPONENTS[x]]);
                    }
                    sort(connected_COMPONENTS.begin(),connected_COMPONENTS.end());
                    sort(Positions_vector.begin(),Positions_vector.end());
                    for(int x=0;x<connected_COMPONENTS.size();++x)
                    {
                            TMP_IP[connected_COMPONENTS[x]]=Positions_vector[x];
                    }
                    connected_COMPONENTS.clear();
                    Positions_vector.clear();

                }
            }
            lexo_smallest(K);

    return 0;
}