2018暑期SICNU-ACM組集訓(xùn)報(bào)告(5)

題目:

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 · 105
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

原題鏈接:http://codeforces.com/gym/101341/problem/B

題意:
看似很復(fù)雜的題目實(shí)際上讀懂之后只需要明白一點(diǎn),交換字符串中的兩個(gè)位置能否不構(gòu)成“happiness”
值得注意的是,若串中原不含模式串時(shí),這時(shí)隨意交換可能反而會(huì)形成模式串。
那么原串中若有相同字符,互換即可。若沒有,交換頭兩個(gè)字符即可保證不會(huì)形成模式串。
AC代碼:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
    string s,ss;
    ss="happiness";
    cin>>s;
    long long i=0,k,l;
    int cnt=0;
    for(;;)
    {
        if(cnt==0)
        {
            i=s.find(ss);
            if (i!=string::npos)
            {
                cnt++;
                k=i;
            }
            else
                break;
        }
        else
        {
            i=s.find(ss,i+ss.size());
            if (i!=string::npos)
            {
                cnt++;
                if(cnt==2)
                {
                    l=i;
                }
            }
            else
                break;
        }
    }
    if (cnt==0)
    {
        printf("YES\n");
        long long m,n,a=0;
        for(m=0;m<s.size();m++)
        {
            for(n=m+1;n<s.size();n++)
            {
                char s1;
                s1=s[m];
                s[m]=s[n];
                s[n]=s1;
                if(s.find(ss)==string::npos)
                {
                    printf("%lld %lld\n",m+1,n+1);
                    a=1;
                    break;
                }
            }
            if(a==1)
                break;
        }
    }
    else if(cnt==1)
    {
        printf("YES\n");
        long long m,n,a=0;
        for(m=k;m<k+ss.size();m++)
        {
            for(n=m+1;n<k+ss.size();n++)
            {
                char s1;
                s1=s[m];
                s[m]=s[n];
                s[n]=s1;
                if(s.find(ss)==string::npos)
                {
                    printf("%lld %lld\n",m+1,n+1);
                    a=1;
                    break;
                }
            }
            if(a==1)
                break;
        }
    }
    else if(cnt==2)
    {
        printf("YES\n");
        long long m,n,a=0;
        for(m=k;m<k+ss.size();m++)
        {
            for(n=l;n<l+ss.size();n++)
            {
                char s1;
                s1=s[m];
                s[m]=s[n];
                s[n]=s1;
                if(s.find(ss)==string::npos)
                {
                    printf("%lld %lld\n",m+1,n+1);
                    a=1;
                    break;
                }
            }
            if(a==1)
                break;
        }
    }
    else
        printf("NO\n");
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,835評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,032評(píng)論 0 23
  • 時(shí)間和生命,都有長(zhǎng)有短,但又不成正比。 1. “咱們移民吧?” “去哪???” “無所謂,有你就行?!?“那就去北歐...
    張承宇Tomato閱讀 398評(píng)論 0 3
  • 我和他是一個(gè)縣城的,很小的一個(gè)縣城,好的重中就兩個(gè), 而我倆就在一個(gè)初中,初三那樣,他以轉(zhuǎn)校生的身份進(jìn)入了我們班級(jí)...
    逝者君閱讀 2,799評(píng)論 0 0
  • 今兒個(gè)清明,還是出去鍛煉了。說真的,想找借口的時(shí)候真有一百個(gè),不找的時(shí)候啥都不是事,哈哈。 今天換了個(gè)顏色寫寫 連...
    紫看片子閱讀 206評(píng)論 0 0

友情鏈接更多精彩內(nèi)容