2011-11-08 2 views
2

매우 열심히 질문하는 것이지만 나는 시도 할 것입니다. 내 4 글자 mugo입니다. 또한 무료 문자열 단어 (s)가 있습니다.
Let'say : ogoggmuogss. 내 편지 만 사용하여 단어 (s)를 만들 수 있는지 확인하는 현명한 방법을 찾고 있습니다. 우리는 한번 g을 사용했음을 유의하십시오. 우리는 다시 사용할 수 없습니다.문자가 문자열에 있는지 확인하는 방법은 무엇입니까?

og - possible because we need only **g** and **o** 
ogg - not possible we took **o** and **g**, need the second **g** 
muogss - not possible we took all, need also additional **s** 

그래서 내 전술은 배열을 숯불 내 편지를 가지고 하나 하나를 제거하고 단어 ()를 구축하는 왼쪽 얼마나 많은 확인할 수 있습니다. 하지만 어떻게 든 몇 줄에서 사용할 수 있습니까, 나는 모르겠습니다 - 정규식?

+6

"어떤 사람들은 단지 몇 줄입니다 "알아, 정규 표현식을 사용할거야."이제는 두 가지 문제가 있습니다. " - Jamie Zawinski – neeKo

+0

도움이 될 수 있습니다 : http://stackoverflow.com/questions/541954/ –

+0

세계에서 단지 10 가지 유형의 사람들이 있습니다 : 바이너리를 이해하는 사람들과 그렇지 않은 사람들 : – deadfish

답변

7

당신의 방법은 ...

public static bool CanBeMadeFrom(string word, string letters) 
    { 
     foreach (var i in word.Select(c => letters.IndexOf(c, 0))) 
     { 
      if (i == -1) return false; 
      letters = letters.Remove(i, 1); 
     } 
     return true; 
    } 
+0

이 LINQ는 굉장합니다 :) 감사합니다 선생님! – deadfish

0

단어의 정의가 사용 가능한 문자의 임의 순열이라면 왜 정규식이 필요합니까? 각 문자를 한 번만 사용하십시오. 정규식은 "정확한 단어"이 무엇인지 알지 못하며, 정규식을 사용하여 사용하지 않은 것을 사용하는 것보다 알고리즘에 의해 유효하지 않은 문자를 사용하지 않는 것이 좋습니다.

3

다음은 간단한 접근 방법입니다. 소스 단어의 경우 크기가 26 인 배열을 만들어 각 문자가 나타나는 횟수를 계산합니다. 사전의 각 단어에 대해 동일한 작업을 수행하십시오. 그런 다음 둘을 비교하십시오. 모든 문자가 원본 단어와 같은 사전 단어에서 여러 번 나타나면 그 단어를 만드는 데 사용할 수 있습니다. 그렇지 않다면, 그럴 수 없습니다.

C-Sharpish 의사 코드 (아마 작성된 컴파일되지 않습니다) 문제에 직면 할 때

/** Converts characters to a 0 to 25 code representing alphabet position. 
    This is specific to the English language and would need to be modified if used 
    for other languages. */ 
int charToLetter(char c) { 
    return Char.ToUpper(c)-'A'; 
} 

/** Given a source word and an array of other words to check, returns all 
    words from the array which can be made from the letters of the source word. */ 
ArrayList<string> checkSubWords(string source, string[] dictionary) { 

    ArrayList<string> output = new ArrayList<string>(); 

    // Stores how many of each letter are in the source word. 
    int[] sourcecount = new int[26]; // Should initialize to 0, automatically 
    foreach (char c in source) { 
     sourcecount[c]++; 
    } 

    foreach (string s in dictionary) { 

     // Stores how many of each letter are in the dictionary word. 
     int[] dictcount = new int[26]; // Should initialize to 0, automatically 
     foreach (char c in s) { 
      dictcount[c]++; 
     } 

     // Then we check that there exist no letters which appear more in the 
     // dictionary word than the source word. 
     boolean isSubword = true; 
     for (int i=0;i<26;i++) { 
      if (dictcount[i] > sourcecount[i]) { 
       isSubword = false; 
      } 
     } 

     // If they're all less than or equal to, then we add it to the output. 
     if (isSubWord) { 
      output.add(s); 
     } 
    } 
    return output; 
} 
+0

소리가납니다. 달콤한 :) – deadfish

+0

하나의 문자로 작동합니까 :) –

+0

@LB 알파벳 및 charToLetter 기능의 크기를 변경하여 다른 소리 나는 알파벳을 사용할 수 있도록 수정할 수 있습니다. 어떤 글자인지 빨리 알 수 없기 때문에 중국어, 중국어 번체자, 일본어 한자, 히라가나 또는 카타카나가 단순한 것인지 확실하지 않습니다. 그러나 큰 것 중 하나라면 해시 테이블로 전환하고 싶지 않을 것입니다. 그렇지 않으면 매우 희소 한 배열을 검사해야하기 때문입니다. 코드를 조금 복잡하게 만들지 만 동일한 기본 방법이 효과적입니다. –

관련 문제