簡體   English   中英

C# 算法按數字搜索單詞

[英]C# algorithm searching words by numbers

我必須在 C# 中編寫算法,其中輸入是:

3 2
aj
oj
ck
25
73

和輸出:

aj ck
DOES NOT EXIST

iput 中的第一行是兩個數字,第一個是單詞數。 第二個是我們將要搜索單詞的數字的數量。 每個字符由如下數字表示:

 2 (abc)
 3 (def)
 4 (ghi)
 5 (jkl)
 6 (mno)
 7 (pqrs)
 8 (tuv)
 9 (wxyz)

例如,對於數字 25,有 9 個雙字符詞:aj、ak、al、bj、bk、bl、cj、ck、cl。 對於數字 438,有 27 個三字符字等。

到目前為止,我有這段代碼,但有些東西不能正常工作,我不知道是什么不起作用。

int n;
int k;
string input;
List<string> dict = new List<string>();
List<string> res = new List<string>();
n = int.Parse(Console.ReadLine());
k = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
    input = Console.ReadLine();
    dict.Add(input);
}
dict.Sort();
for (int i = 0; i < k; i++)
{
    input = Console.ReadLine();
    res = new List<string>(dict);
    int inputLength = input.Length;
    for (int j = 0; j < inputLength; ++j)
    {
        switch (input[j])
        {
            case '2':
                res = new List<string>(filter(res, i, 'a', 'b', 'c'));
                break;
            case '3':
                res = new List<string>(filter(res, i, 'd', 'e', 'f'));
                break;
            case '4':
                break;
            case '5':
                res = new List<string>(filter(res, i, 'j', 'k', 'l'));
                break;
            case '6':
                res = new List<string>(filter(res, i, 'm', 'n', 'o'));
                break;
            case '7':
                res = new List<string>(filterWithFour(res, i, 'p', 'q', 'r', 's'));
                break;
            case '8':
                res = new List<string>(filter(res, i, 't', 'u', 'v'));
                break;
            case '9':
                res = new List<string>(filterWithFour(res, i, 'w', 'x', 'y', 'z'));
                break;
        }
    }
    if (res.Any())
        foreach (var item in res.ToList())
            Console.WriteLine(item);
    else
        Console.WriteLine("DOES NOT EXIST");
}

static List<string> filterWithFour(List<string> resGiven, int pos, char a, char b, char c, char d)
{
    List<string> res = new List<string>();
    foreach (var item in resGiven.ToList())
    {
        if (item.Length > pos)
            if (item[pos] == a || item[pos] == b || item[pos] == c || item[pos] == d)
                res.Add(item);
    }
    return res;
}

static List<string> filter(List<string> resGiven, int pos, char a, char b, char c)
{
    List<string> res = new List<string>();
    foreach (var item in resGiven.ToList())
    {
        if (item.Length > pos)
            if (item[pos] == a || item[pos] == b || item[pos] == c)
                res.Add(item);
    }
    return res;
}

感謝您的任何幫助,因為我堅持這個,我無法通過。

為了做你想做的,我會用正則表達式模式替換數字,如下所示:

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq; // so you can use .Where on List
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        int n;
        int k;
        string input;
        List<string> dict = new List<string>();
        n = int.Parse(Console.ReadLine());
        k = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            input = Console.ReadLine();
            dict.Add(input);
        }
        dict.Sort();
        for (int i = 0; i < k; i++)
        {
            input = Console.ReadLine();

            // get the full pattern for the whole number
            string patternToSearch = "";
            foreach(var c in input)
            {
                patternToSearch += GetPattern(c);
            }

            Console.WriteLine(patternToSearch);

            // find the words that matches the pattern
            var filteredDict = dict.Where(w => Regex.Match(w, patternToSearch).Success);

            if(!filteredDict.Any())
                Console.WriteLine("DOES NOT EXIST");
            else
                Console.WriteLine(string.Join(" ", filteredDict));
        }
    }

    // returns the regex pattern for one number
    static string GetPattern(char c)
    {
        switch(c)
        {
            case '2': return "[abc]";
            case '3': return "[def]";
            case '4': return "[ghi]";
            case '5': return "[jkl]";
            case '6': return "[mno]";
            case '7': return "[pqrs]";
            case '8': return "[tuv]";
            case '9': return "[wxyz]";
            default: return "";
        }
    }
}

有輸入:

3
2
aj
oj
ck
25
73

它輸出:

[abc][jkl]
aj ck
[pqrs][def]
DOES NOT EXIST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM