字符串中子序列的长度
我需要实现 lastSeq
函数,它作为参数 string str
和 char chr
并返回最后一个重复chr
序列的长度(该序列可以是任意长度) 例如: lastSeq("abbaabbbbacd",'a')
应返回 1lastSeq("abbaabbbbacd",'b')
应返回 4 lastSeq("abbaabbbbacd",'t')
应该返回 0
是否有 C++ 函数可以解决这个问题?
I need to implement lastSeq
function,which gets as argument string str
and char chr
and returns the length of last sequence of repeated chr
(the sequence can be of any length)
for example:lastSeq("abbaabbbbacd",'a')
should return 1lastSeq("abbaabbbbacd",'b')
should return 4lastSeq("abbaabbbbacd",'t')
should return 0
Is there C++ function which can solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像是家庭作业,所以我只是给你指导,让你自己找到答案。
首先,如果没有计算机为您的样品提供正确的结果,您将如何自己进行操作。从这些手动运行中,您将如何通过简单的步骤进行概括,以便您可以解决所有不同输入的问题。
此时您应该有一个粗略的算法来解决问题。您对 C++ 中字符串的存储以及该类中可用的方法了解多少?可以用它们来解决你的算法的某些步骤吗?
尝试使用这些函数编写一个程序,编译并运行它。你得到预期的结果吗?如果没有,您可以尝试打印中间状态(使用 std::cout << "Some value: " << variable << "\n";)来尝试调试它。
完成所有这些操作后,如果您仍然遇到问题,请使用您的代码更新您的问题,我们将能够为您提供更多定向帮助。
This appear to be homework, so I'm just going to give you direction so that you can find the answer by yourself.
First, how would you do it yourself, without a computer to give the correct result for your samples. From those manual run, how would you generalize then in simple steps so that you can solve the problem for all different inputs.
By this point you should have a rough algorithm to solve the problem. What do you know about storage of string in C++, and the method that are available from that class? Can some one them be used to solve some of the steps of your algorithm?
Try to write a program using those function, to compile it and to run it. Do you get the expected result? If not, can you try to print intermediate state (using
std::cout << "Some value: " << variable << "\n";
) to try to debug it.Once you have done all of that, and if you are still having issues, update your question with your code, and we'll be able to give you more directed help.