해결방법
- ab <-> abab 일 경우
- 포인터를 있다고 가정하여 문자열이 끝나면 다시 0으로 옮겨 주는 방법
코드
C++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string s, t;
int che = 0;
void solve() {
if (s.size() > t.size()) swap(s, t); // swap 개꿀...
int a = 0; int b = 0;
bool flag = false;
int ct = 0;
while (ct <= 500) {
if (s[a] != t[b]) {
che = 0;
flag = true;
break;
}
a++;
b++;
if (a == s.size()) a = 0;
if (b == t.size()) b = 0;
ct++;
}
if (!flag) che = 1;
}
int main() {
cin >> s >> t;
solve();
cout << che << endl;
return 0;
}
Java
import java.io.*;
public class Main {
static int che =0;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();//ab
String t = br.readLine();//abab
if(s.length() > t.length())
{
String tmp;
tmp = s;
s = t;
t = tmp;
}
int a=0;
int b=0;
boolean flag = false;
int ct = 0;
while(ct<=500)
{
if(s.charAt(a) != t.charAt(b))
{
che = 0;
flag = true;
break;
}
a++;
b++;
if(a==s.length())
a=0;
if(b==t.length())
b=0;
ct++;
}
if(!flag) che=1;
System.out.println(che);
}
}
'Algorithm' 카테고리의 다른 글
[백준 2178] 미로찾기 (C++, Java) (0) | 2020.09.06 |
---|---|
[백준_1260] DFSBFS (0) | 2020.09.06 |
[백준2667] 단지번호 붙히기 (C++, Java) (0) | 2020.09.03 |
[백준 1987] 알파벳 (C++, Java) (0) | 2020.09.03 |
[백준 1697] 숨바꼭질 C++, Java (0) | 2020.09.03 |