bigram이란 알파벳 두 글자로 이루어진 패턴을 의미한다. 예를 들어, I have a hat 이란 문장에는 ha, av, ve, at 이렇게 네가지의 bigram이 있다 (ha는 두 번 등장). 입력 첫째 줄과 둘째 줄에 있는 bigram 중에 공통인 것을 ascii순으로 정렬해 출력하자. 입력 각 줄에는 공백 포함 최대 10000글자가 있다.
* 대소문자 구분
* 특수문자 무시 (bigram에 포함하지 않음)
* ascii 순이란 A..Z가 먼저 오고 그 다음 a..z가 온다는 뜻
입력
frequency of letters, in text
require equal amounts, of time
출력
eq of qu re
이건데 교수가 인터넷으로 제출하라고 해서 넣었는데 1,3번은 통과인데 2번에서 자꾸에러나는데
틀렸다고만 하고 뭐가 틀렸는지 안알려줘서 돌아버릴거 같다. 좀 도와주라.
package ex1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class ex1 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
bigram first,second;
System.out.print("input first:");
first = new bigram(sc.nextLine());
first.makeBigram();
// first.printBigram();
System.out.print("input second:");
second = new bigram(sc.nextLine());
second.makeBigram();
// second.printBigram();
first.compare(second);
}
}
class bigram{
private String bigramInput;
private ArrayList<String> rawbigram;
public ArrayList<String> bigram;
public bigram(String bigramInput){
this.bigramInput = bigramInput;
}
public void makeBigram(){
String match = "[^\uAC00-\uD7A3xfe0-9a-zA-Z\\s]";
bigramInput = bigramInput.replaceAll(match, " ");
String[] word = bigramInput.split(" ");
rawbigram = new ArrayList<String>();
for(String token:word){
if(token.length()>=3){
for(int i = 0; i<token.length()-2; i++){
rawbigram.add(token.substring(i,i+2));
}
rawbigram.add(token.substring(token.length()-2));
}
else if(token.length()==2){
rawbigram.add(token);
}
}
bigram = new ArrayList<String>(new HashSet<String>(rawbigram));
Collections.sort(bigram);
}
public void printBigram(){
for(int i = 0; i < bigram.size(); i++) {
System.out.print(bigram.get(i)+" ");
}
System.out.println();
}
public void compare(bigram other){
for(String str:bigram){
if(other.bigram.contains(str)){
System.out.print(str+" ");
}
}
System.out.println();
}
}
^\uAC00-uD7A3xfe0-9a-zA-Z\\s