帮助这个在C语言中衣衫array的数组/子字符串程序? [英] Help with this ragged array/substring program in C?

查看:60
本文介绍了帮助这个在C语言中衣衫array的数组/子字符串程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个长度为200的参差不齐的数组(称为名称)。数组中的每个指针都指向一个不超过50个字符且没有空格的字符串。我也有一个通过用户输入inname给出的字符串,长度为50,inname将是存储在名称中的字符串之一。我需要找到一种方法来遍历衣衫agged的数组中的字符串,并找到子字符串最大的字符串与inname重叠,但不包括inname本身,因为它将在文件中。如果没有字符串重叠,那么我们将打印出不推荐。
我一直在想解决这个问题,已经花费了很多小时,对吗? O :)因此,基本上,程序在数组中找到名称与inname重叠的最大子字符串。
会根据需要进行编辑以提供其他信息

解决方案

这不会引导您找到最有效的方法找到重叠(动态编程是一种方法,还有其他疯狂的方法,例如后缀树),但这应该可以帮助您入门:



首先,考虑一下您将如何找到两个字符串的开头对齐的重叠长度。例如,找到这两者之间最长的重叠:

 编程
不语法的

在这种情况下,只有一个 m 重叠-长度为1。 / p>

然后考虑如何将字符串移位并在对齐方式不同时查找重叠。 (实际上不更改字符串:只需更改在它们之间循环比较它们的方式即可。)这两者之间有什么重叠?

 编程
不合语法的

思考如何查看所有可能的对齐方式。如果跟踪找到的最长字符串,则两个特定字符串之间的对齐时间最长。



此后,继续检查所有不同的字符串。跟踪最匹配的那个,然后再次查看所有这些,您就会得到答案。


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

解决方案

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:

programming
ungrammatical

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?

programming
 ungrammatical

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.

这篇关于帮助这个在C语言中衣衫array的数组/子字符串程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆