標籤:style os io for re c
題意:對於給出的一些單片語成字典,然後對於每一個輸入的單詞檢查,有以下幾種情況:
1.這個單詞在字典裡有
2.這個單詞刪掉任意一個字母能在字典裡有
3.這個單詞插入任意一個字母能在字典裡有
4.這個單詞任意一個字母被替換在字典裡有
這裡可以用string,細心一點,10000個字典單詞,50個查詢單詞,每個單詞長度不超過15,那麼可以對於每個查詢單詞暴力遍曆判斷一次
#include <iostream>#include <string>using namespace std;string dic[10005];//字典string ans[10005];//可能有的答案int sum,ans_sum;//字典單詞數,可能答案數string str;//臨時輸入的字串void Imput(){ sum = 0; while(cin >> str) { if(str == "#") break; dic[sum ++] = str; }}void Print(){ cout << str << ':'; for(int i = 0; i < ans_sum; i ++) cout << ' ' << ans[i]; cout<<endl;}void check(){ while(cin >> str) { if(str == "#") break; ans_sum = 0; bool should_print = true;//是否要輸出,這裡對於 is correct 之後就不用後面的Print函數啦 for(int i = 0; i < sum; i ++) { if(str == dic[i]){ cout << str << " is correct" << endl; should_print = false; break; } else if(dic[i].length() == str.length()){ //長度相等,則可能有一個字母被替換 int flag = 0; for(int j = 0; j < dic[i].length(); j ++) { if(dic[i][j] != str[j]) flag ++; } if(flag == 1) ans[ans_sum ++] = dic[i]; } else if(dic[i].length() - str.length() == 1){ //在str中插入一個字母,比較 int flag = 0; for(int str_l = 0, dic_l = 0; str_l < str.length() && dic_l < dic[i].length(); ) { if(str[str_l] == dic[i][dic_l]) str_l ++, dic_l ++; else { dic_l ++; flag ++; } } if(!flag || flag == 1) ans[ans_sum++] = dic[i]; } else if(str.length() - dic[i].length() == 1){ //刪除一個字母 int flag = 0; for(int str_l = 0, dic_l = 0; str_l < str.length() && dic_l < dic[i].length(); ) { if(str[str_l] == dic[i][dic_l]) str_l ++, dic_l ++; else { str_l ++; flag ++; } } if(!flag || flag == 1) ans[ans_sum++] = dic[i]; } } if(should_print) Print(); }}int main(){ Imput(); check(); return 0;}