Codeforces Round #422 B. Crossword solving

来源:互联网 发布:lol遇到一个网络问题 编辑:程序博客网 时间:2024/06/05 09:27

题目网址: Codeforces Round #422 B. Crossword solving

题意分析:

  • 题意: 给字符串s, t; 求替换掉字符串s中最少的字符的次数使得s是t的子串

  • 由于数据量不是很大, 暴力将 字符串s从t的首字符开始匹配, 匹配完一次, 再从t的下一个字符开始匹配. 双重循环, 求出最大匹配的位置, 即可求出最少需要替换次数

代码:

#include <iostream>using namespace std;const int SIZE = 1e3+5;char s[SIZE];char t[SIZE];int main(int argc, char const *argv[]){    int n, m;    while (~scanf("%d %d", &n, &m))    {        scanf("%s", s);        scanf("%s", t);        int pos = 0, suit = 0;        int cnt;        for (int i = 0; i <= m-n; ++i)        {            cnt = 0;            int k = i;            for (int j = 0; j < n; ++j, ++k)            {                if(s[j] == t[k]) ++cnt;            }            if(cnt > suit)            {                pos = i;                suit = cnt;            }        }        printf("%d\n", n-suit);        for (int i = 0, k = pos; i < n; ++i, ++k)        {            if(s[i] != t[k]) printf("%d ", i+1);         }        printf("\n");    }    return 0;}
原创粉丝点击