Java implements Blue Bridge Cup algorithm training Bit Compressor (violent)

Test algorithm training Bit Compressor

Problem description
  The purpose of data compression is to reduce the redundancy that occurs when storing and exchanging data. This increases the proportion of effective data and increases the transmission rate. One way to compress a binary string is this:
  replace consecutive n 1s with a binary representation of n (Note: the replacement occurs if and only if this replacement reduces the total length of the binary string)
  (Translator's Note: Continuous The n around 1 must be 0 or the beginning and end of the string) For
  example: 11111111001001111111111111110011 will be compressed to 10000010011110011. The original string length is 32 and the compressed string length is 17.
  The disadvantage of this method is that sometimes the decompression algorithm will get more than one possible original string, making it impossible for us to determine what the original string is. Please write a program to determine whether we can use the compressed information to determine the original string. Give the original string length L, the number N of 1 in the original string, and the compressed string.
  L <= 16 Kbytes, the compressed string length <= 40 bits.
Input format The
  first two integers L, N, the meaning is the same as the problem description The
  second line is a binary string, which represents the compressed string.
Output format
  Output "YES" or "NO" or "NOT UNIQUE" (without the quotation marks)
  , respectively :
  YES: the original string is unique
  NO: the original string does not exist
  NOT UNIQUE: the original string exists but is not unique

样例输入
样例132 26
10000010011110011
样例29 7
1010101
样例314 14
111111
样例输出
样例1:YES
样例2:NOT UNIQUE
样例3:NO
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main { 
// 转自:	https://blog.csdn.net/a1439775520   
static  int l; 
static int n; 
    static int ans=0;
    static  int len; 
    static  char[] s;
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter wr=new BufferedWriter(new OutputStreamWriter(System.out));
        String []nm=br.readLine().split(" ");
        l=Integer.parseInt(nm[0]);
        n=Integer.parseInt(nm[1]);
        s=br.readLine().toCharArray();
       // wr.write(Arrays.toString(s));
        len=s.length;
        dfs(0,0,0);
        //wr.write(ans+" ");
        //wr.write(" "+l+" "+n+" "+len);
        if(ans>=2){
            wr.write("NOT UNIQUE");
        }
        else if(ans==1)
        {
            wr.write("YES");
        }
        else
        {
            wr.write("NO");
        }
        wr.close();
    } 
static void dfs(int i,int num,int curlen)
{
    if(ans>=2||num>n||curlen>l){return;}
    if(i>=len)
    {
        if(curlen==l&&num==n)
        {
            ans++;
        }
        return;
    } 
    if(s[i]=='0')
    {
        dfs(i+1,num,curlen+1);
        return;
    } 
    if(i!=0&&s[i-1]=='1')
    {
        dfs(i+1,num+1,curlen+1);
        return;
    } 
    dfs(i+1,num+1,curlen+1); 
    int tem=0; 
    for (int j=i;j<len;j++)
    {
        //
        tem*=2;
        tem+=s[j]-'0'; 
        if (tem+num>n||tem+curlen>l)
        {
            break;
        } 
        if(tem>j-i+1&&(j+1==len||(j+1<len&&s[j+1]=='0')))
        {dfs(j+1,num+tem,tem+curlen);}
    }


}
}

1794 original articles published · 30,000 likes + · 4.49 million views

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105488855