"BOJ 1316 그룹 단어 체커"의 두 판 사이의 차이

(새 문서: 분류: BOJ 7단계 ==개요== * {{BOJ|1316}} * 알고리즘 분류: 문자열 처리, 탐색 ==Java== 분류: BOJ Java <source lang='java'> import java.util.Scanner; public...)
 
 
(사용자 3명의 중간 판 12개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: BOJ 7단계]]
==개요==
==개요==
* {{BOJ|1316}}
{{BOJ|단계=6}}
* 규칙에 맞는 알파벳의 개수를 출력하는 문제1
* 알고리즘 분류: 문자열 처리, 탐색
* 알고리즘 분류: 문자열 처리, 탐색
==C++==
<syntaxhighlight lang='cpp'>
#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';
}
</syntaxhighlight>


==Java==
==Java==
[[분류: BOJ Java]]
<syntaxhighlight lang='java'>
<source lang='java'>
import java.util.Scanner;
import java.util.Scanner;
public class Main {
public class Main {
12번째 줄: 37번째 줄:
         for(int i=0; i<word.length(); i++) {
         for(int i=0; i<word.length(); i++) {
             char a = word.charAt(i);
             char a = word.charAt(i);
            //System.out.println(a);
             boolean continuos = true;
             boolean continuos = true;
             for(int j=i+1; j<word.length(); j++) {
             for(int j=i+1; j<word.length(); j++) {
                 char b = word.charAt(j);
                 char b = word.charAt(j);
                //System.out.print(a + "-" + b + " ");
                //System.out.println(continuos);
                 if( a == b ) {
                 if( a == b ) {
                     if( !continuos ) return false;
                     if( !continuos ) return false;
30번째 줄: 52번째 줄:
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
        /*
        System.out.println( isGroupWord("ccazzzzbb") );
        System.out.println( isGroupWord("kin") );
        System.out.println( isGroupWord("aabbbccb") ); // false
       
        System.out.println( isGroupWord("happy") );
        System.out.println( isGroupWord("new") );
        System.out.println( isGroupWord("year") );
        System.out.println( isGroupWord("aba") ); // false
        System.out.println( isGroupWord("abab") ); // false
        System.out.println( isGroupWord("abcabc") ); // false
        System.out.println( isGroupWord("a") );
        */
       
         Scanner sc = new Scanner(System.in);
         Scanner sc = new Scanner(System.in);
         int n = sc.nextInt();
         int n = sc.nextInt();
55번째 줄: 62번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
 
==Perl==
<syntaxhighlight lang='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);
</syntaxhighlight>
 
==PHP==
<syntaxhighlight lang='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;
</syntaxhighlight>
 
==Python==
<syntaxhighlight lang='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)
</syntaxhighlight>

2023년 8월 26일 (토) 18:29 기준 최신판

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 }}