codeforces 822 B Crossword solving

来源:互联网 发布:mt7688 linux 编辑:程序博客网 时间:2024/06/06 01:30

题目:http://codeforces.com/contest/822/problem/B
大致题意:
给出两个字符串,问第一个字符串变成第二个字符串的子串最少需要做多少次replace,并输出replace的位置。
解法:
直接暴力求解,中间记录replace的次数和位置,保留最小值和对于的位置
代码:

#include <iostream>using namespace std;const int N = 1005;int main(){    int n,m;    cin >> n >> m;    char s[N],s2[N];    cin >> s >> s2;    int res = n;    int ans[N],temp[N];    for(int i = 0 ; i < n ; i++){        ans[i] = 1;    }    for(int i = 0 ; i < m-n+1 ; i++){        int sum = 0;        for(int j = 0 ; j < n ; j++){            if(s[j]!=s2[i+j]){                temp[j] = 1;                sum++;            }            else{                temp[j] = 0;            }        }        if(sum<res){            for(int j = 0; j < n ;j++){                ans[j] = temp[j];            }            res = sum;        }    }    cout << res << endl;    for(int i = 0; i < n; i++){        if(ans[i]) cout << (i+1) << " ";    }    cout << endl;}
原创粉丝点击