zoukankan      html  css  js  c++  java
  • Codeforce

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is a lamp in a block, it will light it’s block and the direct adjacent blocks. For example, if there is a lamp at block 3, it will light the blocks 2, 3, and 4.

     Given the state of the street, determine the minimum number of lamps to be installed such that each block is lit.

     Input

     The first line of input contains an integer T (1 ≤ T ≤   1025) that represents the number of test cases.

     The first line of each test case contains one integer N (1 ≤ N ≤ 100) that represents the number of blocks in the street.

     The next line contains N characters, each is either a dot ’.’ or an asterisk   ’*’.

     A dot represents an empty block, while an asterisk represents a block with a lamp installed in it.

     Output

     For each test case, print a single line with the minimum number of lamps that have to be installed so that all blocks are lit.

    Sample Input

    Sample Output

    3

    2

    6

    0

    ......

    1

    3

     

    *.*

     

    8

     

    .*.....*

     

    是的,这是在codeforce上面的一道题,当时写的时候思路和官方给的思路一模一样,可是还是有一些细节没有注意到,比如当字符串是str + 1输入时是可以取到长度len的,for循环中忘记

    加上等号了,最近的刷题也不是很顺利,不知道究竟是怎么搞的,可能是写的时候太快的,虽然每道题都写了一遍,可惜都没几个对的,全是都死在了细节上面,以后呀以此为戒,切记!

    对了,顺便提醒自己一下,在遇到这种需要改动题目给定条件的时候一定要复制一个改变用的数组来标记,不然可能会自己都不知道错在哪里,希望以后不再犯。

     

    2016-07-26 切记

     

    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    const int MX = 150;
    int sign[MX];
    int n;
    char str[MX];
    
    int main() {
        //freopen("input.txt", "r", stdin);
        int cas;
        while (scanf("%d", &cas) != EOF) {
            while (cas--) {
                scanf("%d %s", &n, str + 1);
                memset(sign, 0, sizeof(sign));
                for (int i = 1; i <= n; i++) {
                    if (str[i] == '*') {
                        sign[i] = sign[i - 1] = sign[i + 1] = 1;
                    }
                }
                int ans = 0;
                for (int i = 1; i <= n - 2; i++) {
                    if (sign[i] == 0 && sign[i + 1] == 0 && sign[i + 2] == 0) {
                        sign[i] = sign[i + 1] = sign[i + 2] = 1;
                        ans++;
                    }
                }
                for (int i = 1; i <= n - 1; i++) {
                    if (sign[i] == 0 && sign[i + 1] == 0) {
                        sign[i] = sign[i + 1] = 1;
                        ans++;
                    }
                }
                for (int i = 1; i <= n; i++) {
                    if (sign[i] == 0) {
                        sign[i] = 1;
                        ans++;
                    }
                }
                printf("%d
    ", ans);//直接的暴击解决,看到有人喜欢用动态规划,我其实并不想那么多,就用自己的第一感觉做题是最好的
            }
        }
        return 0;
    }
  • 相关阅读:
    POJ 3126 Prime Path
    POJ 2429 GCD & LCM Inverse
    POJ 2395 Out of Hay
    【Codeforces 105D】 Bag of mice
    【POJ 3071】 Football
    【POJ 2096】 Collecting Bugs
    【CQOI 2009】 余数之和
    【Codeforces 258E】 Devu and Flowers
    【SDOI 2010】 古代猪文
    【BZOJ 2982】 combination
  • 原文地址:https://www.cnblogs.com/steamedbun/p/5708757.html
Copyright © 2011-2022 走看看