zoukankan      html  css  js  c++  java
  • ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】

    1. Street Lamps

     

    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

    .*.....*

    /*
    这题纯水。 
    */
    
    
    #include"iostream"
    #include"algorithm"
    #include"cstdio"
    #include"cstring"
    #include"cmath"
    #define MX 100 + 5
    using namespace std;
    
    char s[MX];
    bool ss[MX];
    
    int main() {
    	int T,ans,len;
    	scanf("%d",&T);
    	while(T--) {
    		memset(ss,0,sizeof(ss));
    		memset(s,0,sizeof(s));
    		scanf("%d%s",&len,s+1);
    		ans=0;
    		for(int i=1; i<=len; i++) {
    			if(s[i]=='*') {
    				ss[i-1]=ss[i]=ss[i+1]=1; //把已经照亮的地方标记为 1 
    			}
    		}
    		int tot=0;
    		for(int i=1; i<=len; i++) {
    			if(ss[i]) {				//统计每一个没亮的地方的长度 
    				ans+=(tot+2)/3;     //除3 向上取整 
    				tot=0;
    			}
    			else tot++;
    		}
    		ans+=(tot+2)/3;
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    JavaScript严谨模式(Strict Mode)提升开发效率和质量(转载)
    如何调用.Net中的非Public方法
    ViewState机制由浅入深1
    使用SQL向SQL Server2005中插入图片
    ViewState机制由浅入深3
    在服务器端修改HTML控件的属性
    ViewState机制由浅入深2
    IsPostBack深入探讨
    关于架构的小整理,仅限于个人
    关于文件操作的小方法
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5709392.html
Copyright © 2011-2022 走看看