zoukankan      html  css  js  c++  java
  • B. Crossword solving(Round422)

    B. Crossword solving
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?

    Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets strings="ab?b" as a result, it will appear in t="aabrbb" as a substring.

    Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol.

    Input

    The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string tcorrespondingly.

    The second line contains n lowercase English letters — string s.

    The third line contains m lowercase English letters — string t.

    Output

    In the first line print single integer k — the minimal number of symbols that need to be replaced.

    In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.

    Examples
    input
    3 5
    abc
    xaybz
    output
    2
    2 3
    input
    4 10
    abcd
    ebceabazcd
    output
    1
    2

     hint:问你最少换几个字符,可以让s变成t的子串

    大力搞一发

    直接从t的头开始往后匹配判断找最小就好了。。。

     1     #include<iostream>
     2     #include<cstdio>
     3     #include<stdlib.h>
     4     #include<string>
     5     #include<string.h>
     6     #include<vector>
     7     #include<stack>
     8     #include<queue>
     9     #include<map>
    10     #include<cmath>
    11     using namespace std;
    12     #define pi acos(-1.0)
    13     typedef long long ll;
    14 
    15     int key[1050];
    16     int main(){
    17         memset(key, -1, sizeof(key));
    18         int n, m;
    19         cin >> n >> m;
    20         string s, t;
    21         cin >> s >> t;
    22        // cout << s << " " << t << endl;
    23        // int len1 = s.size(), len2 = t.size();
    24         if(n == m){
    25             int cnt = 0;
    26             for(int i = 0; i < n; i++)
    27                 if(s[i] != t[i]){
    28                     key[cnt] = i + 1;
    29                     cnt++;
    30             }
    31             cout << cnt << endl;
    32             for(int i=0; i<cnt; i++){
    33                 cout << key[i] << " ";
    34             }
    35             cout << endl;
    36         }else{
    37             int Min = 1050, cnt = 0;
    38             for(int i=0; i<=m-n; i++){
    39                 cnt = 0;
    40                 for(int j=i; j<i+n; j++){
    41                     if(s[j-i] != t[j]){
    42                         cnt++;
    43                     }
    44                 }
    45                 if(cnt < Min){
    46                     Min = cnt;
    47                     int ind = 0;
    48                     for(int j=i; j<i+n; j++){
    49                         if(s[j-i] != t[j]){
    50                             key[ind++] = j - i + 1;
    51                         }
    52                     }
    53                 }
    54             }
    55             cout << Min << endl;
    56             for(int i=0; i<Min; i++){
    57                 cout << key[i] << " ";
    58             }
    59             cout << endl;
    60         }
    61 
    62         return 0;
    63     }
  • 相关阅读:
    第二章 金字塔内部的结构
    第一章 为什么要用金字塔结构
    考研级《计算机网络》知识梳理——第二期
    考研级《计算机网络》知识梳理——第一期
    leetcode常规算法题复盘(科普短文篇)——为何哈希表的容量一般是质数
    leetcode常规算法题复盘(第十六期)——数据流中的第 K 大元素
    leetcode常规算法题复盘(第十四期)——最后一块石头的重量
    leetcode常规算法题复盘(第十三期)——最大矩形&柱状图中最大的矩形
    leetcode常规算法题复盘(第十二期)——摆动序列&买卖股票的最佳时机含手续费
    leetcode常规算法题复盘(基础篇)——线性表java实现
  • 原文地址:https://www.cnblogs.com/ledoc/p/7127009.html
Copyright © 2011-2022 走看看