有谁知道一种将输入字符串自动换行到指定行数而不是设置宽度的好算法。基本上达到X线的最小宽度。
e.g. "I would like to be wrapped into two lines" goes to "I would like to be wrapped into two lines" "I would like to be wrapped into three lines" goes to "I would like to be wrapped into three lines"
根据需要插入新行。我可以找到其他自动换行问题,但它们都具有已知的宽度,并希望根据需要插入尽可能多的行以适合该宽度。我追求相反。
最好使用.NET语言的答案,但任何语言都将有所帮助。显然,如果有执行此操作的框架方法,我不会通知我。
编辑 我发现了这一点,因此我认为可以接受的答案是解决我的问题的方法,但是很难理解。将文本分成3个均匀大小的组的算法,只要有人可以将其转换为c#或vb.net。
这是算法中公认的解决方案,可将文本分为3个均匀大小的组转换为C#:
static List<string> Minragged(string text, int n = 3) { var words = text.Split(); var cumwordwidth = new List<int>(); cumwordwidth.Add(0); foreach (var word in words) cumwordwidth.Add(cumwordwidth[cumwordwidth.Count - 1] + word.Length); var totalwidth = cumwordwidth[cumwordwidth.Count - 1] + words.Length - 1; var linewidth = (double)(totalwidth - (n - 1)) / n; var cost = new Func<int, int, double>((i, j) => { var actuallinewidth = Math.Max(j - i - 1, 0) + (cumwordwidth[j] - cumwordwidth[i]); return (linewidth - actuallinewidth) * (linewidth - actuallinewidth); }); var best = new List<List<Tuple<double, int>>>(); var tmp = new List<Tuple<double, int>>(); best.Add(tmp); tmp.Add(new Tuple<double, int>(0.0f, -1)); foreach (var word in words) tmp.Add(new Tuple<double, int>(double.MaxValue, -1)); for (int l = 1; l < n + 1; ++l) { tmp = new List<Tuple<double, int>>(); best.Add(tmp); for (int j = 0; j < words.Length + 1; ++j) { var min = new Tuple<double, int>(best[l - 1][0].Item1 + cost(0, j), 0); for (int k = 0; k < j + 1; ++k) { var loc = best[l - 1][k].Item1 + cost(k, j); if (loc < min.Item1 || (loc == min.Item1 && k < min.Item2)) min = new Tuple<double, int>(loc, k); } tmp.Add(min); } } var lines = new List<string>(); var b = words.Length; for (int l = n; l > 0; --l) { var a = best[l][b].Item2; lines.Add(string.Join(" ", words, a, b - a)); b = a; } lines.Reverse(); return lines; }