728x90
728x90

Java

System I/O

// 출력

System.out.println("...");
System.out.print("...");
// 1개
System.out.printf("... %d | %s",값1);
// 2개
System.out.printf("... %1$d  %2$s",값1,값2,...);


// 입력

int keyCode = System.in.read();

Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
정수 %d
%6d
실수 %f
%.2f
문자열 %s
특수
문자
\t
\n
%%
  • 비교 : 기본타입 == , 문자열 equals()
728x90

'programming language > java' 카테고리의 다른 글

[Java] 변수  (0) 2023.09.30
[JAVA] Serializable 직렬화 - 역직렬화  (0) 2023.03.08
728x90

Java

Variable

  • 변수
  • 하나의 변수 하나의 타입
  • 변수 선언 : Type VariableName = Value ;
  • Keyword
기본타입 boolean,byte,char,short,int,long,float,double
접근 제한자 private, protected,public
*클래스 class,abstarct,interface,extends,implements,enum
*객체 new, instanceof,this,super,null
*메소드 void,return
*제어문 id,else,switch,case,defualt,for,do,while,break,continue
논리 true,false
*예외처리 try,catch,finally,throws
* 기타 package,import,syschronized,final,static
  • 로컬 변수 : 메소드 내에서 사용 메소드 실행이 끝나면 없어진다.
  • 변수 사용은 블록 내에서만
728x90

'programming language > java' 카테고리의 다른 글

[JAVA] System 입출력  (0) 2023.10.05
[JAVA] Serializable 직렬화 - 역직렬화  (0) 2023.03.08
728x90
from datetime import date
from dateutil.relativedelta import relativedelta

today = date.today()

print(today)

sub_date = today - relativedelta(years=2)

print(sub_date)

 

728x90
728x90

ahocorasick 알고리즘은 문자열 검색 알고리즘 중 하나로, 다중 문자열 검색(multiple pattern matching)을 수행하는 알고리즘입니다.

문자열들을 Trie 자료구조로 생성하고, 이를 기반으로 탐색하여 문자열 검사를 실시합니다.

핵심 사항은 검색 하고자하는 문자열을 입력으로 받은 후에 검색이 끝나기전에 검색결과가 나온후에는 다시 앞으로 돌아가는것는 과정이 포함됩니다. 실패 링크

https://ko.wikipedia.org/wiki/%EC%95%84%ED%98%B8_%EC%BD%94%EB%9D%BC%EC%8B%9D_%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98

class Node:
    def __init__(self,text):
        self.text = text
        self.last = 0
        self.output = ""
        self.child = {}
        self.fail = None
        self.rank = 0
    def show(self):
        print( "text:", self.text, "output:",self.output, "fail:", self.fail.text if self.fail is not None else "None","rank:",self.rank)
        if self.last:
            print( "LAST OUTPUT : ", self.output)
        for k in self.child.keys():
            s = self.child[k]
            s.show()
class AhocorasickTrie:
    
    def __init__(self):
        self.no = 0
        self.root = Node(None)
    
    def add(self,text):
        rank = 0
        currentNode = self.root
        for t in text:
            if t not in currentNode.child:
                self.no +=1
                currentNode.child[t] = Node(t)
                print(f"ADD : {t} || Current No. : {self.no}")
            currentNode.rank = rank
            rank+=1
            currentNode = currentNode.child[t]
        currentNode.output = text
        currentNode.last = 1
        currentNode.rank = rank
        print(f"TOTAL : {self.no}")
        
        
    def set_failur(self):
        queue = []
        for c,node in self.root.child.items():
            node.fail = self.root
            queue.append(node)
        while queue:
            currentNode = queue.pop(0)
            for t,node in currentNode.child.items():
                fail_node = currentNode.fail
                while fail_node and t not in fail_node.child:
                    fail_node = fail_node.fail
                if fail_node:
                    node.fail = fail_node.child[t]
                else:
                    node.fail = self.root
                queue.append(node)
    
    def show(self):
        self.root.display()  
        
    def find(self,text):
        result = []
        currentNode = self.root
        for t in text:
            while currentNode is not None and t not in currentNode.child:
                print("fail",currentNode.text)
                currentNode = currentNode.fail
            if t in currentNode.child:
                currentNode = currentNode.child[t]
                if currentNode.output != "":
                    result += [currentNode.output]
                    print(result,t,currentNode.output,currentNode.last,currentNode.output)
                    
        return result

 

728x90
728x90

Trie는 문자열 검색 알고리즘 중 하나로, 문자열을 저장하고 검색하는 데 유용합니다.

이진 탐색 트리와 달리, Trie는 문자열을 저장하는 트리 자료구조입니다.

Trie의 각 노드가 문자열의 한 문자에 대응되는 것입니다.

Trie를 이용해 문자열을 빠르게 검색 할 수 있습니다.

검색을 수행할 문자열을 루트에서 시작해 각 문자에 해당하는 노드들을 따라 검색을 합니다.

class Node:
    def __init__(self,text):
        self.text = text
        self.last = 0
        self.child = {}
        
class Trie:
    
    def __init__(self):
        self.no = 0
        self.root = Node(None)
    
    def add(self,text):
        currentNode = self.root
        for t in text:
            if t not in currentNode.child:
                self.no +=1
                currentNode.child[t] = Node(t)
                print(f"ADD : {t} || Current No. : {self.no}")
            currentNode = currentNode.child[t]
        currentNode.last = 1
        print(f"TOTAL : {self.no}")
        
    def find(self,text):
        result = ""
        currentNode = self.root
        for t in text:
            if t in currentNode.child:
                result += t
                currentNode = currentNode.child[t]
        return result
728x90
728x90
728x90

+ Recent posts