POJ1035 串

Sample Input

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#

Sample Output

me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me

这道题首先看到题目数据范围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
"; 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
原文地址:https://www.cnblogs.com/000what/p/11546710.html