本文共 1853 字,大约阅读时间需要 6 分钟。
/*---------------------------------------- 随机生成文本程序 2011.3.15 by LiWang112358----------------------------------------*/#include#include using namespace std;int k = 2;//二元模型;当前单词的产生依赖于前一个单词char inputchars[5000000];char * word[1000000];int nword = 0;int cmp(char * p,char * q){ int n = k; for (; *p == *q; p ++ ,q ++) if ( 0 == *p && -- n == 0)//只有当后缀数组的前两个单词相同时,默认是相同的 return 0; return *p - *q;}int sortcmp(const void * p,const void * q){ return cmp((char *)p,(char *)q) <= 0;}void myQsort(char * a[],int l,int r){ int i,j; char * s = a[l]; i=l; j=r; while (i<=j) { while (cmp(a[i],s) < 0 && i<=j) i++; while (cmp(a[j],s) > 0 && i<=j) j--; if (i<=j) { swap(a[i],a[j]); i++; j--; } } if (i > 1; while (l <= r) { if (strcmp(a[mid],x) < 0) l = mid + 1; else if (strcmp(a[mid],x) > 0) r = mid - 1; else if (strcmp(a[mid],x) == 0) { while (mid > 0 && strcmp(a[mid],a[mid - 1]) == 0) --mid; return mid; } mid = (l + r) >> 1; } return -1;}char * skip(char * p,int num){ for (int i = 0; i < num ; ++ i) { for (; (*p) > 0; ++ p); p++; } return p;}int main(){ freopen("in.txt","r",stdin); memset(inputchars,0,sizeof(inputchars)); word[0] = inputchars; while (scanf("%s",word[nword]) != EOF) { word[nword + 1] = word[nword] + strlen(word[nword]) + 1; nword ++; } for (int i = 0; i < k; ++ i) word[nword][i] = 0; myQsort(word,0,nword - 1); srand(int(time(0))); char * p = inputchars; for (int i = 0; i < 10000; ++ i) { printf("%s ",p); if ( i > 0 && i % 20 == 0) printf("/n"); int pos = find(0,nword - 1,p,word); int j = 0; for (j = 0; strcmp(p,word[pos + j]) == 0 && pos + j < nword; ++ j); int rd = abs(rand()) % j; p = word[pos + rd]; p = skip(p,1); if (strlen(skip(p,k - 1)) == 0) break; } return 0;}
转自:
转载地址:http://gieti.baihongyu.com/