比较字符数组
例如,如果我有一个字符数组:
A = [w, o, r, n, g, , w, o, r, d]
另一个数组例如:
B = [c, o, r, r, e, c, t, , w, o, r, d, .]
我需要将数组 A 中的单词(以空格分隔)与数组 B 进行比较,并且第一个数组中的任何单词是否存在于第二个数组,那么应该打印该单词。例如,由于“word”存在于第一个数组和第二个数组中,因此应该打印出“word”。
我该怎么办?
If I had an array of characters for example:
A = [w, o, r, n, g, , w, o, r, d]
And another array for example:
B = [c, o, r, r, e, c, t, , w, o, r, d, .]
I need to compare the words in array A (which are separated by a blank space) to array B and if any of the words in the first array exist in the second array, then that word should be printed. So for example, since "word" exists in the first array and in the second array, then "word" should be printed out.
How do I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们看看我会怎么做:
您将需要一个函数,给定一个
char
数组,将其拆分为一个单词数组(并将它们放入 C 字符串中,请以 NUL 结尾:-)) 。我会将该数组的长度和该数组放入一个结构中...现在如何执行此功能?
假设我们“作弊”了一点,并决定我们的数组
A
和B
以 NUL 终止(或者如果它们像 B 一样以.
终止,然后将.
替换为 NUL)。现在,这是 C 语言,您应该首先计算字符串中的空格数,分配一个足以包含char*
(WordCollection::Words
) 的数组>n + 1char*
(并将此n + 1
放入WordCollection::NumWords
中)并使用 strtok “标记”字符串并放置您创建的数组中的单词。那么你应该(可以)使用这个函数将 A 和 B 数组分割成单词。您将获得两个
WordCollection
:A1 和 B1。为了使其更快,我会 qsort B1。
然后,对于 A1 中的每个单词,您在 B1 中 bsearch 它(它不是这是一个坏词...它的意思是二分搜索,它是一种在有序数组中搜索某些内容的快速方法)
完成 :-)
如果这是您第一次使用
bsearch
和qsort
,您最好查看周围可以找到的示例。它们的语法可能很“棘手”。现在...我知道你不会看代码 :-) 所以我会把它放在这里
并放在 ideone
Let's see how I would do it:
You will need a function that, given an array of
char
, splits it in an array of words (and put them in C strings, NUL terminated please :-) ). I would put the length of this array and the array in a structNow... How to do this function?
Let's say we "cheat" a little and decide that our arrays
A
andB
are NUL terminated (or if they are.
terminated like B, then you replace the.
with a NUL). Now, this being C, you should first count the number of spaces in the string, allocate an array ofchar*
(WordCollection::Words
) big enough to containn + 1
char*
(and put thisn + 1
inWordCollection::NumWords
) and using strtok "tokenize" the string and put the words in the array you created.Then you should (could) split the A and B array in words using this function. You'll obtain two
WordCollection
, A1 and B1.To make it quicker, I would qsort B1.
Then for each word in A1 you bsearch it in B1 (it isn't a bad word... It means Binary Search, and it's a quick method of searching something in an ordered array)
Done :-)
I'll add that, if this is the first time you use
bsearch
andqsort
, it's better you look at the samples you can find around. Their syntax can be "tricky".Now... I know you won't look at the code :-) so I'll put it here
And on ideone
基本上,您需要以某种方式分离单词,然后迭代组合。有一百种方法可以做到这一点——它只需要编程。
Basically you need to somehow separate the words, and then iterate through the combinations. There are a hundred ways to do this -- it simply requires programming.
您也可以这样做:
将集合 A 中的所有单词插入集合 C 中,并带有后缀“A”。你会得到 =>
worngA
wordA
将集合 B 中的所有单词插入到集合 C 中,并带有后缀“B”。你会得到 =>
rightB
wordB
在集合C上运行排序算法,例如qsort。你会得到 =>
correctB
wordA
wordB
worngA
在集合C中循环,直到它尺寸-1。将
word[i]
与word[i+1]
进行比较 - 如果它们除了最后一个字母之外都匹配 - 您发现重复的内容,您可以将其打印出来。我不知道这个算法的复杂性,但它显然应该比暴力扫描所有单词组合更快:-)
You can also do it like that:
Insert all words from set A into set C with suffix 'A'. You will get =>
worngA
wordA
Insert all words from set B into set C with suffix 'B'. You will get =>
correctB
wordB
Run sorting algo on set C such as qsort. You will get =>
correctB
wordA
wordB
worngA
Loop in set C until it's size-1. Compare
word[i]
withword[i+1]
- if they match except the last letter - you found duplicate and you can print it out.I don't know about this algorithm complexity, but it clearly should be faster than just brute-force scan of all words combinations :-)