poj 1035 串 枚举 暴力 tmp.insert(i, 1, current[i]); //谁能告诉我这句什么意思?

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int ok;
int ldic, lstr;
string current;
vector<string> dic;
 
/* 替换一个字符 */
void replace(string dic)
{
    int count = 0;
    /* 只有一个字符不一样就OK */
    for (int i = 0; i < dic.length(); i++)
    {
        if (current[i] != dic[i]) count++;
        if (count > 1) return;
    }
    ok = 1;
    cout << " " + dic;
}
 
/* 增加一个字符 */
void insert(string dic)
{
    string tmp;
    for (int i = 0; i < current.length(); i++)
    {
        /* 在不同位置增加字符 */
        if (current[i] != dic[i])
        {
            tmp = dic;
            tmp.insert(i, 1, current[i]);  //谁能告诉我这句什么意思?
            if (tmp == current)
            {
                ok = 1;
                cout << " " + dic;
            }
            return;
        }
    }
}
 
/* 删除一个字符 */
void del(string dic)
{
    string tmp;
    for (int i = 0; i < dic.length(); i++)
    {
        /* 删除不同位置的字符 */
        if (current[i] != dic[i])
        {
            tmp = dic;
            tmp.erase(i, 1);
            if (tmp == current)
            {
                ok = 1;
                cout << " " + dic;
            }
            return;
        }
    }
}
 
int main()
{
    /* 读数据 */
    while(1)
    {
        cin >> current;
        if (current == "#") break;
        dic.push_back(current);
    }
 
    while(1)
    {
        cin >> current;
        if (current == "#") break;
 
        ok = -1;
        lstr = current.length();
 
        /* 判断是否correct */
        for (int i = 0; i < dic.size(); i++)
        {
            if (dic[i] == current)
            {
                cout << current + " is correct";
                ok = 0;
                break;
            }
        }
 
        /* 处理 */
        if (ok < 0)
        {
            cout << current + ':';
            for (int i = 0; i < dic.size(); i++)
            {
                ldic = dic[i].length();
                if (lstr == ldic)
                {
                    replace(dic[i]);
                }
                else if (lstr == ldic + 1)
                {
                    insert(dic[i]);
                }
                else if (lstr == ldic - 1)
                {
                    del(dic[i]);
                }
            }
        }
 
        cout << endl;
    }
    system("pause");
    return 0;
}

你可能感兴趣的:(String,vector,System,insert,include)