Srm 225 div2 1000pts

这题很简单,只要会灵活的运用java的库函数即可,当然,库函数的复杂度事先也是应该要有所知道的。
代码如下:
public class ImageSteganography {
   public String[] encode(String[] img, String message) {
		String[] ans=null;
		
		String encodeMes=encodeMessage(message);
		String encodeImg[]=new String[img.length];
		for(int i=0;i<img.length;i++)
			encodeImg[i]=encodeImage(img[i]);
		
		String con=complete6Chars(Integer.toBinaryString(63));
		String cons[]=new String[3];
		for(int i=0;i<con.length();){
			cons[i/2]=con.substring(i,i+2);
			i=i+2;
		}
		
		StringBuffer sb=new StringBuffer();
		for(String s:encodeImg)
			sb.append(s);
		
		//System.out.println(sb.length()+":"+img[0].length()/3);
	
		int k=0;
		for(int i=0;i<encodeMes.length();){
			String replaceStr=encodeMes.substring(i,i+6);
			String temp[]=new String[3];
			for(int j=0;j<replaceStr.length();){
				temp[j/2]=replaceStr.substring(j,j+2);
				j=j+2;
			}
			for(int j=temp.length-1;j>=0;j--){
				int start=k*8+6;
				int end=k*8+8;
				sb.replace(start,end,temp[j]);
				k++;
			}
	
			i=i+6;
		}
	
		int m=cons.length-1;
		int size=sb.length()/8;
		for(;k<size;k++){
			String replaceStr=cons[m];
			int start=k*8+6;
			int end=k*8+8;
			sb.replace(start,end,replaceStr);
			m=(m-1+3)%3;
		}
			
		ans=decodeImage(sb,img);
		
		return ans;
   }
   
   public String[] decodeImage(StringBuffer sb,String[] img){
   			String[] imgs=new String[img.length];
   			int start=0;
   			int end=0;
   			for(int i=0;i<imgs.length;i++){
   				start=end;
   				end=end+img[i].length()/3*8;
   				imgs[i]=sb.substring(start,end);
   			}
   			
   			for(int i=0;i<imgs.length;i++){
   				int n=imgs[i].length();
   				StringBuffer st=new StringBuffer();
   				for(int j=0;j<n;){
   					String c=imgs[i].substring(j,j+8);
   					String s=complete3Chars(Integer.parseInt(c,2)+"");
   					st.append(s);
   					j=j+8;	
   				}
   				imgs[i]=st.toString();
   			}
   			
   			return imgs;
   }
   
  public String complete3Chars(String s){
  			StringBuffer sb=new StringBuffer();
  			for(int i=0;i<3-s.length();i++){
  				sb.append('0');
  			}
  			sb.append(s);
  			
  			return sb.toString();
  }
   
   public String encodeImage(String img){
		   	StringBuffer sb=new StringBuffer();
		 		int n=img.length();
		 		for(int i=0;i<n;){
		 			int c=Integer.parseInt(img.substring(i,i+3));
		 			sb.append(complete8Chars(Integer.toBinaryString(c)));
		 			i=i+3;
		 		}
		   	
		   	return sb.toString();
   }
   
   public String complete8Chars(String s){
   			StringBuffer sb=new StringBuffer();
   			for(int i=0;i<8-s.length();i++)
   				sb.append('0');
   			
   			sb.append(s);	
   			
   			return sb.toString();
   }
   
   public String encodeMessage(String message){
		   	StringBuffer sb=new StringBuffer();
		   	HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
		   	hm.put(' ',0);
		   	for(int i=0;i<26;i++){
		   		hm.put((char)('A'+i),i+1);
		   		hm.put((char)('a'+i),i+27);
		   	}
		   	for(int i=0;i<10;i++){
		   		hm.put((char)('0'+i),i+53);
		   	}
		   	
		   	for(char c:message.toCharArray()){
		   		int i=hm.get(c);
		   		sb.append(complete6Chars(Integer.toBinaryString(i)));
		   	}
		   	
		   	return sb.toString();
   }
	
		public String complete6Chars(String s){
				StringBuffer sb=new StringBuffer();
				for(int i=0;i<6-s.length();i++){
					sb.append('0');
				}
				sb.append(s);
				
				return sb.toString();
		}
}


complete那几个函数有点冗余,可以重构一下,但我懒得再去重构了。。。。。

你可能感兴趣的:(C++,c,C#,J#)