Algorithm exercise--DP C # Implementation of the full combinatorial algorithm

Source: Internet
Author: User

Const string splitor = "----"; void Main () {var r = Dp (new list<string> () {"A", "B", "C", "D", "E", "F", "G"}); Console.WriteLine (R);//print combosition Countvar count = 0;for (var i = 0; i < R.count; i++) {var c = regex.matches (R[i] , Splitor). Count;count + = c + 1;} Console.WriteLine (String. Format ("total: {0}", count));} Dp[0] = "a[0],a[1],... a[n]"//dp[i] = dp[i-1] Cross with dp[0]static list<string> Dp (list<string> arr) {if ( arr = = NULL | | Arr. Count = = 0) {return new list<string> ();} var Dparr = new Dynamic[arr. Count];var t = string. Empty;for (var i = 0;i < arr. Count-1; i++) {T + = Arr[i] + ",";} T + = Arr[arr. count-1];//set Dparr[0] = "a[0],a[1],a[2] ..." dparr[0] = t;dparr[0] = dparr[0]. Replace (",", splitor);//set dparr[1] = "A[0...N" Cross A[0...N] "dparr[1" = Cross (t,t);//set DPARR[I...N] = dparr[0] Cross Dparr[i-1]for (var i = 2;i< dparr.length; i++) {var tmp = Cross (dparr[i-1], T);DP arr[i] = tmp;} Twick and save result into listvar ret = new list<string> (); Ret.ADD (Dparr[0]); for (var i = 1; i < dparr.length; i++) {ret. ADD (String. Join (Splitor. ToString (), dparr[i]));} return ret;} for stra = ["A, B", "A,c"] and StrB = "A,b,c,d"//return ["A,b,c", "A,b,d", "A,c,d"]static list<string> Cross (List <string> stra, String StrB) {var ret = new list<string> (); var arr = Strb.split (', '); for (var i =0;i< stra.co Unt i++) {var str = stra[i];for (var j = 0; J < arr. Length; J + +) {var t = str + "," + arr[j];if (!STR). Contains (Arr[j]) &&! Isinclude (ret,t)) {ret. ADD (t);}}} return ret;} For "A-B" and "B,c,d,a"//return ["A, B", "A,c", "A,d", "B,c", "B,d"]static list<string> Cross (string A, string b) { var arr1 = A.split (', '); var arr2 = B.split (', '); var r = new list<string> (); for (var i = 0;i < arr1. Length; i++) {for (var j = 0;j < arr2. Length; J + +) {if (Arr1[i]! = Arr2[j]) {var t = Arr1[i] + "," +arr2[j];if (! Isinclude (r,t)) {R.add (t);}}} return r;} Define other methods and classes herestatic bool Isinclude (list<string> lst,string str){for (var i = 0; i < LST.) Count; i++) {bool Containsall = True;if (Lst[i]. Length! = str. Length) {Containsall = false;} for (var j = 0; j< str. Length; J + +) {if (!lst[i]. Contains (Str[j])) {Containsall = false;}} if (Containsall) {return true;}} return false;}

Algorithm exercise--DP C # Implementation of the full combinatorial algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.