帮忙用 C 语言编写这个不规则的数组/子字符串程序吗?
所以,我有一个长度为 200 的参差不齐的数组(称为名称)。数组中的每个指针都指向一个不超过 50 个字符且没有空格的字符串。我还有一个通过用户输入给出的名为 inname 的字符串,长度为 50,inname 将是存储在名称中的字符串之一。我需要找到一种方法来遍历我的参差不齐的数组中的字符串,并找到与 inname 具有最大子字符串重叠的字符串,不包括 inname 本身,因为它将在文件中。如果没有字符串重叠,那么我们打印出“无推荐”。 我已经花了好几个小时试图解决这个问题,有帮助吗? O:) 所以基本上,程序会在数组中查找与 inname 重叠最大的子字符串的名称。 如果您需要,将进行编辑以提供附加信息
so, I have a ragged array (called names) of length 200. each pointer in the array points to a string that is no longer than 50 characters and has no white spaces. I also have a string given through user-input called inname, with length 50, inname will be one of the strings stored in names. I need to find a way to go through the strings in my ragged array and find the string with the largest substring overlap with inname, excluding inname itself as that will be in the file. If no string has an overlap then we print out "no recommendation".
I've been melting trying to puzzle this out for many hours now, helps? O:) SO basically, the program finds the name in the array with the largest substring overlap with inname.
will edit to provide additional info if you need it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该从确定 infunc 和单个字符串之间重叠大小的较小问题开始。
维基百科介绍了一些解决最长公共子串问题的算法(包括伪代码!)
You should probably start on the smaller problem of determining the size of the overlap between infunc and a single string.
Wikipedia goes over some algorithms for solving the longest common substring problem (including pseudocode!)
这不会引导您找到找到重叠的最有效方法(动态编程是一种方法 - 还有其他疯狂的方法,例如后缀树),但它应该让您开始:
首先,考虑如何找到重叠的长度重叠部分与两个字符串的开头对齐。例如,找到这两者之间最长的重叠:
在这种情况下,只有一个
m
重叠 - 长度为 1。然后考虑如何“移动”字符串并查找重叠当它们以不同方式对齐时。 (实际上不要更改字符串:只需更改循环它们以比较它们的方式。)这两者之间有什么重叠?
思考如何看待所有可能的对齐方式。如果您跟踪找到的最长的字符串,则您将获得两个特定字符串之间的最长对齐。
之后,继续检查所有不同的字符串。跟踪最匹配的一个,再次,一旦您查看了所有这些,您就会得到答案。
This won't lead you to the most efficient way to find overlap (dynamic programming is one way--there are other crazy methods like suffix trees), but it should get you started:
First, think about how you would find the length of the overlap with the beginning of both strings aligned. For instance, find the longest overlap between these two:
In this case, only a single
m
overlaps--a length of 1.Then think about how you would "shift" the strings and look for the overlap when they are aligned differently. (Don't actually change the strings: just change how you loop through them to compare them.) What is the overlap between these two?
Think about how to look at all possible alignments. If you keep track of the longest one you find, you have the longest alignment between two particular strings.
After that, move on to checking all the different strings. Keep track of the one with the best match, and again, once you have looked at all of them, you have your answer.