BOJ 1316 그룹 단어 체커

1 개요[ | ]

BOJ 1316 그룹 단어 체커
  • 규칙에 맞는 알파벳의 개수를 출력하는 문제1
  • 알고리즘 분류: 문자열 처리, 탐색

2 C++[ | ]

#include <iostream>
using namespace std;

bool isGroupWord(string s) {
    for(int i=0; i<s.length()-1; i++) {
        if(s[i] == s[i+1]) continue;
        if(s.find(s[i], i+2) != string::npos) return false; 
    }
    return true;
}

int main() {
    int N;
    cin >> N;
    string s;
    int count = 0;
    for(int i=0; i<N; i++) {
        cin >> s;
        if(isGroupWord(s)) count++;
    }
    cout << count << '\n';
}

3 Java[ | ]

import java.util.Scanner;
public class Main {
    private static boolean isGroupWord(String word) {
        for(int i=0; i<word.length(); i++) {
            char a = word.charAt(i);
            boolean continuos = true;
            for(int j=i+1; j<word.length(); j++) {
                char b = word.charAt(j);
                if( a == b ) {
                    if( !continuos ) return false;
                    i++;
                }
                else {
                    continuos = false;
                }
            }
        }
        return true;
    }
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        for( int i=0; i<n; i++ ) {
            String word = sc.next();
            if( isGroupWord(word) ) count++;
        }
        System.out.println( count );
    }
}

4 Perl[ | ]

sub isGroupWord {
    $word = shift;
    for $i (0..(length $word)-1) {
        $a = substr($word, $i, 1);
        $continuous = 1;
        for ($i+1..(length $word)-1) {
            $b = substr($word, $_, 1);
            if ($a eq $b) {
                return 0 if (!$continuous);
                $i++;
            } else {
                $continuous=0;
            }
        }
    }
    return 1;
}
$n = <>;
$count = 0;
for (1..$n) {
    $word = <>;
    $count++ if ( isGroupWord($word) );
}
printf("%d\n", $count);

5 PHP[ | ]

<?php
function is_group_word($word) {
    preg_match_all('/(.)\1*/', $word, $matches);
    return ( count($matches[1]) == count(array_unique($matches[1])) );
}
$n = intval(fgets(STDIN));
$cnt = 0;
for($i=0; $i<$n; $i++) {
    $word = rtrim(fgets(STDIN));
    if( is_group_word($word) ) $cnt++;
}
echo $cnt;

6 Python[ | ]

def isGroupWord(word):
    for ch in word:
        if ch*word.count(ch) not in word:
            return False
    return True
TT = int(input())
cnt = 0
for t in range(TT):
    word = input()
    if isGroupWord(word):
       cnt += 1 
print(cnt)
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}