这道题首先看到题目数据范围1e4,所以可以加剪枝暴力求解。

重点是要分析出长度相差1的和长度相等的才可以经过变换的到字典中的串。

在此基础上,查看匹配情况,长串作为被匹配的, 相同的字符ans++。

分情况分析是否能够变换得到。

最后注意输出格式和要求。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

// bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, maxn = 1e4 + 24;
string m, a;
// vector<int> len1, len2;
vector<string> v1, v2;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(cin >> m) {
        if(m == "#") break;
        v1.push_back(m); //len1.push_back(m.size());
    }
    while(cin >> a) {
        if(a == "#") break;
        v2.push_back(a); //len2.push_back(a.size());
    }
    int x = v1.size(), y = v2.size();
    for(int i = 0; i < y; i++) {
        // if(find(v1.begin(), v1.end(), v2[i]) - v1.end() != v1.size() ) cout << v2[i] << " is correct";
        bool flag = 0;
        for(int k = 0; k < x; k++) {
            if(v1[k] == v2[i]) { flag = 1; break;}
        }
        if(flag) { cout << v2[i] << " is correct\n"; continue; }
        else {
            cout << v2[i] << ":";
            int l = v2[i].size();
            for(int j = 0; j < x; j++) {
                int h = v1[j].size();
                int ans = 0;
                if(l == h + 1) {
                    for(int m = 0, d = 0; m < l; m++)
                        if(v1[j][d] == v2[i][m]) { ans++; d++; }
                }
                else if(l + 1 == h) {
                    for(int m = 0, d = 0; m < h; m++)
                        if(v1[j][m] == v2[i][d]) { ans++; d++; }
                }
                else if(l == h) {
                    for(int m = 0, d = 0; m < l; m++, d++)
                        if(v1[j][d] == v2[i][m]) ans++;
                }
                if((l >= h) && (ans == l - 1)) cout << " " << v1[j];
                if((l < h) && (ans == l)) cout << " " << v1[j];
            }
        }
        puts("");
    }

    return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/
View Code
01-14 12:44