急求c# 顶级算法问题!

hongt7 2009-03-26 04:54:10
现有如下一个数组
eg:
string[] str1={"1:7:4","1:7:8","11:15","11:16","1:7:29","1:7:3:12","1:7:3:88","19","33","11:10","11:4","11:3","11:1","22","10"};

现需要写个方法,使之能使str1数组内中的元素变为 要求数值和顺序相同的数字用:隔开 数值和顺序不同的数字用,隔开
eg:
str1中的 "1:7:4","1:7:8","1:7:29"变为"1:7:4,8,29"
str1中的 "11:15","11:16","11:10","11:4","11:3","11:1"变为"11:15,16,10,4,3,1"
str1中的 "19","33","22","10"变为"19,33,22,10"
即string[] str2={"1:7:4,8,29","11:15,16,10,4,3,1","19,33,22,10"};
怎么写这个方法呢???? 谢谢!
...全文
138 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
coodd 2009-03-27
  • 打赏
  • 举报
回复
居然跟2楼几乎一样,不过2楼我知道Key是可以为""的
coodd 2009-03-27
  • 打赏
  • 举报
回复
偶也写一个玩玩

class Program
{
static void Main(string[] args)
{
string[] strGroup ={ "1:7:4", "1:7:8", "11:15", "11:16", "1:7:29", "1:7:3:12", "1:7:3:88", "19", "33", "11:10", "11:4", "11:3", "11:1", "22", "10" };
Dictionary<string, List<int>> strDic = new Dictionary<string, List<int>>();
string[] KeyAndValue; List<int> lstValue;
for (int i = 0; i < strGroup.Length ; i++)
{
KeyAndValue = split(strGroup[i]);
if (strDic.ContainsKey(KeyAndValue[0]))
{
(strDic[KeyAndValue[0]] as List<int>).Add(Convert.ToInt32(KeyAndValue[1]));
}
else
{
lstValue = new List<int>(); lstValue.Add(Convert.ToInt32(KeyAndValue[1]));
strDic.Add(KeyAndValue[0], lstValue);
}
}

List<string> lstStr = new List<string>();
foreach (string key in strDic.Keys)
{
string Str = key;
lstValue = strDic[key] as List<int>;
for (int i = 0; i < lstValue.Count; i++)
{
Str += lstValue[i] + ",";
}
lstStr.Add(Str.Remove(Str.Length - 1));
}

string[] Result = lstStr.ToArray();
for (int i = 0; i < Result.Length; i++)
{
Console.WriteLine(Result[i]);
}
Console.Read();
}
//用:分隔字符串,取最后一个,前面的组成两个字符串的数组
private static string[] split(string source)
{
string key, value;
int lastSplitCharPosition = source.LastIndexOf(':');
if (lastSplitCharPosition < 1)
{
key = ""; value = source;
}
else
{
key = source.Remove(lastSplitCharPosition + 1);
value = source.Substring(lastSplitCharPosition + 1);
}
return new string[] { key, value };
}
}
hongt7 2009-03-27
  • 打赏
  • 举报
回复
回 Occam 要么全部30,要么全部030,不能同值不同形...
这种情况不会出现
huanglaobo 2009-03-27
  • 打赏
  • 举报
回复
少了一段,我给补全。

string result = string.Empty;
foreach (string item in tmpResult)
if (item != string.Empty)
result += item + ",";
if (result.Length > 0 && result[result.Length - 1] == ',')
return "(" + result.Substring(0, result.Length - 1) + ")";
return result;
}
}
huanglaobo 2009-03-27
  • 打赏
  • 举报
回复

private string GetStr(string[] source)
{
Dictionary<string, List<string>> tmpDic = new Dictionary<string, List<string>>();
foreach (string item in source)
{
string[] tmp = item.Split(':');
if (tmp[0] == string.Empty)
continue;
if (!tmpDic.ContainsKey(tmp[0]))
tmpDic.Add(tmp[0], new List<string>());
if (tmp.Length > 1)
tmpDic[tmp[0]].Add(item.Substring(item.IndexOf(':') + 1));
else
tmpDic[tmp[0]].Add(string.Empty);
}

List<string> tmpResult = new List<string>();
foreach (string item in tmpDic.Keys)
{
string tmpRes = string.Empty;
foreach (string item2 in tmpDic[item])
if (item2 == string.Empty)
tmpRes = item;

if (tmpRes != string.Empty)
tmpResult.Add(tmpRes);

if (tmpDic[item].Count > 1)
tmpResult.Add(item + ":" + GetStr(tmpDic[item].ToArray()));
}


我写一个递归,基本思路跟树结构有关。
无聊就放上来玩玩。等待其他人给我意见。
occam 2009-03-26
  • 打赏
  • 举报
回复
我的意思是要么全部30,要么全部030,不能同值不同形...
hongt7 2009-03-26
  • 打赏
  • 举报
回复
回 Occam 先排序,把相同的字符串并到一起,然后组合到一起就成了
但是要求不能出现030,30,0030这样的情况... 否则要转换成int再比较

肯定会有30的情况 但不会出现030、0030的情况
这个问题其实是我做无限级的问题
eg: 1:7:4 1代表id为1的目录一级选项 7代表id为7的二级选项 4代表id为7的二级选项的选项值的id为4 若有多个数字的组合,最后一个数字代表选项值而就一个数字如:"22"或"10"就代表一个无选项值的选项

回二楼 如果str1中出现类似1:7:4:5,按照你的需求如何处理?
str1数组中出现了1:7:4 如果1:7:4下面还有值的话如1:7:4:5 那么1:7:4就不会存在 因为这个选项下面还有值的话 选中后 就会弹出子选项供选择 取消则不插入数据
occam 2009-03-26
  • 打赏
  • 举报
回复
先排序,把相同的字符串并到一起,然后组合到一起就成了
但是要求不能出现030,30,0030这样的情况... 否则要转换成int再比较

class Program
{
static void Main(string[] args)
{
string[] str1 = { "1:7:4", "1:7:8", "11:15", "11:16", "1:7:29", "1:7:3:12", "1:7:3:88", "19", "33", "11:10", "11:4", "11:3", "11:1", "22", "10" };

// 转换+排序
string[][] strTmp = Array.ConvertAll<string, string[]>(str1, delegate(string str) { return str.Split(':'); });
Array.Sort<string[]>(strTmp, CompStr);

// 组合字符串
List<string> str2 = new List<string>(str1.Length);
int p = 0;
for (int i = 1; i < strTmp.Length; i++)
{
if (strTmp[i].Length != strTmp[i - 1].Length)
{
str2.Add(BuildStr(strTmp, p, i));
p = i;
}
else if (i < strTmp.Length - 1)
{
for (int j = 0; j < strTmp[i].Length - 1; j++)
{
if (strTmp[i][j] != strTmp[p][j])
{
str2.Add(BuildStr(strTmp, p, i));
p = i;
break;
}
}
}
else
{
str2.Add(BuildStr(strTmp, p, i + 1));
}
}
}

static int CompStr(string[] a, string[] b)
{
if (a.Length > b.Length)
return 1;
else if (a.Length < b.Length)
return -1;

for (int i = 0; i < a.Length; i++)
if (a[i].Length > b[i].Length)
return 1;
else if (a[i].Length < b[i].Length)
return -1;
return 0;
}
static string BuildStr(string[][] ints, int p0, int p1)
{
int i, j = ints[p0].Length;

StringBuilder s = new StringBuilder(32);
for (i = 0; i < j; i++)
{
s.Append(ints[p0][i]);
s.Append(':');
}
if (s.Length > 0)
--s.Length;

for (i = p0 + 1; i < p1; i++)
{
s.Append(',');
s.Append(ints[i][j - 1]);
}
return s.ToString();
}
}
terry 2009-03-26
  • 打赏
  • 举报
回复
有个bug,
if (!string.IsNullOrEmpty(strKey))
把(str1中的 "19","33","22","10"变为"19,33,22,10" )一组数给过滤了.


List<int> listEnd;
int intEnd;
foreach (string[] strList in listTemp)
{
string strKey = string.Empty;
intEnd = Convert.ToInt32(strList[strList.Length - 1]);
for (int i = 0; i < strList.Length-1; i++)
{
strKey += strList[i] + DELIMITER;
}
if (strKey.Length == 0) strKey="-1";//key为空,set key = "-1"
if (!string.IsNullOrEmpty(strKey))
{
if (dicReturn.Keys.Contains(strKey))
{
if (dicReturn.TryGetValue(strKey, out listEnd))
{
listEnd.Add(intEnd);
}
}
else
{
listEnd = new List<int>();
listEnd.Add(intEnd);
dicReturn.Add(strKey, listEnd);
}
}
}

List<string> lstReturn = new List<string>();
foreach (KeyValuePair<string, List<int>> pair in dicReturn)
{
string strEnd = string.Empty;
listEnd = pair.Value;
listEnd.Sort();

foreach (int val in listEnd)
{
strEnd += val.ToString() + END_DELIMITER;
}
strEnd = strEnd.Remove(strEnd.Length - 1, 1);

lstReturn.Add((pair.Key == "-1") ? strEnd : pair.Key + strEnd);// 这里改了.
}
iabswfg858 2009-03-26
  • 打赏
  • 举报
回复
呵呵,没时间写。
睡神在睡觉 2009-03-26
  • 打赏
  • 举报
回复
够麻烦....整了一个小时都没完善了算法,等待高手,写了4个方法才实现,但是bug太多,不知道怎么才是合理的,帮顶
terry 2009-03-26
  • 打赏
  • 举报
回复

string[] str1 ={ "1:7:4", "1:7:8", "11:15", "11:16", "1:7:29", "1:7:3:12", "1:7:3:88", "19", "33", "11:10", "11:4", "11:3", "11:1", "22", "10" };
//string[] str2 = { "1:7:4,8,29", "11:15,16,10,4,3,1", "19,33,22,10" };
Dictionary<string, List<int>> dicReturn = new Dictionary<string, List<int>>();
List<string[]> listTemp = new List<string[]>();
foreach (string str in str1)//str1中的每一个字符串转化成string[],并将数组str1 写成泛型list。这里是我比较习惯用泛型,所以这么做。
{
string[] strTemp = str.Split(DELIMITER);
listTemp.Add(strTemp);
}

//下面,将 上面形成的 list 进行分组放入Dictionary.
//将最后一个数字 前面的部分作为key,key为str2的前面一部分。
//当key相同时,最后一数字放在list中 做value.
List<int> listEnd;
int intEnd;
foreach (string[] strList in listTemp)
{
string strKey = string.Empty;
intEnd = Convert.ToInt32(strList[strList.Length - 1]);
for (int i = 0; i < strList.Length-1; i++)
{
strKey += strList[i] + DELIMITER;
}
if (!string.IsNullOrEmpty(strKey))
{
if (dicReturn.Keys.Contains(strKey))
{
if (dicReturn.TryGetValue(strKey, out listEnd))
{
listEnd.Add(intEnd);
}
}
else
{
listEnd = new List<int>();
listEnd.Add(intEnd);
dicReturn.Add(strKey, listEnd);
}
}
}
//将Dictionary中的value部分,即list 先排序,然后处理成字符串,作为str2中字符串的后面一部分。
List<string> lstReturn = new List<string>();
foreach (KeyValuePair<string, List<int>> pair in dicReturn)
{
string strEnd = string.Empty;
listEnd = pair.Value;
listEnd.Sort();

foreach (int val in listEnd)
{
strEnd += val.ToString() + END_DELIMITER;
}
strEnd = strEnd.Remove(strEnd.Length - 1, 1);

lstReturn.Add(pair.Key + strEnd);
}
//ok,将这里就是你想要的。
string[] str2 = lstReturn.ToArray();



下班时都过了。有什么问题大家帮看看,多提意见。特别是代码简化方面。

booker 2009-03-26
  • 打赏
  • 举报
回复
如果str1中出现类似1:7:4:5,按照你的需求如何处理?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧