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\n");
28         printf("%d %d\n", 1, 2);
29     }
30     else if(i+N-1<len) printf("NO\n");
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\n");
51                 printf("%d %d\n", 1, 2);
52             }
53 
54             else
55             {
56                 printf("YES\n");
57                 printf("%d %d\n", 1, 3);
58             }
59 
60         }
61         else if(num==1)
62         {
63             printf("YES\n");
64             printf("%d %d\n", re[0], re[0]+1);
65         }
66         else
67         {
68             printf("YES\n");
69             printf("%d %d\n", re[0], re[1]+1);
70         }
71     }
72     return 0;
73 }

 

转载于:https://www.cnblogs.com/0xiaoyu/p/11559246.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值