博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程珠玑 15.3生成文本
阅读量:4154 次
发布时间:2019-05-25

本文共 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/

你可能感兴趣的文章
Commit our mod to our own repo server
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>
There's Much More than Intel/AMD Inside
查看>>
CentOS7 安装MySQL 5.6.43
查看>>
使用Java 导入/导出 Excel ----Jakarta POI
查看>>
本地tomcat 服务器内存不足
查看>>
IntelliJ IDAE 2018.2 汉化
查看>>
基于S5PV210的uboot移植中遇到的若干问题记录(一)DM9000网卡移植
查看>>
Openwrt源码下载与编译
查看>>
我和ip_conntrack不得不说的一些事
查看>>
Linux 查看端口使用情况
查看>>
文件隐藏
查看>>
两个linux内核rootkit--之二:adore-ng
查看>>