String compression algorithm improves

Problem Description
  write a program, an input string, then follows the compression rules every character string from among:
  (1) If the character is a space, the character is retained;
  (2) if the character is the first occurrence or third or sixth appearance appears, leave the character;
  (3) otherwise, delete the character.
  For example, if the user inputs "occurrence", after the compression, the second occurrence of the character c is deleted, the first and third time remains; the second occurrence of the character e and r are deleted, so the final result It is: "ocurenc".
  Input formats: Enter only one line, that is, the original string.
  Output Format: Output a single line, i.e., through the string after compression.
O Sample
Sample Input
occurrence
Sample Output
ocurenc

package Main;
import java.math.BigInteger;
import java.util.*;
public class Main {
	static String s,a="";//s输入字符串,a记录出现在s的字符串有哪些
	static int []b;//b记录a对应下标的字符在s中出现的次数
	public static void main(String []args) {
		Scanner input=new Scanner(System.in);
s=input.nextLine();
b=new int[s.length()];
for(int i=0;i<s.length();i++)
	if(jie(s.charAt(i)))//如果首次出现,将之加入a;
	{a+=s.charAt(i);
	b[a.length()-1]=1;
	System.out.print(s.charAt(i));
	}
			}
	public static boolean jie(char zi) {//检查字符在输入字符串里出现的第几次,如果zi首次出现返回true,否则false(空格除外)
		int i;
		for( i=0;i<a.length();i++)
			if(zi==' ')
				{System.out.print(zi);
				return false;}
			else if(a.charAt(i)==zi)
				{b[i]++;
				if(b[i]==3|b[i]==6)
					System.out.print(zi);
					   return false;}
         return true;			}
				}



在这里插入代码片
Published 130 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/104605210