알로리즘 문젠데 ... 도저히 ㅠㅠ

익명 사용자의 이미지

안녕하세요

제가 프로그래밍중 다음과 같은 문제에 봉착했는데, 제가 기초가 부족
해서 인지 하루종일 생각해 봐도 통 감이 안옵니다.

배열, 혹은 파일에 다음과 같은 데이타가 있을때,

aaa
aaa
bbb
bbb
bbb
ccc
ccc
ccc
ccc

이것을 다음과 같이 정렬합니다.

ccc
ccc
ccc
ccc
bbb
bbb
bbb
aaa
aaa

즉 c 가 가장 많으니까, 제일 위로 올라가고 그다음 b a 이런 식으로
정렬이 되게 하는 겁니다.
생각날듯 말듯, 생각이 않나는 군요 ㅠㅠ

익명 사용자의 이미지

안녕하세요...

Step 1)
일단은 aaa, bbb, ccc 이 세가지 패턴이 있다면은 이것들이 전부 몇 번이
오는지를 먼저 판별을 하세요...

Step 2)
위 Step 1) 에서 나온 값을 근거로 제일 큰 값을 가진 것들을 우선 처음
에 정렬을 하고...
다시 그 다음 값을 가진 패턴을 이에 붙혀 주는 식으로 하면은 될 것 같네
요...

_ 信

익명 사용자의 이미지

패턴의 종류와 수만 기억한 다음...

이 수에 따라 패턴 종류를 정렬하고 차례대로

수만큼 각 패턴을 출력해 주면 되겠네요.

그럼 고운 하루...

익명 사용자의 이미지

이런 경우는 트리나 해쉬테이블과 같은 연관 컨테이너(associative container)를 사용하는 방법이
가장 간단하고, 빠르며, 메모리 사용량도 적습니다.

다음은, STL의 연관 컨테이너인 map과 multimap을 사용하는 방법입니다.

//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include

//-------------------------------------------------------------------
using namespace std;

int main()
{
typedef vector vec_str;
vec_str input_words, output_words;
input_words.push_back("aaa");
input_words.push_back("aaa");
input_words.push_back("bbb");
input_words.push_back("bbb");
input_words.push_back("bbb");
input_words.push_back("ccc");
input_words.push_back("ccc");
input_words.push_back("ccc");
input_words.push_back("ccc");

typedef map freq_map; // map 대신 hash_map을 써도 좋습니다.
freq_map freq;
for (size_t i = 0; i < input_words.size(); ++i)
// freq에 words[i]가 있으면 1만큼 증가,
// 없으면 새로 추가하고 빈도를 1로 세팅.
freq[input_words[i]]++;

typedef multimap invert_freq_map;
invert_freq_map invert_freq;
for (freq_mapiterator i = freq.begin(); i != freq.end(); ++i)
invert_freq.insert(invert_freq_mapvalue_type(i->second, i->first));
// 단어와 빈도를 거꾸로 해서 multimap에 저장

for (invert_freq_mapiterator i = invert_freq.begin();
i != invert_freq.end(); ++i)
for (int j = 0; j < i->first; ++j) // 빈도만큼 output_words에 삽입
output_words.push_back(i->second);

copy(output_words.rbegin(), output_words.rend(),
ostream_iterator(cout, "\n")); // output_words를 cout에 출력

return 0;
}

익명 사용자의 이미지

#!/usr/bin/perl

# 정말 많은 분들이 답변 해 주셨습니다.
# 그런데 저는 perl 로 해봤습니다.
# 이걸아마 C/C++ 로 했다면, .... 아마 저는 휴~

@array = qw( 3 2 3 1 3 2 1 1 1 1 3 ); # 배열에 임의의 숫자 저장

# 일단 숫자 별로 sorting ex) 1 1 1 1 1 2 2 3 3 3 ...
# 그러나 이건 우리가 원하는 답이 아니죠? ^^;

@sortarr = sort { $a <=> $b } @array;

# 배열을 돌면서....
for($i = 0; $i < @sortarr; $i++ )
{
$value = $sortarr[$i];

# 빈도수를 변수에 저장..
$ret = grep(/$value/, @sortarr);

# 빈도수+데이타 <- 요런 식으로 배열에 푸쉬....
push(@part, "$ret" . "+" . "$sortarr[$i]");
}

# 앞에 빈도수 를 기준으로 거꾸로 정렬
# 그럼 가장 빈도수가 높은 데이타가 위로 가지요 ^^;

@sortarr = reverse sort { $a <=> $b } @part;

# 그 다음에 빈도수를 제외하고 데이타만 출력하면
for($i = 0; $i < @sortarr ; $i++ )
{
my ($q, $p) = split(/\+/, $sortarr[$i]);
print "$p\n";
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.