zoukankan      html  css  js  c++  java
  • [HDOJ5455]Fang Fang

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5455

    先判断一发,然后记下c出现的位置(还要考虑转回到头部的情况),总之有c总会比单个f或者双f更少。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 
     7 using namespace std;
     8 
     9 const int maxn = 100010;
    10 const int INF = (1<<18);
    11 
    12 char str[maxn];
    13 int pos[maxn];
    14 
    15 int main() {
    16     // freopen("in", "r", stdin);
    17     int T;
    18     int kase = 1;
    19     scanf("%d", &T);
    20     while(T--) {
    21         memset(pos, 0, sizeof(pos));
    22         scanf("%s", str);
    23         int len = strlen(str);
    24         int cnt = 0;    //how c
    25         int flag = 0;   //not
    26         for(int i = 0; str[i]; i++) {
    27             if(str[i] != 'c' && str[i] != 'f') {
    28                 flag = 1;
    29                 break;
    30             }
    31             if(str[i] == 'c') {
    32                 cnt++;
    33             }
    34         }
    35         if(flag) {
    36             printf("Case #%d: -1
    ", kase++);
    37             continue;
    38         }
    39         if(cnt == 0) {
    40             printf("Case #%d: %d
    ",kase++, len / 2 + len % 2);
    41             continue;
    42         }
    43         int fir = INF;
    44         int cur = 0;
    45         for(int i = 0; str[i]; i++) {
    46             if(str[i] == 'c') {
    47                 if(fir == INF) {
    48                     fir = i;
    49                 }
    50                 pos[cur++] = i;
    51             }
    52         }
    53         pos[cur++] = fir + len;   //circle
    54         for(int i = 1; i < cur; i++) {
    55             if(pos[i] - pos[i-1] <= 2) {
    56                 flag = 1;
    57                 break;
    58             }
    59         }
    60         if(flag) {
    61             printf("Case #%d: -1
    ", kase++);
    62         }
    63         else {
    64             printf("Case #%d: %d
    ", kase++, cnt);
    65         }
    66     }
    67 }
  • 相关阅读:
    事件的解密
    C#世界中的委托
    这次是C#中的接口
    完全二叉树的建立和翻转
    全排列的应用
    网易笔试-按位或运算
    柱状图的最大矩形--单调栈
    Linux将线程绑定到CPU内核运行
    Windows多线程与线程绑定CPU内核
    B+树介绍
  • 原文地址:https://www.cnblogs.com/kirai/p/4822019.html
Copyright © 2011-2022 走看看