http://poj.org/problem?id=1035

poj的一道字符串的水题,不难,但就是细节问题我也wa了几次

题意就是给你一个字典,再给你一些字符,首先如果字典中有这个字符串,则直接输出,如果没有的话,那就找字符串加一个字符或少一个字符或者换一个字符是否可以在字典中找到相应的字符串

解题思路:我是用string类型的,比较方便看两个字符串是否相等,用char的话,就是strcmp函数也行。

如果找不到相等的,那么久分别在字典中找到与这个字符串的长度相差1的或者相等的。

然后匹配,如果匹配的结果相差一个则输出

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. string str[],str1[];
  10.  
  11. int main()
  12. {
  13. int dic=,need=;
  14. while(cin>>str[dic]){
  15. if(str[dic]=="#") break;
  16. dic++;
  17. }
  18. while(cin>>str1[need]){
  19. if(str1[need]=="#") break;
  20. need++;
  21. }
  22. //qsort(str,dic,sizeof(str[0]),cmp); //没用的,最开始我是以为要对字典排序输出,其实并不用
  23. for(int i=;i<need;i++){
  24. int flog=; //标记,如果找得到相同的字符串,则continue。
  25. for(int j=;j<dic;j++){
  26. if(str1[i]==str[j]) {
  27. cout<<str1[i]<<" is correct"<<endl;
  28. flog=;
  29. break;
  30. }
  31. }
  32. if(flog==) continue;
  33. int len=str1[i].size();
  34. cout<<str1[i]<<":";
  35. for(int j=;j<dic;j++){
  36. int strl=str[j].size();
  37. if(strl==len||strl==len+||strl==len-){ //字符串相差1的或者相等的,就用来匹配是否有可能相差一个字符,这是一种减枝的办法。
  38. int ans=;
  39. if(len>strl){ //吧那个较长的字符作为被匹配的,用短的来匹配长的字符串。
  40. for(int m=,d=;m<len;m++){
  41. if(str1[i][m]==str[j][d]){
  42. ans++;
  43. d++;
  44. }
  45. }
  46. }else if(len<strl){
  47. for(int m=,d=;m<strl;m++){
  48. if(str[j][m]==str1[i][d]){
  49. ans++;
  50. d++;
  51. }
  52. }
  53. }else if(len==strl){
  54. for(int m=,d=;m<strl;m++,d++)
  55. if(str[j][m]==str1[i][d]) ans++;
  56. }
  57. if(len>=strl&&ans==len-) cout<<" "<<str[j];
  58. if(len<strl&&ans==len) cout<<" "<<str[j];
  59. }
  60. }
  61. cout<<endl;
  62. }
  63. return ;
  64. }

poj 1035的更多相关文章

  1. POJ 1035 代码+具体的目光

    Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19319 Accepted: 7060 Descri ...

  2. poj 1035 Spell checker(水题)

    题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...

  3. poj 1035 Spell checker(hash)

    题目链接:http://poj.org/problem?id=1035 思路分析: 1.使用哈希表存储字典 2.对待查找的word在字典中查找,查找成功输出查找成功信息 3.若查找不成功,对word增 ...

  4. POJ 1035 Spell checker 字符串 难度:0

    题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...

  5. POJ 1035 Spell checker(串)

    题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...

  6. poj 1035 Spell checker ( 字符串处理 )

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16675   Accepted: 6087 De ...

  7. poj 1035 Spell checker

    Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   J ...

  8. 【POJ 1035】Spell checker

    题 题意 每个单词,如果字典里存在,输出”该单词 is correct“:如果字典里不存在,但是可以通过删除.添加.替换一个字母得到字典里存在的单词,那就输出“该单词:修正的单词”,并按字典里的顺序输 ...

  9. Spell checker(poj 1035)

    题意:     此题是一个字符串的问题,首先要给出一个字典,里面存储了数个单词.而后,给出一个单词,如果字典中存在,那么就输出correct,如果字典中没有,那么就要判断是不是这个单词有错误,错误有3 ...

随机推荐

  1. mssql注入

    <%@ Page Language="C#" AutoEventWireup="true" %> <%@ Import Namespace=& ...

  2. Effective Objective-C 2.0 — 第三条:多用字面量语法,少用与之等价的方法

    第三条:多用字面量语法,少用与之等价的方法 几个类:NSString  NSNumber  NSArray  NSDictionary 字面量语法是一种语法糖(syntactic sugar) NSS ...

  3. HBase命令(二) -- 表操作

    创建表 hbase shell> create 'mytable','col1','col2' //建表语句 create '表名','列簇名','列簇名','列簇名' hbase shell& ...

  4. RabbitMQ 用户角色详解

    RabbitMQ的用户角色分类:none.management.policymaker.monitoring.administrator RabbitMQ各类角色描述:none不能访问 managem ...

  5. jquery插件写法

    //传统写法 //全局方法 ;(function($){ $.method = function(){ } //or $.obj = { method1:function(){}, method2:f ...

  6. angular-ngSanitize模块-$sanitize服务详解

    本篇主要讲解angular中的$sanitize这个服务.此服务依赖于ngSanitize模块. 要学习这个服务,先要了解另一个指令: ng-bing-html. 顾名思义,ng-bind-html和 ...

  7. rose中设置组合

    1.在工具栏选择 "association" (关联).2.编辑role B 属性 open specital , role B detail ,multiplic(重数) 为1, ...

  8. 记录在xx公司被考核的15天及自己的感想

    在大学有两件事让我很遗憾. 第一:在2013年7月我和自己的前任女朋友分手,这是两年前的事了,我们谈了七个月. 第二:在2015年4月我被xx公司淘汰了,正如我的前任女朋友是我遇到的最好女孩,这家公司 ...

  9. 腾讯web前端笔试题及个人答案

    每道题都有答案,大多数答案亲测正确. 简答题 1.js中“5”+4=? 答案:54 2.js中void(0)=? 答案:undefined 3.js中NaN*4=? 答案:NaN 4.js中null* ...

  10. ACM_1 大数求和

    /*1 *2014.11.18 *大数求和 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <strin ...