他告诉我在这个程序中输入任何单词,我可以看到一个到另一个的转换,但我不知道在哪里插入我想要的单词。我下载了CodeBlocks,因为根据谷歌的说法,它看起来像是最好的C++跑步器,但当我点击run时,它只显示我需要两个字符串。我猜这意味着单词。
谁能看一下这个,告诉我怎么插入2个单词?
该计划:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
#define MIN(X,Y) ((X <= Y) ? X:Y)
#define MIN3(A,B,C) ((A <= MIN(B,C)) ? A : MIN(B,C))
class EDIST {
private:
string _s1, _s2;
int _edit_distance;
enum backtrace_pointer { L, D, U };
// L==left, D==diagonal, U==up.
vector<vector<int> > dp_table;
vector<vector<int> > backtrace;
public:
EDIST(const char *a, const char *b) {
_s1 = a;
_s2 = b;
_edit_distance = 0;
}
void run();
private:
int edit_distance() const { return _edit_distance; }
void dp_edit_distance();
void print_dp_tables();
void init_dp_tables();
void print_solution();
};
void EDIST :: print_dp_tables()
{
cout << "\nPrinting dynamic programming table\n";
cout << "\t_";
for ( int i=0; i < _s2.size(); i++ )
cout << "\t" << _s2[i];
cout << endl;
for ( int i=0; i <= _s1.size(); i++ ) {
if ( i ) cout << _s1[i-1];
else cout << "_";
for ( int j=0; j <= _s2.size(); j++ )
cout << "\t" << dp_table[i][j];
cout << endl;
}
cout << "\nPrinting backtrace table\n";
cout << "\t_";
for ( int i=0; i < _s2.size(); i++ )
cout << "\t" << _s2[i];
cout << endl;
for ( int i=0; i <= _s1.size(); i++ ) {
if ( i ) cout << _s1[i-1];
else cout << "_";
for ( int j=0; j <= _s2.size(); j++ ) {
cout << "\t";
if ( backtrace[i][j] == L ) cout << "L";
else if ( backtrace[i][j] == D ) cout << "D";
else cout << "U";
}
cout << endl;
}
}
void EDIST :: init_dp_tables()
{
for ( int i=0; i <= _s1.size(); i++ ) {
vector <int> v;
vector<int> b;
for ( int j=0; j <= _s2.size(); j++ ) {
int n=0, op=0;;
if ( !i ) { n=j; op=L; }
else if ( !j ) { n=i; op=U; }
else { n=0; op=D; }
v.push_back(n);
b.push_back(op);
}
dp_table.push_back(v);
backtrace.push_back(b);
}
for ( int i=0; i <= _s1.size(); i++ )
dp_table[i][0] = i;
for ( int j=0; j <= _s2.size(); j++ )
dp_table[0][j] = j;
}
void EDIST :: dp_edit_distance()
{
for ( int j=1; j <= _s2.size(); j++ ) {
for ( int i=1; i <= _s1.size(); i++ ) {
int a = dp_table[i-1][j]+1;
int b = dp_table[i-1][j-1];
int c = dp_table[i][j-1]+1;
if ( _s1[i-1] != _s2[j-1] )
b++;
dp_table[i][j] = MIN3(a,b,c);
if ( a == dp_table[i][j] ) backtrace[i][j] = U;
else if ( b == dp_table[i][j] ) backtrace[i][j] = D;
else backtrace[i][j] = L;
}
}
}
void EDIST :: print_solution()
{
vector<string> string_sequence;
string_sequence.push_back(_s2);
int i = _s1.size();
int j = _s2.size();
while ( i || j ) {
string s = string_sequence[string_sequence.size()-1];
bool add_string=true;
int new_i=i, new_j=j;
if ( backtrace[i][j] == L ) {//LEFT :: insert
new_j--;
s.erase(j-1,1);
}
else if ( backtrace[i][j] == U ) {//UP : delete
new_i--;
string sub1 = (j >= 1 ? s.substr(0,j) : "");
string sub2 = (j < s.size() ? s.substr(j) : "");
s = sub1 + _s1[i-1] + sub2;
}
else {//DIAGONAL : replace OR no-operation
new_i--;
new_j--;
if ( i && j && dp_table[i][j] != dp_table[new_i][new_j] )
s.replace(j-1,1,_s1.substr(i-1,1));
else
add_string = false;
}
if ( add_string ) {
string_sequence.push_back(s);
_edit_distance++;
}
i = new_i;
j = new_j;
}
cout << "\nEdit distance : " << edit_distance() << endl;
if ( string_sequence.size() )
cout << "\nPrinting mutations : \n";
for ( int i=string_sequence.size()-1; i >= 0; i-- )
cout << "\t" << string_sequence[i] << endl;
}
void EDIST :: run()
{
cout << "\nFinding edit-distance between strings `";
cout << _s1 << "' and `" << _s2 << "'" << endl;
init_dp_tables();
dp_edit_distance();
print_dp_tables();
print_solution();
}
int main(int argc, char *argv[])
{
if ( argc != 3 ) {
cerr << "Need 2 strings as input!\n" << endl;
exit(1);
}
EDIST e(argv[1], argv[2]);
e.run();
return 0;
}发布于 2013-01-17 12:04:46
这是一个关于CodeBlocks而不是C++的问题,但在谷歌上快速搜索会发现,你需要在“项目”菜单下选择“设置程序参数”选项。
发布于 2013-01-17 12:04:21
当您下载该程序时,您可能会得到一个.exe文件,您需要使用两个命令行参数执行此程序,如下所示:
programname.exe word1 word2如果你的朋友没有给你一个可执行文件,你需要把源代码编译成一个可执行文件。CodeBlocks提供此功能并自动运行编译结果。不幸的是,它没有向结果传递任何参数,这就是为什么程序告诉你它需要两个单词。(CodeBlocks只是在执行programname.exe)
此问题的一种解决方案是配置代码块以提供参数。As danben pointed out您可以使用“项目”菜单中的“设置程序参数”选项将其配置为提供参数。如果您将程序参数设置为word1 word2,那么CodeBlocks将执行您想要的programname.exe word1 word2。
如果你想在别的地方运行这个程序,你需要编译它。幸运的是,每当你在CodeBlocks中点击“运行”时,它实际上会在某个地方编译一个可执行文件,通常是在bin\debug或bin\release下的项目文件夹中,你会发现一个可执行文件。您可以按照上面概述的方式使用此文件。
https://stackoverflow.com/questions/14372026
复制相似问题