AOJ基础题 ITP1_8_D Ring

Ring

Write a program which finds a pattern p in a ring shaped text s.
在这里插入图片描述

Input

In the first line, the text s is given. In the second line, the pattern p is given.

Output

If p is in s, print Yes in a line, otherwise No.

Constraints

  • 1 ≤ length of p ≤ length of s ≤100
  • s and p consists of lower-case letters

Sample Input

vanceknowledgetoad
advance

Sample Output

Yes

問題を解く

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char a[1000];
    char b[1000];
    char c[1000];

    cin.getline(a, 1000);
    cin.getline(b, 1000);

    strcpy(c, a);

    strcat(a, c);

    if (strstr(a, b))
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }


    return 0;
}

改进

#include<iostream>
#include<string>

using namespace std;

int main()
{
  string s,p;

  cin >> s >> p;

  // 这里当初没有想到
  s=s+s;

  if(s.find(p)!=-1) cout << "Yes" << endl;
  else cout << "No" << endl;

  return 0;
}