16570번: 앞뒤가 맞는 수열
수열 (a1, a2, ⋯, aN) 이 다음의 성질을 가지면 그 수열은 k-앞뒤수열 이라고 한다. (a1, a2, ⋯, ak) = (aN-k+1, aN-k+2, ⋯ , aN), 1 ≤ k < N 어떤 수열이 k-앞뒤수열일 때, k의 최댓값 k*를 그 수열의 앞뒤계
www.acmicpc.net
- 생각
KMP알고리즘으로 풀면 쉽게 풀 수 있을 것 같다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class Algorithm {
static int[] array, pi;
static int N;
public static void main(String[] args) throws Exception {
SetData();
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
array = new int[N];
pi = new int[N];
for (int i = N - 1; i >= 0; i--) {
array[i] = in.nextInt();
}
KMP();
int length = 0, count = 0;
for (int i = 0; i < N; i++) {
if (pi[i] > length) {
length = pi[i];
count = 0;
}
if (pi[i] == length)
count++;
}
if (length == 0)
System.out.println("-1");
else
System.out.println(length + " " + count);
}
public static void KMP() {
int j = 0;
for (int i = 1; i < N; i++) {
while (j > 0 && array[j] != array[i]) {
j = pi[j - 1];
}
if (array[j] == array[i]) {
pi[i] = ++j;
}
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
- KMP 알고리즘이 아니여도 풀 수 있는 문제이다.
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class Algorithm {
static int[] array, pi;
static int N;
public static void main(String[] args) throws Exception {
SetData();
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
array = new int[N + 1];
pi = new int[N + 1];
for (int i = 1; i <= N; i++) {
array[i] = in.nextInt();
}
KMP();
for (int i = N; i >= 1; i--) {
if (pi[i] != 0) {
System.out.println(i + " " + pi[i]);
System.exit(0);
}
}
System.out.println(-1);
}
static void KMP() {
for (int i = N - 1; i >= 1; i--) {
int count = 0;
int j = N;
int index = i;
while (array[index--] == array[j--]) {
count++;
}
pi[count]++;
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 14888번 : 연산자 끼워넣기 (0) | 2021.03.26 |
---|---|
[SW Expert Academy/JAVA] 1767번 프로세서 연결하기 (0) | 2021.03.26 |
[BOJ/JAVA] 백준 1786번 : 찾기 (0) | 2021.03.16 |
[BOJ/JAVA] 백준 5052번 : 전화번호 목록 (0) | 2021.03.15 |
[BOJ/JAVA] 백준 14890번 : 경사로 (0) | 2021.03.14 |