• Codeforces 344A Magnets


    Description

    Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.

    Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own.

    Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.

    Input

    The first line of the input contains an integer n (1 ≤ n ≤ 100000) — the number of magnets. Then n lines follow. The i-th line (1 ≤ i ≤ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position.

    Output

    On the single line of the output print the number of groups of magnets.

    Sample Input

    Input
    6
    10
    10
    10
    01
    10
    10
    Output
    3
    Input
    4
    01
    01
    10
    10
    Output
    2

    Hint

    The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.

    The second testcase has two groups, each consisting of two magnets.

    题意:给你一个数n,表示接下来会有n行 01或者 10,0表示磁极的正级,1,表示磁极的负极,目的是让你把这n个01或者10连接成一个字符串,判断这个字符串有多少快组成,也就是说有多少各连续的1,1或者0,0把这个长串分成了几个部分。。。

    分析: 简单模拟,瞎搞。

    code:

     1 /*************************************************************************
     2     > File Name: cf.cpp
     3     > Author: PrayG
     4     > Mail: 
     5     > Created Time: 2016年07月10日 星期日 12时57分34秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<cstdio>
    10 #include<bits/stdc++.h>
    11 #include<string>
    12 using namespace std;
    13 const int maxn = 200005;
    14 string arr;
    15 int main()
    16 {
    17     string str;
    18     int n,ans= 1;
    19     cin >> n;
    20     for(int t = 1; t <= n; t++)
    21     {
    22         str.clear();
    23         cin >> str;
    24         if(t == 1)
    25         {
    26             arr += str;
    27         }
    28         else
    29         {
    30             int len = arr.length();
    31            // printf("len = %d
    ",len);
    32            // cout <<"str = " <<  str << endl;
    33             if(arr[len-1] == str[0])
    34             {
    35                 //cout << "arr  = " << arr << endl;
    36                 ans++;
    37             }
    38             arr += str;
    39         }
    40     }
    41     cout << ans << endl;
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    百度一下,你就知道
    Struts 2.x 与Spring4.x整合出现:No mapping found for dependency [type=java.lang.String, name='actionPackages...
    Struts 2.x Unable to load configuration.
    CentOS 6使用VNC配置远程桌面
    Tomcat7配置数据源(Oracle)
    org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session
    java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=Aict/listPagedAict.action
    vsftpd限制用户不能更改根目录
    CentOS6.3下搭建vsftpd(采用虚拟用户设置)
    Fedora 20忘记root密码
  • 原文地址:https://www.cnblogs.com/PrayG/p/5657963.html
Copyright © 2020-2023  润新知