【标题】:密文搜索

标题:密文搜索

福尔摩斯从X星收到一份资料,全部是小写字母组成。
他的助手提供了另一份资料:许多长度为8的密码列表。
福尔摩斯发现,这些密码是被打乱后隐藏在先前那份资料中的。

请你编写一个程序,从第一份资料中搜索可能隐藏密码的位置。要考虑密码的所有排列可能性。

数据格式:

输入第一行:一个字符串s,全部由小写字母组成,长度小于1024*1024
紧接着一行是一个整数n,表示以下有n行密码,1<=n<=1000
紧接着是n行字符串,都是小写字母组成,长度都为8

要求输出:
一个整数, 表示每行密码的所有排列在s中匹配次数的总和。

例如:
用户输入:
aaaabbbbaabbcccc
2
aaaabbbb
abcabccc

则程序应该输出:
4

这是因为:第一个密码匹配了3次,第二个密码匹配了1次,一共4次。


资源约定:
峰值内存消耗(含虚拟机) < 512M

 

CPU消耗  < 5000ms

 

这个题比较有意思,之前被题目设计的全套给坑了,以给出的密码进行全排列来匹配原字符串,这样做就相当繁琐了。

反过来想,其实很简单的:

 

/**
 * 
 */
package 决赛Java大学C组2015;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @作者: gx_143
 * @创建时间: 2017-4-28下午09:17:41
 */
public class T5改 {

	static long ans=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine();
		int n=sc.nextInt();
		while(n>=0){
			n--;
			String s=sc.nextLine();
			count(str,sort(s));
		}
		System.out.println(ans);
	}

	private static void count(String str, String sort) {
		
		for (int i = 0; i <= str.length()-8; i++) {
			String sb=str.substring(i,i+8);
			sb=sort(sb);
			if(sb.equals(sort))
				ans++;
		}
	}

	private static String sort(String s) {
		
		char[] ch=s.toCharArray();
		Arrays.sort(ch);
		String re=new String(ch);
		return re;
	}
}

 

 

 

以下是Java中使用DES算法进行加密和解密的示例代码: ``` import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class DESUtil { // 加密 public static String encrypt(String plaintext, String key) { try { Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptedBytes); } catch (Exception e) { e.printStackTrace(); return null; } } // 解密 public static String decrypt(String ciphertext, String key) { try { Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "DES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext)); return new String(decryptedBytes, StandardCharsets.UTF_8); } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String plaintext = "hello world"; String key = "12345678"; String ciphertext = encrypt(plaintext, key); System.out.println("加密后的密文:" + ciphertext); String decryptedText = decrypt(ciphertext, key); System.out.println("解密后的明文:" + decryptedText); } } ``` 上述代码中,加密和解密的核心部分都使用了Java中的Cipher类来完成。在加密和解密时,需要指定加密算法、工作模式和填充方式,这里使用了DES算法、ECB模式和PKCS5Padding填充方式。同时,还需要使用SecretKeySpec类来指定密钥,并通过init方法初始化Cipher对象。在加密时,将明文转换为字节数组,然后调用doFinal方法进行加密操作,最后使用Base64进行编码。在解密时,将密文使用Base64进行解码,然后调用doFinal方法进行解密操作,最后将解密后的字节数组转换为字符串。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值