【全排列+子序列】Color

【题意】

  这个题目就是问,是否存在每个人对应每一种颜色,如果存在则输出字典序最小的。

  否则输出-1

【题解】

  利用next_permutation来构造36种情况。记住最后还需要排序一遍。

  然后用子序列判断是否存在。while写即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 string Ans[40] ;
 4 string T[40] ;
 5 string Name[3] = {"Alice","Bob","Yazid"};
 6 string name[3] = {"alice" ,"bob","yazid"};
 7 string Color[3] = {"red","blue","green"};
 8 string str;
 9 
10 int cnt = 0 ;
11 
12 void Init(){
13     int N_idx[3] = {0,1,2};
14     int C_idx[3] = {0,1,2};
15 
16     do{
17         do{
18             for(int i=0;i<3;i++)
19                 T[cnt] += name[N_idx[i]] + "is" + Color[C_idx[i]] ;
20             for(int i=0;i<3;i++)
21                 Ans[cnt] += Name[N_idx[i]] + " is " + Color[C_idx[i]] + ".";
22             cnt ++ ;
23         }while(next_permutation(C_idx,C_idx+3));
24     }while( next_permutation(N_idx,N_idx+3));
25 }
26 int main()
27 {
28     ios_base :: sync_with_stdio(false);
29     cin.tie(NULL) , cout.tie(NULL) ;
30     Init();
31 
32     sort( T , T + 36 );
33     sort( Ans , Ans + 36 );
34 
35     //cout << T[0].length() << endl;
36 
37 /*
38     for( auto x : Ans ){
39         cout << x << endl;
40     }
41 */
42 
43     int kase ;
44     cin >> kase ;
45     while( kase -- ){
46         cin >> str;
47         int len = str.length() ;
48         int i , j , k ;
49         for(k = 0 ; k < 36 ; k++ ){
50             i = j = 0 ;
51             while( i < 31 && j < len ){
52                 if( T[k][i] == str[j] ) i++ ;
53                 j ++ ;
54             }
55             if( i == 31 ){
56                 break;
57             }
58         }
59         if( k == 36 ){
60             cout << "No solution." << endl;
61         }else{
62             cout << Ans[k] << endl;
63         }
64     }
65     return 0 ;
66 }
67 /*
68 4
69 aliceisredbobisblueyazidisgreen
70 aliceisgreenbobisgreenyazidisgreen
71 aliceisyellowbobisblueyazidisgreen
72 xxyazidxxisxxgreenxxbobisblueaxlxixcxexixsxrxexdx
73 */
View Code
原文地址:https://www.cnblogs.com/Osea/p/11437366.html