Google-APAC2015-"Password Attacker"
来源:互联网 发布:中国乘用车8月销量数据 编辑:程序博客网 时间:2024/05/21 16:40
昨天在焦虑中度过了大部分时间,然后刷了10+集火影来弥补被狗吃掉的童年。前天去了De Young美术馆,下一篇blog再po一点图吧。
反正我觉得焦虑是最没有意义的事情。加油啊!
========================================
原题:password attacker
# 1 题目理解:
假设用户使用M个不同的key,去组成长度为N的密码(M<=N),那么有多少种不同的密码。
来看一下test cases。
输入是:
1 1
3 4
5 5
输出是:
Case #1: 1
Case #2: 36
Case #3: 120
比如说M=3, N=4,那么就有36种密码组合。
注意,因为有可能数字太大会溢出,所以所有结果要对1000000007求余。
========================================
#2 算法:
使用dynamic programing。dp[i][j]表示使用i个不同的key去排列长度为j的密码的所有排列方法。
状态转移方程是:
dp[i][j] = i * (dp[i-1][j-1] * j + dp[i][j-1])
这样理解:
考虑长度为j的密码,最后一位肯定是i个key里面的一员,因此有i种情况;
最后一位决定之后,考虑前j-1位,有可能前面没有用过这个key,那么有dp[i-1][j-1]种情况;有可能前面已经使用过这个key,那么有dp[i][j-1]种情况。
========================================
#3 数据结构:
二维数组dp[M+1][N+1]
========================================
#4 代码:
import java.util.*;import java.io.*;public class Solution {public static void main(String[] args) {File inFile = new File("A-large-practice.in");File outFile = new File("A-large-practice.out");try {BufferedReader br = new BufferedReader(new FileReader(inFile));BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));int T = Integer.parseInt(br.readLine());for (int i = 1; i <= T; i++) {String s = br.readLine();String[] parts = s.split("\\s");int M = Integer.parseInt(parts[0]);int N = Integer.parseInt(parts[1]);bw.write("Case #" + i + ": " + solve(M, N) + "\n");}bw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/* dynamic programing */public static long solve(int M, int N) {long mod = 1000000007;long[][] dp = new long[M+1][N+1];// base casefor (int j = 1; j <= N; j++) {dp[1][j] = 1;}// dpfor (int i = 2; i <= M; i++) {for (int j = i; j <= N; j++) {dp[i][j] = i * (dp[i-1][j-1] + dp[i][j-1]);dp[i][j] %= mod;}}return dp[M][N];}}
- Google-APAC2015-"Password Attacker"
- Google APAC test 2015 Round B Problem A - Password Attacker
- Problem A. Password Attacker
- Google-APAC2015-Addition (1)
- Google-APAC2015-Addition (2)
- Google-APAC2015-"cut tiles"
- Google-APAC2015-"super 2048"
- Google-APAC2015-"Seven-segment display"
- Google-APAC2015-"New Years Eve"
- attacker
- replay attacker
- CODE JAM APAC 2015 round B: Problem A. Password Attacker(小球放进盒子的计数问题)
- WS-Attacker v.1.1 Released
- password
- password
- Password
- password
- Password
- kO9f RqdXzlouis vuitton outlet obxx 9OE
- kqfs 0FAeKcheap burberry s7P9 Ops
- io流的小事例
- CGAffineTransformMakeTranslation的使用问题
- topcoder Single Round Match 457 Round 1 - Division I, Level Two baTheHexagonsDivOne
- Google-APAC2015-"Password Attacker"
- Android AsynTask 异步线程处理讲解与代码测试
- SPPS java 创建template
- 去哪兒網服務電話號碼是多少-百度百科
- 面经
- 去哪兒網官方電話號碼是什麽-百度百科
- boost环境搭建
- 高效SQL语句必杀技
- boost多线程简单实现