【Codeforces Round #422 (Div. 2) B】Crossword solving

【题目链接】:http://codeforces.com/contest/822/problem/B

【题意】

让你用s去匹配t,问你最少需要修改s中的多少个字符;
才能在t中匹配到s;

【题解】

O(n2)的暴力搞就好;

【Number Of WA

1

【反思】

一开始判断的时候脑抽了;
写成只有s[1]==t[1]的时候才枚举;
hack点是:
很多人两重for循环,没有给j层循环加限制;
直接两层循环1..n和1..m

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e3+50;

int n,m;
char s[N],t[N];
int temp[N][N];

int main(){
    //Open();
    Close();
    cin >> n >> m;
    cin >> (s+1);
    cin >> (t+1);
    int mi = 0;
    temp[0][0] = n;
    rep1(i,1,n) temp[0][i] = i;
    rep1(i,1,m)
        if (i+n-1<=m){
            int now = 0;
            rep1(j,i,i+n-1){
                if (t[j]!=s[j-i+1]){
                    now++;
                    temp[i][now] = j-i+1;
                }
            }
            temp[i][0] = now;
            if (now < temp[mi][0]){
                mi = i;
            }
        }
    cout << temp[mi][0] << endl;
    rep1(i,1,temp[mi][0])
        cout << temp[mi][i] <<' ';
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626223.html