zoukankan      html  css  js  c++  java
  • 【codeforces 761C】Dasha and Password(动态规划做法)

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:

    There is at least one digit in the string,
    There is at least one lowercase (small) letter of the Latin alphabet in the string,
    There is at least one of three listed symbols in the string: ‘#’, ‘*’, ‘&’.

    Considering that these are programming classes it is not easy to write the password.

    For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. The i-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).

    During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.

    You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.

    Input
    The first line contains two integers n, m (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols.

    Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters ‘#’, ‘*’ or ‘&’.

    You have such input data that you can always get a valid password.

    Output
    Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

    Examples
    input
    3 4
    1**2
    a3*0
    c4**
    output
    1
    input
    5 5

    &#

    *a1c&
    &q2w*

    a3c

    &#&
    output
    3
    Note
    In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.

    In the second test one of possible algorithms will be:

    to move the pointer of the second symbol once to the right.
    to move the pointer of the third symbol twice to the right.

    【题目链接】:http://codeforces.com/contest/761/problem/C

    【题解】

    /*
        change[now][数字、字母、字符]
        表示第now个字符串光标移到数字或字母或字符的最小操作数;
        (只要一直往左移动,或一直往右移动就好两个方向取最小值就好)
        f[i][1]表示前i个字符只弄出了数字的最小步骤数
        f[i][2]表示前i个字符只弄出了小写字母的最小步骤数
        f[i][3]表示前i个字符只弄出了符号的最小步骤数
        f[i][4]表示前i...数字和小写字母
        f[i][5]表示前i...数字和符号
        f[i][6]表示前i...符号和小写字母
        f[i][7]表示前i...数字和符号和小写字母
        f[i][1] = min(f[i-1][1]+change[now][数字])
        f[i][2] = min(f[i-1][2]+change[now][字母])
        f[i][3] = min(f[i-1][3]+change[now][符号]);
        f[i][4] = min(f[i-1][1]+change[now][字母],f[i-1][2]+change[now][数字],f[i-1][4]+change[now][字母或数字]);
        f[i][5] = min(f[i-1][1]+change[now][符号],f[i-1][3]+change[now][数字或符号]);
        f[i][6] = min(f[i-1][3]+change[now][字母],f[i-1][2]+change[now][符号或字母]);
        f[i][7] = min(f[i-1][4]+change[now][符号],f[i-1][5]+change[now][字母],
                      f[i-1][6]+change[now][数字],f[i-1][7]+change[now][字母或数字或字符]);
    */


    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 50+10;
    
    int n,m;
    int f[MAXN][7];
    int a[MAXN][MAXN];
    int change[MAXN][4];
    char s[MAXN];
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        cin >> n >> m;
        for (int i = 1;i <= n;i++)
        {
            scanf("%s",s+1);
            for (int j = 1;j <= m;j++)
            {
                if (s[j]>='0' && s[j] <='9')
                    a[i][j]=1;
                if (s[j]>='a' && s[j] <= 'z')
                    a[i][j]=2;
                if (s[j]=='#' || s[j]=='*' || s[j] == '&')
                    a[i][j]=3;
            }
        }
        memset(change,0x3f3f3f3f,sizeof change);
        memset(f,0x3f3f3f3f,sizeof f);
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
                change[i][a[i][j]] = min(change[i][a[i][j]],j-1);
            for (int j = m;j >= 1;j--)
                change[i][a[i][j]] = min(change[i][a[i][j]],m-j+1);
        }
        for (int i = 1;i <= 3;i++)
            f[1][i] = change[1][i];
        for (int i = 2;i <= n;i++)
        {
            f[i][1] = min(f[i][1],f[i-1][1]+change[i][1]);
            f[i][2] = min(f[i][2],f[i-1][2]+change[i][2]);
            f[i][3] = min(f[i][3],f[i-1][3]+change[i][3]);
            f[i][4] = min(f[i][4],min(f[i-1][1]+change[i][2],f[i-1][2]+change[i][1]));
            f[i][4] = min(f[i][4],f[i-1][4]+change[i][1]);
            f[i][4] = min(f[i][4],f[i-1][4]+change[i][2]);
            f[i][5] = min(f[i][5],min(f[i-1][1]+change[i][3],f[i-1][3]+change[i][1]));
            f[i][5] = min(f[i][5],f[i-1][5]+change[i][1]);
            f[i][5] = min(f[i][5],f[i-1][5]+change[i][3]);
            f[i][6] = min(f[i][6],min(f[i-1][3]+change[i][2],f[i-1][2]+change[i][3]));
            f[i][6] = min(f[i][6],f[i-1][6]+change[i][2]);
            f[i][6] = min(f[i][6],f[i-1][6]+change[i][3]);
            f[i][7] = min(f[i][7],min(f[i-1][4]+change[i][3],min(f[i-1][5]+change[i][2],
                      f[i-1][6]+change[i][1])));
            f[i][7] = min(f[i][7],f[i-1][7]+change[i][1]);
            f[i][7] = min(f[i][7],f[i-1][7]+change[i][2]);
            f[i][7] = min(f[i][7],f[i-1][7]+change[i][3]);
        }
        printf("%d
    ",f[n][7]);
        return 0;
    }
    
  • 相关阅读:
    从Malvar的论文与两通道QMF设计原理到Speex 与 ISAC中的QMF使用
    转:薪酬与GDP
    转:A PitchEnergy Quantizer for Codec2
    韩国JoonHyuk Chang DSP Lab 专家
    Audio Codec : MPEG2 AAC 反量化模块
    VOIP Codec 三剑客之 SILK (1) 介绍
    CELT 视频PPT介绍
    HEAAC专利
    关于变换编码算法的(Blocking artifacts)和(Ringing artifacts)(一)
    转:免费国际长途 热门应用Line发布中文版
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626658.html
Copyright © 2011-2022 走看看