找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2296|回复: 1

字典序 我的解法

[复制链接]
发表于 2014-2-8 23:35:30 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. string tofind="ab";
  2. char* mystring=NULL;
  3. int c=0;
  4. int i;
  5. int count=0;
  6. #define LEN 13
  7. hash_map<string,int> myhash;
  8. ofstream out1("log1.txt");
  9. time_t begin=time(NULL);

  10. while(c++ < LEN)
  11. {
  12. mystring=new char[c+1];
  13. for(i=0;i<c+1;i++)
  14. mystring[i]='a'+i;
  15. mystring[c]=0;
  16. while(true)
  17. {
  18. myhash[mystring]=++count;
  19. mystring[c-1]++;
  20. if(mystring[c-1] > 'z')
  21. {
  22. bool canfind=false;
  23. for(i=c-2;i>=0;i--)
  24. {
  25. if(mystring[i] < 'z'-c+1+i)
  26. {
  27. canfind=true;
  28. break;
  29. }
  30. }

  31. if(!canfind)
  32. break;
  33. else
  34. {
  35. mystring[i]++;
  36. while(++i<c)
  37. {
  38. mystring[i]=mystring[i-1]+1;
  39. }
  40. }
  41. }
  42. }
  43. delete []mystring;
  44. }

  45. hash_map<string,int>::iterator itor=myhash.find(tofind);
  46. cout<<(*itor).second<<endl<<"用时:"<<time(NULL)-begin;
  47. //LEN=6时 debug 601s release 1s 单词数20W+ LEN=10时 release 16s
复制代码
QQ图片20140208233228.jpg
回复

使用道具 举报

发表于 2014-2-9 00:16:58 | 显示全部楼层
贴代码不带TAB……
回复 赞! 靠!

使用道具 举报

本版积分规则

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-10-18 13:41 , Processed in 0.030851 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表