1
String s="101010101010";
String sub=""; //substring
int k=2;


   package coreJava;
    import java.util.Scanner; 
    public class substring {    
           public static void main(String args[])
           {
              String string, sub;
              int k, c, i;

              Scanner in = new Scanner(System.in);
              System.out.println("Enter a string to print it's all substrings");
              string  = in.nextLine();

              i = string.length();   

              System.out.println("Substrings of \""+string+"\" are :-");

              for( c = 0 ; c < i ; c++ )
              {
                 for( k = 1 ; k <= i - c ; k++ )
                 {
                    sub = string.substring(c, c+k);
                    System.out.println(sub);
                 }
              }
           }
        }
  1. バイナリ文字列 s="1010011010" を取得します。//等
  2. 変数 k=2 を 1 つ取ります。
  3. 別の変数 i を取ります。//これは部分文字列の長さです(i>k)

ここで、上記の文字列の部分文字列を見つけたいと思います。k=2 の場合、部分文字列の 1 の数は 2 でなければならず、k=3 の場合、部分文字列の 1 の数は 3 でなければなりません。 ...

Output should be like this: 
string s="1010011010" 
Enter value of k=2; 
Enter length of substring i=3; 
substring= 101 110 101 011
4

3 に答える 3

1

文字を繰り返し、1 の数を数えます。カウンターが目的の数に達したら、反復を停止し、インデックス 0 から取得した場所まで部分文字列を取得します。

String str = "010101001010";
int count = 0;
int k = 2;
int i = 0;
for (; i < str.length() && count < k; ++i)
{
    if (str.charAt(i) == '1') count++;
}
于 2013-09-03T07:58:58.853 に答える
1

現在のウィンドウ内の 1 の数を維持しながら、文字列に沿って移動する目的の部分文字列の長さの「ウィンドウ」を作成します。ウィンドウを 1 つずつ移動する反復ごとに、現在のウィンドウの外にある次の文字、現在のウィンドウの最初の文字をテストし、それに応じてカウントを更新します。各反復中に、カウントが目的の長さに等しい場合は、現在のウィンドウから部分文字列を出力します。

public class Substring {

    public static void main(String[] args) {
        String str = "1010011010";

        int k = 2;
        int i = 3;

        printAllSubstrings(str, i, k);

    }

    private static void printAllSubstrings(String str, int substringLength, int numberOfOnes) {
        // start index of the current window
        int startIndex = 0;

        // count of 1s in current window
        int count = 0;

        // count 1s in the first i characters
        for (int a = 0; a < substringLength; a++) {
            if (str.charAt(a) == '1') {
                count++;
            }
        }

        while (startIndex < str.length() - substringLength + 1) {
            if (count == numberOfOnes) {
                System.out.print(str.substring(startIndex, startIndex + substringLength));
                System.out.print(" ");
            }
            // Test next bit, which will be inside the window next iteration
            if (str.length() > startIndex + substringLength && str.charAt(startIndex + substringLength) == '1') {
                count ++;
            }
            // Test the starting bit, which will be outside the window next iteration
            if (str.charAt(startIndex) == '1') {
                count --;
            }
            startIndex++;
        }   
    }
}

これは以下を出力します:

101 011 110 101 
于 2013-09-03T09:53:15.683 に答える
-1

正規表現を使用できます:

public class BinaryString {

    public static void main(String[] args) {
        String binary = "11000001101110";

        int count = 3;
        String regEx = "1{" + count + "}";

        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(binary);

        if (m.find()) {
            int startIndex = m.start();
            System.out.println("MATCH (@index " + startIndex + "): "+ m.group());
        } else {
            System.out.println("NO MATCH!");
        }
    }
}

出力

MATCH (@index 10): 111
于 2013-09-03T08:27:36.877 に答える