• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            coreBugZJ

            此 blog 已棄。

            生成全排列的非回溯方法(TopCoder SRM 591 DIV 2)

            問題來自 TopCoder SRM 591 DIV 2 的第二題:

            Problem Statement
               
            Let X and Y be two strings of equal length, consisting of uppercase English letters only. The two strings are called convertible if there is a permutation P of the English alphabet with the following property: if each character c in the string X is replaced by the character P(c), we obtain the string Y. (In other words, X and Y are convertible iff the following holds: whenever two letters of X are equal, the corresponding two letters of Y must be equal, and vice versa.)  For example, consider the string "ABCA". We can choose to replace each 'A' by a 'F', each 'B' by a 'B', and each 'C' by a 'G', obtaining the string "FBGF". Thus the strings "ABCA" and "FBGF" are convertible. The strings "ABCA" and "EFGH" are not convertible, because the two 'A's in the first string must correspond to the same letter in the second string. The strings "ABCA" and "EEEE" are not convertible, because different letters in the first string must correspond to different letters in the second string.  You are given two strings A and B of the same length. These strings only contain English letters from 'A' to 'I', inclusive. (That is, only the first 9 letters of the alphabet are used.)  You want to change A and B into two strings that are convertible. The only allowed change is to choose some indices (possibly none) and to remove the characters at those indices from each of the strings. (I.e., the removed characters must be at the same positions in both strings. For example, it is not allowed to only remove character 0 of A and character 3 of B.) For example, if A="ABAC", B="AHHA" and the chosen indices are 0 and 2, then the resulting strings will be "BC" and "HA". Our goal is to choose as few indices as possible, given that after the erasing we want to obtain two convertible strings. Compute and return the smallest possible number of chosen indices.
            Definition
               
            Class:
            ConvertibleStrings
            Method:
            leastRemovals
            Parameters:
            string, string
            Returns:
            int
            Method signature:
            int leastRemovals(string A, string B)
            (be sure your method is public)
               

            Constraints
            -
            A will contain between 1 and 50 characters, inclusive.
            -
            A and B will be of the same length.
            -
            A will contain characters from 'A' to 'I' only.
            -
            B will contain characters from 'A' to 'I' only.

            我的思路是窮舉A中字母與B中字母的對應關系,看哪種對應需要刪除的字母最少,這一最少值即是答案。
            窮舉對應關系,就是生成全排列。
            我生成全排列的方式是回溯。

            之后看其他人的代碼,發現一個由給定排列求出其下一個排列的函數,于是學習一下,自己實現如下:

            // 生成下一字典序排列
            // 假設a中元素互不相同
            // 若已經是最后一個字典序排列,則返回0,否則返回1
            int next_permutation( int a[], int n ) {
                    
            int i, j;
                    
            for ( i = n-1; (0 < i) && (a[i-1> a[i]); --i ) {
                    }
                    
            if ( 0 >= i ) {
                            
            return 0;
                    }
                    
            for ( j = n-1; j >= i; --j ) {
                            
            if ( a[ j ] > a[ i-1 ] ) {
                                    
            int tmp = a[ i-1 ];
                                    a[ i
            -1 ] = a[ j ];
                                    a[ j ] 
            = tmp;
                                    j 
            = n - 1;
                                    
            while ( i < j ) {
                                            tmp 
            = a[ i ];
                                            a[ i ] 
            = a[ j ];
                                            a[ j ] 
            = tmp;
                                            
            ++i; --j;
                                    }
                                    
            break;
                            }
                    }
                    
            return 1;
            }

            還有人使用的是C++的 <algorithm> 中 next_permutation 函數,功能一樣。


            posted on 2013-09-28 17:03 coreBugZJ 閱讀(907) 評論(0)  編輯 收藏 引用 所屬分類: ACMAlgorithm

            久久精品亚洲精品国产欧美| 色婷婷噜噜久久国产精品12p | 久久国产热这里只有精品| 国产精品成人久久久久久久| 欧美麻豆久久久久久中文| 奇米影视7777久久精品| 久久亚洲综合色一区二区三区| 狠狠色综合久久久久尤物| 人人狠狠综合久久亚洲高清| 97久久精品无码一区二区天美 | 婷婷久久久亚洲欧洲日产国码AV| 国产一区二区精品久久| 午夜精品久久久内射近拍高清| 久久亚洲精品成人av无码网站| 久久青青国产| 91精品国产综合久久四虎久久无码一级| 精品久久久久久久国产潘金莲| 青青青国产成人久久111网站| 一本久久知道综合久久| 久久免费视频6| 国产综合精品久久亚洲| 免费观看成人久久网免费观看| 亚洲AV无一区二区三区久久| 亚洲一区精品伊人久久伊人| 国产成人无码精品久久久久免费 | 久久亚洲AV成人无码国产| 久久综合久久综合亚洲| 久久99久久成人免费播放| 狠狠狠色丁香婷婷综合久久五月| 无码人妻少妇久久中文字幕蜜桃 | 久久AⅤ人妻少妇嫩草影院| 久久综合综合久久97色| 99久久精品国产免看国产一区| 久久人人爽人人爽人人片av高请 | 久久精品桃花综合| 久久久久久久91精品免费观看| 亚洲国产成人久久综合野外| 亚洲精品国产自在久久| 欧美日韩精品久久久免费观看| 久久精品日日躁夜夜躁欧美| 午夜天堂精品久久久久|