zoukankan      html  css  js  c++  java
  • Pursuing the Happiness

    http://codeforces.com/gym/101341/problem/B

    ProblemB. Pursuing the Happiness
    Input file: standard input
    Output file: standard output
    Time limit: 2 seconds
    Memory limit: 256 megabytes


     Mike wants to find a substring «happiness» in the string s, but Constantine cannot allow this and decided to hinder him. He is planning to swap two characters on two different positions in the string s so that Mike wouldn’t be able to find what he looks for. Which two characters Constantine should swap?


    Input
     The only line contains from 2 to 2*10^5 lowercase Latin letters — the string s, in which Mike wants to find a substring «happiness».


    Output
     If Constantine succeeds in achieving his goal, in the first line output «YES» without quotes. In the second line output two distinct integers separated by a space — the positions of characters in the string s, which Constantine should swap. Positions in the string are numbered from one. If there are several possible answers, output any of them.
     If for any choice of Constantine Mike still would be able to find a substring «happiness», in the only line output «NO» without quotes.

    Examples

    standard input
    pursuingthehappiness


    standard output
    YES
    15 18


    standard input
    happinessformehappinessforyouhappinessforeverybodyfreeandletnoonebeleftbehind


    standard output
    NO

    由于数据量较小,不用KMP也可。

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     int len, i, j, N;
     7     char a[200005], b[15] = "happiness", t;
     8     int re[5], num;
     9     N = 9;
    10     num = 0;
    11     scanf("%s", a);
    12     len = strlen(a);
    13     for(i=0; i+N-1<len; i++)
    14     {
    15         for(j=0; j<N; j++)
    16         {
    17             if(a[i+j]!=b[j]) break;
    18         }
    19         if(j==N)
    20         {
    21             re[num++] = i+1;
    22             if(num>=3) break;
    23         }
    24     }
    25     if(len < 9)
    26     {
    27         printf("YES
    ");
    28         printf("%d %d
    ", 1, 2);
    29     }
    30     else if(i+N-1<len) printf("NO
    ");
    31     else
    32     {
    33         if(num==0)
    34         {
    35             t = a[0];
    36             a[0] = a[1];
    37             a[1] = t;
    38 
    39             for(i=0; i+N-1<len; i++)
    40             {
    41                 for(j=0; j<N; j++)
    42                 {
    43                     if(a[i+j]!=b[j]) break;
    44                 }
    45                 if(j==N) break;
    46             }
    47 
    48             if(i+N-1>=len)
    49             {
    50                 printf("YES
    ");
    51                 printf("%d %d
    ", 1, 2);
    52             }
    53 
    54             else
    55             {
    56                 printf("YES
    ");
    57                 printf("%d %d
    ", 1, 3);
    58             }
    59 
    60         }
    61         else if(num==1)
    62         {
    63             printf("YES
    ");
    64             printf("%d %d
    ", re[0], re[0]+1);
    65         }
    66         else
    67         {
    68             printf("YES
    ");
    69             printf("%d %d
    ", re[0], re[1]+1);
    70         }
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    EF6学习
    jQuery中each的用法之退出循环和结束本次循环
    Convert.ToInt16(s);int.Parse(s);和(int)s的区别
    .net网站部署winserver2008R2 iis浏览只显示目录浏览
    二维数组按某列排序
    XML文件的操作
    .net中 IOC DI
    .net在Oracle数据库中为In条件查询防止sql注入参数化处理
    测试代码耗时的时间段(.net)
    沙箱测试
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11559246.html
Copyright © 2011-2022 走看看