Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("efghi", 28);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("abcde", 16);
dtsi.Add("cccdd", 13);
string 长度是一样的,int是由大到小排列的.
-------------------------------------------------------------
abcde
bcdef
cdefg
defgh
.... 这些( 后面去掉一个 等于 另一条去掉前面一个) 的一连串..被视为重复.这样的只留他所在int最大的那条.最后结果要这样:
efghi 28 //只留这个最大的
opqrs 26
aaabb 25
cccdd 13
使用hashTable,
如果Int重复,用Int做Key,如果string重复,用string做Key
bcdef 前面去掉一个得 cdef
cdefg 前面去掉一个得 defg 后面去掉一个得cdef
defgh 后面去掉一个得 defg
则这几条是重复的. 只留int最大的.
bcdef的int是18.
cdefg的int是20.
defgh的int是19. 那么只留cdefg .去掉defgh和bcdef字符串里面可能有中文.
所以他们是重复的.到这里能理解吧?
下面处理重复的.把重复集里面 int最大的留下.其他都去掉.
bcdef的int是18.
cdefg的int是20.
defgh的int是19. 那么只留cdefg .去掉defgh和bcdef
字符串里面可能有中文.
Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("efghi", 28);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("abcde", 16);
dtsi.Add("cccdd", 13); string key = "";
string key1 = "";
List<string> list = new List<string>();
foreach (KeyValuePair<string, int> kv in dtsi)
{
foreach (KeyValuePair<string, int> kv1 in dtsi)
{
if (kv.Key == kv1.Key) continue;
key = kv.Key.Substring(0, 4);
key1 = kv1.Key.Substring(1, 4);
if (key == key1)
{
if (!list.Contains(kv.Key))
{
list.Add(kv.Key);
}
if (!list.Contains(kv1.Key))
{
list.Add(kv1.Key);
}
}
}
}
foreach (string k in list)
{
dtsi.Remove(k);
}
foreach (KeyValuePair<string, int> kv in dtsi)
{
MessageBox.Show(kv.Key + "***" + kv.Value);
}
你这个结果不对呀.."efghi", 28 都没了...
bcdef
cdefg
defgh 为什么是重复?
因为是由abcdefgh 这个字符串切成的..是连续的.这一堆只取int最大的一条.
再不明白绝对是理解能力的问题.而不是表达能力的问题.
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("efghi", 28);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("abcde", 16);
dtsi.Add("cccdd", 13);
Grouping(dtsi).ForEach(x =>
Console.WriteLine(x.Key + "\t" + x.Value));
} static bool AsSame(string a, string b)
{
return a.Contains(b.Substring(0, b.Length - 1)) ||
a.Contains(b.Substring(1));
} static void ProcessItem(Dictionary<string, int> dict, KeyValuePair<string, int> item)
{
if (dict.Where(x => AsSame(x.Key, item.Key)).Count() > 0)
if (!dict.ContainsKey(item.Key))
dict.Add(item.Key, item.Value);
} static List<KeyValuePair<string, int>> Grouping(Dictionary<string, int> dict)
{
List<Dictionary<string, int>> list = new List<Dictionary<string, int>>();
Dictionary<string, int> di;
dict.ToList().ForEach(x => { di = new Dictionary<string,int>(); di.Add(x.Key, x.Value); list.Add(di); });
list.ForEach(x => dict.ToList().ForEach(y => ProcessItem(x, y)));
list.ForEach(x => x = x.OrderBy(y => y.Key).ToDictionary(y => y.Key, y => y.Value));
Dictionary<string, int>[] arr = list.ToArray();
for (int j = 0; j < arr.GetLength(0); j++)
{
var items = list.Where(x => x.Count > arr[j].Count).ToArray();
for (int i = 0; i < items.Count(); i++)
{
if (items[i].Except(arr[j]).Count() == items[i].Count - arr[j].Count)
list.Remove(arr[j]);
}
}
var r = list.Select(x => x.OrderByDescending(y => y.Value).First());
return r.Distinct().ToList();
}
}
}efghi 28
opqrs 26
aaabb 25
cccdd 13
Press any key to continue . . .
Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("abcde", 31);
dtsi.Add("efghi", 28);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("cccdd", 13);
把abcde到上面就不对了.
dtsi.Add("abcde", 31);
dtsi.Add("efghi", 31);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("cccdd", 13);
如果int也重复只留最靠上一个可以么.代码我先用着哈..回头有时间再来学习.
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class MyComparer : IEqualityComparer<KeyValuePair<string, int>>
{
public bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
{
return x.Value == y.Value;
} public int GetHashCode(KeyValuePair<string, int> obj)
{
return obj.Value.GetHashCode();
}
} class Program
{
static void Main(string[] args)
{
Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("abcde", 31);
dtsi.Add("efghi", 31);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("cccdd", 13);
Grouping(dtsi).ForEach(x =>
Console.WriteLine(x.Key + "\t" + x.Value));
} static bool AsSame(string a, string b)
{
return a.Contains(b.Substring(0, b.Length - 1)) ||
a.Contains(b.Substring(1));
} static void ProcessItem(Dictionary<string, int> dict, KeyValuePair<string, int> item)
{
if (dict.Where(x => AsSame(x.Key, item.Key)).Count() > 0)
if (!dict.ContainsKey(item.Key))
dict.Add(item.Key, item.Value);
} static List<KeyValuePair<string, int>> Grouping(Dictionary<string, int> dict)
{
List<Dictionary<string, int>> list = new List<Dictionary<string, int>>();
Dictionary<string, int> di;
dict.ToList().ForEach(x => { di = new Dictionary<string, int>(); di.Add(x.Key, x.Value); list.Add(di); });
for (int i = 0; i < dict.Count; i++)
list.ForEach(x => dict.ToList().ForEach(y => ProcessItem(x, y)));
list.ForEach(x => x = x.OrderBy(y => y.Key).ToDictionary(y => y.Key, y => y.Value));
Dictionary<string, int>[] arr = list.ToArray();
for (int j = 0; j < arr.GetLength(0); j++)
{
var items = list.Where(x => x.Count > arr[j].Count).ToArray();
for (int i = 0; i < items.Count(); i++)
{
if (items[i].Except(arr[j]).Count() == items[i].Count - arr[j].Count)
list.Remove(arr[j]);
}
}
var r = list.Select(x => x.OrderByDescending(y => y.Value).First());
return r.Distinct(new MyComparer()).OrderByDescending(x => x.Value).ToList();
}
}
}abcde 31
opqrs 26
aaabb 25
cccdd 13
Press any key to continue . . .
呃..你这样一调 不是重复的字串 int是31的 也没有了..
{
string key = "";
string key1 = "";
List<string> list = new List<string>();
foreach (KeyValuePair<string, int> kv in dtsi)
{
foreach (KeyValuePair<string, int> kv1 in dtsi)
{
if (kv.Key == kv1.Key) continue;
key = kv.Key.Substring(1);
key1 = kv1.Key.Substring(0, kv1.Key.Length - 1);
if (key == key1)
{
if (!list.Contains(kv1.Key) && kv.Value > kv1.Value)
{
list.Add(kv1.Key);
}
else
{
list.Add(kv.Key);
}
}
}
}
foreach (string k in list)
{
dtsi.Remove(k);
}
}
最后我自己用sunny906前辈的代码改了下好像也适合用..
{
Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("efghi", 28);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("abcde", 16);
dtsi.Add("cccdd", 13);
Dictionary<string, int> result = new Dictionary<string, int>();
var ordered = dtsi.OrderByDescending(x => x.Value);
foreach (var kvp in dtsi)
{
string a = kvp.Key.Substring(1);
string b = kvp.Key.Substring(0,kvp.Key.Length-1);
if (result.Any(x => x.Key.StartsWith(a) || x.Key.EndsWith(b)))
result.Add(kvp.Key, -1);
else
result.Add(kvp.Key, kvp.Value);
} dtsi.Clear(); //这段代码只是为了满足“对dtsi本身修改”
foreach (var item in result.Where(x => x.Value > 0))
{
dtsi.Add(item.Key, item.Value);
} foreach (var item in dtsi)
{
Console.WriteLine(item.Key + "/" + item.Value);
}
Console.Read();
}
static void Main(string[] args)
{
Dictionary<string, int> dtsi = new Dictionary<string, int>();
dtsi.Add("efghi", 28);
dtsi.Add("opqrs", 26);
dtsi.Add("aaabb", 25);
dtsi.Add("defgh", 23);
dtsi.Add("cdefg", 22);
dtsi.Add("bcdef", 22);
dtsi.Add("abcde", 16);
dtsi.Add("cccdd", 13);
Dictionary<string, int?> temp = new Dictionary<string, int?>();
var ordered = dtsi.OrderByDescending(x => x.Value);
foreach (var kvp in dtsi)
{
string a = kvp.Key.Substring(1);
string b = kvp.Key.Substring(0,kvp.Key.Length-1);
if (temp.Any(x => x.Key.StartsWith(a) || x.Key.EndsWith(b)))
temp.Add(kvp.Key, null);
else
temp.Add(kvp.Key, kvp.Value);
}
dtsi.Clear(); //这段代码只是为了满足“对dtsi本身修改”
foreach (var item in temp.Where(x => x.Value != null))
{
dtsi.Add(item.Key, item.Value.Value);
}
foreach (var item in dtsi)
{
Console.WriteLine(item.Key + " " + item.Value);
}
Console.Read();
}