Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20929
  • 博文数量: 7
  • 博客积分: 260
  • 博客等级: 入伍新兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-01 14:57
文章分类

全部博文(7)

文章存档

2012年(7)

最近访客

分类: C/C++

2012-05-22 17:32:42

Spell checker

Description

You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations:
?deleting of one letter from the word;
?replacing of one letter in the word with an arbitrary letter;
?inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements from the dictionary for every given word.
用比较复杂的办法

点击(此处)折叠或打开

  1. #include"stdio.h"
  2. #include"string.h"
  3. char a[100001][20];
  4. int main()
  5. {
  6. char x[50][20];
  7. char c[20],d[20],e[20];
  8. char t;
  9. int i,n,j,k;
  10. i=0;
  11. gets(a[i]);
  12. while(a[i][0]!='#')
  13. gets(a[++i]);
  14. n=i-1;
  15. i=0;
  16. gets(x[i]);
  17. while(x[i][0]!='#')
  18. gets(x[++i]);
  19. for(k=0;x[k][0]!='#';k++)
  20. {
  21. for(i=0;i<=n;i++)
  22. if(strcmp(x[k],a[i])==0)
  23. break;
  24. if(i<=n)
  25. {
  26. printf("%s is correct\n",x[k]);
  27. continue;
  28. }
  29. printf("%s:",x[k]);
  30. for(i=0;i<=n;i++)
  31. {
  32. strcpy(c,a[i]);
  33. for(j=0;c[j]!='\0';j++)
  34. {
  35. for(t='a';t<='z';t++)
  36. {
  37. strcpy(d,c);
  38. d[j]=t;
  39. if(strcmp(d,x[k])==0)
  40. {
  41. printf(" %s",a[i]);
  42. break;
  43. }
  44. }
  45. if(t<='z')
  46. break;
  47. }
  48. if(c[j]!='\0')
  49. continue;
  50. for(j=0;c[j]!='\0';j++)
  51. {
  52. strcpy(d,c);
  53. strcpy(&d[j],&c[j+1]);
  54. if(strcmp(d,x[k])==0)
  55. {
  56. printf(" %s",a[i]);
  57. break;
  58. }
  59. }
  60. if(c[j]!='\0')
  61. continue;
  62. for(j=0;c[j]!='\0';j++)
  63. {
  64. strcpy(d,&c[j]);
  65. for(t='a';t<='z';t++)
  66. {
  67. strcpy(e,c);
  68. e[j]=t;
  69. strcpy(&e[j+1],d);
  70. if(strcmp(e,x[k])==0)
  71. {
  72. printf(" %s",a[i]);
  73. break;
  74. }
  75. }
  76. if(t<='z')
  77. break;
  78. }
  79. if(c[j]!='\0')
  80. continue;
  81. for(t='a';t<='z';t++)
  82. {
  83. strcpy(e,c);
  84. e[j]=t;
  85. e[j+1]='\0';
  86. if(strcmp(e,x[k])==0)
  87. {
  88. printf(" %s",a[i]);
  89. break;
  90. }
  91. }
  92. }
  93. printf("\n");
  94. }
  95. return 0;
  96. }
5555~
阅读(1111) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~