简体   繁体   中英

The best overloaded method match for 'System.Collections.Generic.Dictionary<int,System.Collections.Generic.Dictionary<string,int>>.Dictionary(int)'

Can someone help me figure out this compilation error

Compilation error (line 10, col 17): The best overloaded method match for 'System.Collections.Generic.Dictionary>.Dictionary(int)' has some invalid arguments Compilation error (line 13, col 5): Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable>>' to 'int' Last Run: 8:16:27 pm Compile: 0s Execute: 0.188s Memory: 0b CPU: 0s

that is pointing to the Enumerable.Range(1,21) part of my code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
    static void Main(String[] args)
    {
        int N = Int32.Parse(Console.ReadLine());
        var counter = new Dictionary<int, Dictionary<string, int>>
            (
                Enumerable.Range(1,21)
                .Select(i => new KeyValuePair<int, Dictionary<string, int>>(i, new Dictionary<string, int>()))
            );
        for(int i = 0; i < N; ++i)
        {
            string[] input = Console.ReadLine().Split(' ');
            switch(input[0])
            {
                case "add":                   
                    for(int j = 1; j < input[1].Length; ++j)
                    {
                        string sub = input[1].Substring(0,j);
                        if(counter[j].ContainsKey(sub))
                            counter[j][sub] += 1;
                        else
                            counter[j][sub] = 1;
                    }
                    break;
                case "find":
                    Console.WriteLine
                    (
                        counter[input[1].Length].ContainsKey(input[1]) 
                        ? counter[input[1].Length][input[1]]
                        : 0
                    );
                    break;
                default:
                    break;
            }
        }
    }
}

I'm trying to initialize the dictionary with the key-value pairs

[1] = new Dictionary<string,int>(), 
[2] = new Dictionary<string,int>(),
.
.
.
[21] = new Dictionary<string,int>()

Also I'm curious whether C# has a better data structure for trying to hold a collection of strings with fast-lookup of substrings (for this problem https://www.hackerrank.com/challenges/contacts ).

Dictionary's parameterized constructor requires first argument of "int" and second of type "IEqualityComparor".

https://msdn.microsoft.com/en-us/library/6918612z(v=vs.110).aspx

None of which are being passed correctly as part of your code.

You can simplify

var counter = new Dictionary<int, Dictionary<string, int>>();
foreach (var i in Enumerable.Range(1,21))
{
    counter.Add(i, new Dictionary<string, int>());
}

Also as mentioned by Saravanan in comments you can use following line to even simpler code.

var counter = Enumerable.Range(1, 21).ToDictionary(t => t, t => new Dictionary<string, int>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM