问一个算法

haonanernet 2015-08-07 02:02:25
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("a", "b");
data.Add("a", "c");
data.Add("a", "d");
data.Add("d", "f");
data.Add("d", "b");
data.Add("f", "c");
data.Add("f", "a");
data.Add("b", "a");
data.Add("b", "c");
data.Add("c", "v");
data.Add("c", "b");
data.Add("v", "s");
data.Add("h", "j");


求一个函数 List<string> Get(string str)

如果传人字符 a
得到a所有相关的字符集合,该集合不包含自身,不需要重复的字符。
data.Add("a", "b");---》这是与a相关,b需要添加进入集合
data.Add("b", "c");---》这是与a相关,因为通过b传递了一下,可以无限传递的,不要重复的字符,c需要添加进入集合。
上面的例子得到的集合应该如下结果(实际数据挺多的):
b,c,d,f,b,v,s



...全文
233 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2015-08-07
  • 打赏
  • 举报
回复
可以这样写
        public class MyDict<TKey, TValue> : Dictionary<TKey, List<TValue>>
{
public void Add(TKey key, TValue value)
{
var x = new List<TValue>();
if (this.TryGetValue(key, out x))
{
x.Add(value);
}
else
{
x = new List<TValue>() { value};
base.Add(key, x);
}
}
}

static List<string> Get(string str)
{
var p = new Dictionary<int,int>();
var res = new List<string>();
var buf = new List<string>();
if (data.ContainsKey(str)) buf.AddRange(data[str]);
for(var i=0; i<buf.Count; i++)
{
if (buf[i] != str && res.IndexOf(buf[i]) < 0)
{
res.Add(buf[i]);
if (data.ContainsKey(buf[i])) buf.AddRange(data[buf[i]]);
}
}
return res;
}

public static MyDict<string, string> data;

static void Main(string[] args)
{
data = new MyDict<string, string>();
data.Add("a", "b");
data.Add("a", "c");
data.Add("a", "d");
data.Add("d", "f");
data.Add("d", "b");
data.Add("f", "c");
data.Add("f", "a");
data.Add("b", "a");
data.Add("b", "c");
data.Add("c", "v");
data.Add("c", "b");
data.Add("v", "s");
data.Add("h", "j");

Console.WriteLine(string.Join(",", Get("a")));

Console.ReadKey();
}


定义一个类 MyDict 继承于 Dictionary,改写了 Add 方法
Get 方法应该可以写在 MyDict 中的,但是我不会。只好写成全局的了
peekding 2015-08-07
  • 打赏
  • 举报
回复
key都重复了,根本运行不起来,兄弟,还是先调试下吧。
Poopaye 2015-08-07
  • 打赏
  • 举报
回复
你得定义成 Dictionary<string, List<string>> 然后自己实现Add方法 反而Get用Dictionary自带的就行了
  • 打赏
  • 举报
回复
static List<string> Test(string str)
{
    List<KeyValuePair<string, string>> sourceList = new List<KeyValuePair<string, string>>()
    {
        new KeyValuePair<string, string>("a", "b"),
    new KeyValuePair<string, string>("a", "c"),
    new KeyValuePair<string, string>("a", "d"),
    new KeyValuePair<string, string>("d", "f"),
    new KeyValuePair<string, string>("d", "b"),
    new KeyValuePair<string, string>("f", "c"),
    new KeyValuePair<string, string>("f", "a"),
    new KeyValuePair<string, string>("b", "a"),
    new KeyValuePair<string, string>("b", "c"),
    new KeyValuePair<string, string>("c", "v"),
    new KeyValuePair<string, string>("c", "b"),
    new KeyValuePair<string, string>("v", "s"),
    new KeyValuePair<string, string>("h", "j")
    };

    List<string> resultList = new List<string>();
    LoopSourceList(str, sourceList, resultList);
    return resultList;
}
static void LoopSourceList(string key, List<KeyValuePair<string, string>> sourceList, List<string> resultList)
{
    var query = sourceList.Where(kv => kv.Key == key).ToList();
    if (query.Count > 0)
    {
        foreach (var kv in query)
        {
            if (!resultList.Contains(kv.Value))
            {
                resultList.Add(kv.Value);
                LoopSourceList(kv.Value, sourceList, resultList);
            }
        }
    }
}
不知道你的例子为啥会有自己……
於黾 2015-08-07
  • 打赏
  • 举报
回复
编程不能凭空想象
Imcx 2015-08-07
  • 打赏
  • 举报
回复
6666 路过学算法
xiaodeerdeer 2015-08-07
  • 打赏
  • 举报
回复
估计你都得从写Dictionary类
  • 打赏
  • 举报
回复
 Dictionary<string, string> data = new Dictionary<string, string>();
              data.Add("a", "b");
              data.Add("a", "c");
你确认你的例子正确?这不错到家了么,运行直接异常的货啊
於黾 2015-08-07
  • 打赏
  • 举报
回复
你这代码能运行的起来? key值都重复了

110,534

社区成员

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

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

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