英文:
Displaying all prefixes of a word in C++
问题
我正在尝试显示单词的所有后缀,如下所示:
单词:house
打印输出:
h
ho
hou
hous
house
我的做法是:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char cuvant[100];
int i, k;
cin >> cuvant;
for (i = 0; i < strlen(cuvant); i++)
{
for (k = 0; k < i; k++)
{
if (k == 0)
{
cout << cuvant[k] << endl;
}
else
{
for (k = 1; k <= i; k++)
{
if (k == i) cout << endl;
cout << cuvant[k];
}
}
}
}
}
我做错了什么?
英文:
I am trying to do is display all the suffixes of a word as such:
word: house
print:
h
ho
hou
hous
house
What I did is:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char cuvant[100];
int i,k;
cin>>cuvant;
for(i=0;i<strlen(cuvant);i++)
{
for(k=0;k<i;k++)
{
if(k==0)
{
cout<<cuvant[k]<<endl;
}else
{
for(k=1;k<=i;k++){
if(k==i) cout<<endl;
cout<<cuvant[k];
}
}
}
}
}
What am I doing wrong?
答案1
得分: 2
你过于复杂化了。这里有一种更简单的方法:
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string s;
std::cin >> s;
for (std::string::size_type i = 0, size = s.size(); i != size; ++i)
std::cout << std::string_view{s.c_str(), i + 1} << '\n';
}
如果你没有访问C++17编译器,你可以使用这个:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main() {
std::string s;
std::cin >> s;
for (auto const& ch : s) {
std::copy(s.c_str(), (&ch + 1),
std::ostream_iterator<decltype(ch)>(std::cout));
std::cout << '\n';
}
}
英文:
You're over-complicating it. Here's a simpler way:
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string s;
std::cin >> s;
for (std::string::size_type i = 0, size = s.size(); i != size; ++i)
std::cout << std::string_view{s.c_str(), i + 1} << '\n';
}
If you don't have access to a C++17 compiler, you can use this one:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main() {
std::string s;
std::cin >> s;
for (auto const& ch : s) {
std::copy(s.c_str(), (&ch + 1),
std::ostream_iterator<decltype(ch)>(std::cout));
std::cout << '\n';
}
}
答案2
得分: 2
Here's the translated code:
尽管如此,我认为对于你的学习进度来说,使用调试器自己找出问题会更好。以下是你的代码存在的问题:
对于 `i=0`(外部循环的第一次迭代),`for(k=0;k<i;k++)` 不会执行,因为 `k<0` 计算结果为 `false`。
而且,在两个嵌套的 `for` 循环中更改运行变量 (`k`) 大多数情况下也表示有问题。
所以你想做的是:你想要创建每个可能的前缀,因此你想要创建长度为 `1` 到 `n` 的 `n` 个字符串。所以你的外部循环的第一个想法是正确的。但你在内部部分过于复杂了。
对于内部部分,你想要打印从索引 `0` 到 `i` 的所有字符。
```cpp
int main() {
char cuvant[100];
std::cin >> cuvant;
// 循环遍历字符串的长度
for (int i = 0, size = strlen(cuvant); i < size; i++) {
// 打印从 0 到 i 的所有字符 (k<=0)
for (int k = 0; k <= i; k++) {
std::cout << cuvant[k];
}
// 打印换行符
std::cout << std::endl;
}
}
但是,与其重新发明轮子,我建议使用标准库提供的函数:
int main() {
std::string s;
std::cin >> s;
for (std::size_t i = 0, size = s.size(); i < size; i++) {
std::cout << s.substr(0, i + 1) << std::endl;
}
}
<details>
<summary>英文:</summary>
Even so, I think it would be better for your learning progress to use a debugger to finger out the problem yourself. Here the problems with your code:
For the `i=0` (the first iteration of your outer loop) the `for(k=0;k<i;k++)` will not be executed at all, as `k<0` evaluates to `false`.
And having a running variable (`k`) that you change in two `for` loops that are nested, is most of the time also an indication that something is wrong.
So what you want to do: You want to create each possible prefix, so you want to create `n` strings with the length of `1` to `n`. So your first idea with the outer loop is correct. But you overcomplicate the inner part.
For the inner part, you want to print all chars from the index `0` up to `i`.
int main() {
char cuvant[100];
std::cin >> cuvant;
// loop over the length of the string
for (int i = 0, size = strlen(cuvant); i < size; i++) {
// print all chars from 0 upto to i (k<=0)
for (int k = 0; k <= i; k++) {
std::cout << cuvant[k];
}
// print a new line after that
std::cout << std::endl;
}
}
But instead of reinventing the wheel I would use the functions the std provides:
int main() {
std::string s;
std::cin >> s;
for (std::size_t i = 0, size = s.size(); i < size; i++) {
std::cout << s.substr(0, i + 1) << std::endl;
}
}
</details>
# 答案3
**得分**: 0
以下是翻译好的部分:
对于这个非常简单的字符串后缀任务,您可以使用以下代码:
```cpp
void main()
{
std::string s = "house";
std::string s2;
for(char c : s)
{
s2 += c;
cout << s2 << endl;
}
}
对于更复杂的问题,您可能会对后缀树感兴趣。
英文:
For this very simple string suffix task you can just use:
void main()
{
std::string s = "house";
std::string s2;
for(char c : s)
{
s2 += c;
cout << s2 << endl;
}
}
For more complicated problems you may be interested to read about Suffix Tree
答案4
得分: -2
以下是您的代码翻译:
#include <iostream>
using namespace std;
int main()
{
char cuvant[100];
int i,k;
cin >> cuvant;
for(i=0; i<strlen(cuvant); i++)
{
for (k = 0; k <= i; ++k)
{
cout << cuvant[k];
}
cout << endl;
}
}
英文:
Your code is wrong, the following code can fulfill your requirements
#include <iostream>
using namespace std;
int main()
{
char cuvant[100];
int i,k;
cin>>cuvant;
for(i=0;i<strlen(cuvant);i++)
{
for (k = 0; k <= i; ++k)
{
cout<<cuvant[k];
}
cout<<endl;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论