poj 1035 Spell checker (锻炼自己的编码能力)

本文介绍了如何使用C++中的string类的erase方法进行字符串的比较与特定操作,包括字符串长度相等时的比较、长度相差一的情况下的字符串匹配与替换。通过实际代码示例,深入探讨了编码技巧与实践应用。

利用 string 的 erase 方法的特性。很大的模拟。经典模拟,练习自己的编码能力!

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#define Bug cout << "here\n";
using namespace std;

vector<string> vec;
void work(string a, string b) { // 长度相等 
    // a 是字典里面的 
    int count = 0;
    int len = a.length();
    for(int i = 0; i < len; i++) {
        if(a[i] != b[i]) {
            count++;    
            if(count > 1) {
                return;
            }
        }
    }
    cout << " " << a;
}   
void work1(string a, string b) { // 字典里面的长 1
     int count;
     int len = a.length();
     string temp = a;
     for(int i = 0; i < len; i++) {
        a = temp;
        a.erase(i, 1); // 基本返回自己 
        if(a == b) {
            cout << " " << temp;
            break;
        }
     }
}
void work2(string a, string b) { // 字典里面的短 1
     int count;
     int len = b.length();
     string temp = b;
     for(int i = 0; i < len; i++) {
        b = temp;
        b.erase(i, 1); // 基本返回自己 
        if(a == b) {
            cout << " " << a;
            break;
        }
     }
}
int main() {
    string temp;
    while(1) {
        cin >> temp;
        if(temp == "#") break;
        vec.push_back(temp);
    }
    int vec_size = vec.size();
    while(1) {
        cin >> temp;
        if(temp == "#") break;
        int flag = 0;
        int len_temp = temp.length();
        for(int i = 0; i < vec_size; i++) {
            if(temp == vec[i]) {
                cout << temp << " is correct" << endl;
                flag = 1;
                break;
            }
        }
        if(flag) continue;
        cout << temp << ":";
        for(int i = 0; i < vec_size; i++) {
            int len1 = vec[i].length();
            if(len1 == len_temp) {
                work(vec[i], temp);
            }
            else if(len1 == len_temp+1) {
                work1(vec[i], temp);
            }
            else if(len1+1 == len_temp) { 
                work2(vec[i], temp);
            }
        }
        cout << endl;
    }
    //system("pause");
    return 0;
}
                
            
            
                
                
        
    
    


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值