我现在正在学习如何检测给定字符串是否是回文并且问题是我不知道如何连续读取超过4096字节的输入。这是一个简短的代码,可以说明我要做的事情。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
void LCS(char *str, char *reverseStr);
int main(void) {
int number = 0, index = 0;
char *str;
char *reverseStr;
string buffer;
char c = 0;
scanf("%d", &number);
getchar();
str = new char[number + 1]();
reverseStr = new char[number + 1]();
cin >> buffer;
strncpy(str, buffer.c_str(), buffer.length() + 1);
for (int i = 0; i < strlen(str);i++) {
reverseStr[i] = str[strlen(str) - 1 - i];
}
LCS(str, reverseStr);
return 0;
}
void LCS(char *str, char *reverseStr) {
int **matrix;
int result = strlen(str);
matrix = new int*[strlen(str) + 1];
for (int i = 0; i < strlen(str) + 1; i++) {
matrix[i] = new int[strlen(str) + 1]();
}
for (int i = 1; i <= strlen(str); i++) {
for (int j = 1; j <= strlen(reverseStr); j++) {
if (str[i - 1] == reverseStr[j - 1])
matrix[i][j] = matrix[i - 1][j - 1] + 1;
else if (matrix[i - 1][j] >= matrix[i][j - 1])
matrix[i][j] = matrix[i - 1][j];
else
matrix[i][j] = matrix[i][j - 1];
}
}
result -= matrix[strlen(str)][strlen(reverseStr)];
cout << result;
}
对于那些不想阅读上述代码的人,我想简要介绍一下。我想读一个整数,它告诉我要跟随的字符串的大小以及我添加getchar()函数的原因是因为我不想得到任何行尾字符。分配两个字符数组后,我想读取超过4096字节的输入。以下是输入示例。
4960
abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
你能告诉我为什么这个程序不起作用吗?
答案 0 :(得分:1)
可能你遇到了一些命令行限制。我也无法传递这么大的字符串。但我能够使用文件作为输入流。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void LCS(const char *sourceStr, char *reverseStr);
int main()
{
ifstream input_stream;
const char* filenam = "input_file.txt";
input_stream.open(filenam);
if( !input_stream.good() )
{
cerr << "couldn't open file: " << filenam << "\n";
return 1;
}
string buffer;
size_t size;
input_stream >> size;
input_stream.get();
std::getline(input_stream, buffer);
if ( size != buffer.size() )
{
cerr << "ambiguous input: size != buffer.size()\n";
return 1;
}
//no need to allocate a new bufer for read only purpose
const char* raw_str = buffer.c_str();
//allocate space for reversed string
char* reverse_str = new char[size];
LCS(raw_str, reverse_str);
delete [] reverse_str;
}
void LCS(const char *sourceStr, char *reverseStr){
//implementation skipped
}
不需要深层复制源字符串(源Str),因为函数LCS不会修改它。 BTW。功能LCS也有错误。你不能在这里使用双方括号,因为矩阵包含多个内存块(循环中使用了“new”)。
答案 1 :(得分:0)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void LCS(char *str, char *reverseStr);
int main(void) {
char *str = NULL;
char *reverseStr = NULL;
// Allocate first string (any size will do)
size_t number = 10;
str = malloc( sizeof(char) * (number + 1 ));
// Make sure it is allocated, or return error code
if( !str ) return -2;
// zero the memory
memset( &str[0], 0, sizeof(char)*(number+1));
// Using getline. It will return the actual buffer size to us.
size_t bufferSize = getline( &str, &number, stdin );
// How many characters were read?
printf( "Read in %lu characters (not including null terminator)\n", bufferSize );
//get rid of the 'enter' at the end. Replace it with a NULL.
str[bufferSize-1] = 0;
//Allocate the reversed buffer once we know the size for sure.
reverseStr = malloc( sizeof(char) * (bufferSize + 1 ));
//Zero the memory
memset( &reverseStr[0], 0, sizeof(char)*(bufferSize+1));
//Copy string in reverse
for (int i = 0; i < strlen(str); i++) {
reverseStr[i] = str[strlen(str) - 1 - i];
}
printf( "\nCompare and check that whitespace is gone at both ends:\n" );
printf( "Original string length: %lu\n", strlen(str));
printf( "Reversed string length: %lu\n", strlen(reverseStr));
printf( "Original string: %s\n", str);
printf( "Reversed string: %s\n", reverseStr);
LCS( str, reverseStr);
// Cleanup
free(str);
free(reverseStr);
return 0;
}
void LCS(char *str, char *reverseStr) {
int **matrix;
int result = strlen(str);
matrix = (int**)malloc( sizeof(int) * (strlen(str)+1));
for (int i = 0; i < strlen(str) + 1; i++) {
matrix[i] = (int*)malloc( sizeof(int) * (strlen(str)+1));
}
for (int i = 1; i <= strlen(str); i++) {
for (int j = 1; j <= strlen(reverseStr); j++) {
if (str[i - 1] == reverseStr[j - 1])
matrix[i][j] = matrix[i - 1][j - 1] + 1;
else if (matrix[i - 1][j] >= matrix[i][j - 1])
matrix[i][j] = matrix[i - 1][j];
else
matrix[i][j] = matrix[i][j - 1];
}
}
result -= matrix[strlen(str)][strlen(reverseStr)];
printf( "Result = %d\n", result );
}
输出:
~/myprograms john >>./test
radar
Read in 6 characters (not including null terminator)
Compare and check that whitespace is gone at both ends:
Original string length: 5
Reversed string length: 5
Original string: radar
Reversed string: radar
Result = -18178288