请问:MarkovMode 代码哪儿有阿

quanfeng 2005-02-20 04:14:27
马尔科夫模型相关代码
谢谢
...全文
114 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
古风金 2005-02-24
  • 打赏
  • 举报
回复
楼上的厉害
zixiu2008 2005-02-23
  • 打赏
  • 举报
回复
/* Copyright (C) 1999 Lucent Technologies */
/* Excerpted from 'The Practice of Programming' */
/* by Brian W. Kernighan and Rob Pike */

#include <time.h>
#include <iostream>
#include <string>
#include <deque>
#include <map>
#include <vector>

using namespace std;

const int NPREF = 2;
const char NONWORD[] = "\n"; // cannot appear as real line: we remove newlines
const int MAXGEN = 10000; // maximum words generated

typedef deque < string > Prefix;

map < Prefix, vector < string > > statetab; // prefix -> suffixes

void build(Prefix &, istream &);
void generate(int nwords);
void add(Prefix &, const string &);

// markov main: markov-chain random text generation
int main(void)
{
int nwords = MAXGEN;
Prefix prefix; // current input prefix

srand(time(NULL));
for (int i = 0; i < NPREF; i++) {
add(prefix, NONWORD);
} build(prefix, cin);
add(prefix, NONWORD);
generate(nwords);
return 0;
}

// build: read input words, build state table
void build(Prefix &prefix, istream &in)
{
string buf;

while (in >> buf) {
add(prefix, buf);
}
}

// add: add word to suffix deque, update prefix
void add(Prefix &prefix, const string &s)
{
if (prefix.size() == NPREF) {
statetab[prefix].push_back(s);
prefix.pop_front();
}
prefix.push_back(s);
}

// generate: produce output, one word per line
void generate(int nwords)
{
Prefix prefix;
int i;

for (i = 0; i < NPREF; i++) {
add(prefix, NONWORD);
} for (i = 0; i < nwords; i++) {
vector < string > &suf = statetab[prefix];
const string &w = suf[rand() % suf.size()];
if (w == NONWORD) {
break;
} cout << w << "\n";
prefix.pop_front(); // advance
prefix.push_back(w);
}
}
从《程序設計實踐》上拷貝的,不知滿足要求不?

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧