字符串-拼写检查(数据结构基础 第4周)
2016-07-06 09:42
435 查看
问题描述
分析
题目不算难,但注意容易时间复杂度,容易超吃。
源码
分析
题目不算难,但注意容易时间复杂度,容易超吃。
源码
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <cstdio> using namespace std; int issimilar(string a, string b) { if (a.length() == b.length()){ int counts=0; for (int i=0; i<b.length(); i++){ if (a.at(i)!=b.at(i)) { counts++; } } if (counts==0){ return 2; } else if(counts==1){ return 1; } } if (a.length()-b.length() == 1) { int j=0; int counts=0; for(int i=0 ; i<b.length(); i++, j++) { if (b.at(i) != a.at(j)) { i--; counts++; if (counts > 1) { return 0; } } } return 1; /* for (int i=0; i<a.length(); i++) { if (b==(a.substr(0, i)+a.substr(i+1))) { //使用此操作会超时 return 1; } } */ } if (b.length()-a.length() == 1) { int j=0; int counts=0; for(int i=0; i<a.length(); i++, j++) { if (a.at(i) != b.at(j)) { i--; counts++; if (counts > 1) { return 0; } } } return 1; /* for (int i=0; i<b.length(); i++) { if (a==(b.substr(0, i)+b.substr(i+1))) { return 1; } } */ } return 0; } int main() { vector<string> v; string str; while(cin>>str && str!="#") v.push_back(str); while(cin>>str && str!="#") { bool sam=false; vector<string> sim; for (vector<string>::iterator it=v.begin(); it!=v.end(); it++) { int t=issimilar(str, *it); if (t==2) { sam = true; break; } else if (t==1) { sim.push_back(*it); } } if (sam == true) { cout << str << " is correct" << endl; } else { cout << str << ":"; for(int i=0; i<sim.size(); i++) cout << " " << sim.at(i); cout << endl; } } return 0; }
相关文章推荐
- 字符串-前缀中的周期(数据结构基础 第4周)
- 数据结构与算法(每天更新)
- linux内核数据结构之kfifo
- java/数据结构/栈/队列/数组/链表/ArrayList/泛型/增强for/静态导入/可变参数
- 数据结构试验—链表实现数据信息录入
- C 实现通用Tween缓动动画(2)Tween数据结构
- sdut 数据结构实验之栈三:后缀式求值
- C语言的数据结构
- vector自定义数据结构数组按照指定元素排序
- 【数据结构与算法】Hash Table
- 字符串-KMP算法实现(数据结构基础 第4周)
- 数据结构与算法_学习笔记(1)
- 《数据结构》复习之图
- 字符串-字符串类的实现(数据结构基础 第4周)
- 字符串-英语数字转换器(数据结构基础 第4周)
- python 2.7 中文教程-5:数据结构
- 求圆圈中剩下的最后一个数字
- 【数据结构】顺序队列
- 团体程序设计天梯赛-练习集 L2-004. 这是二叉搜索树吗?GU
- 一个鼓,左右手轮流敲打。