[英]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.