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     }
  • 相关阅读:
    《深度剖析CPython解释器》16. Python函数机制的深度解析(第三部分): 闭包的底层实现以及调用
    《深度剖析CPython解释器》15. Python函数机制的深度解析(第二部分): 函数在底层是如何被调用的
    《深度剖析CPython解释器》14. Python函数机制的深度解析(第一部分): 函数在底层的数据结构、以及它的创建方式
    《深度剖析CPython解释器》13. 剖析Python的流程控制语句(if、for、while),以及异常捕获机制
    QT: QObject::connect()剖析
    C++ 点滴 (二)
    飞书有时黑屏的解决办法
    QT:显示器数量检测
    使用WinRar 制作自定义安装包
    QT-Mac:QT的SetText()在有些Mac OS系统上未显示
  • 原文地址:https://www.cnblogs.com/ledoc/p/7127009.html
Copyright © 2011-2022 走看看