111,126
社区成员
发帖
与我相关
我的任务
分享
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 };
}
}
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;
}
}
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()));
}
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);// 这里改了.
}
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();