zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    部署LVS-DR(直接路由)群集
    快速了解LVS负载均衡群集及部署LVS-NAT(地址转换)群集
    MySQL一些常用的高级SQL语句
    MySQL高可用群集MHA部署及故障测试
    MySQL的主从复制与读写分离
    你知道数据库中数据丢失的严重性吗,快来学习数据库的备份与恢复!!
    快速掌握mysql索引、事务与存储引擎知识以及使用命令
    MySQL数据库常用的一些管理操作
    带大家简单了解MySQL数据库
    剑指offer63-数据流中的中位数**
  • 原文地址:https://www.cnblogs.com/PrayG/p/5657963.html
Copyright © 2011-2022 走看看