演算法練習 -- DP C# 實現 全組合演算法

來源:互聯網
上載者: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);dpArr[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.Count; 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;}

演算法練習 -- DP C# 實現 全組合演算法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.