Minimum Number Of Keystrokes
Description
Given an English word that contains only uppercase and lowercase letters, ask at least a few keystrokes to enter the word (you can press caps lock and shift, and initially enter lowercase letters by default).
- The length of the word does not exceed 200000200000
Example
Give s="Hadoop"
, return 7
Explanation:
Hold down the Shift key and then h to enter H, then press adoop in turn to enter.
Give s="HADOOp"
,return 8
Explanation:
First press caps lock, press hadoo, then caps lock, and finally press p.
Tab可以把大写转小写,也可以把小写转大写,所以还是得用DP
class Solution:
"""
@param s: the English word
@return: The number of keystrokes
"""
def getAns(self, s):
# Write your code here
dp=[[0,1] for _ in range(len(s)+1)]
for i in range(len(s)):
if s[i].isupper():
dp[i+1][0]=min(dp[i][0]+2, dp[i][1]+2)
dp[i+1][1]=min(dp[i][0]+2, dp[i][1]+1)
else:
dp[i+1][0]=min(dp[i][0]+1, dp[i][1]+2)
dp[i+1][1]=min(dp[i][0]+2, dp[i][1]+2)
return min(dp[-1])
Number Of Xor Values
Description
Given n
, m
and an one-dimensional array arr
. Take some elements from arr
to get the XOR of them, and there will be 2^n
results(n
is the size of arr
). Please calculate the number of results that greater than m
,and output the answer modulo 1000000007
.
1<=n,m<=10000
1<=arr[i]<=800
Please pay attention to the memory limit of this problem(32M only)
Example
Givenn=3,m=2,arr=[1,2,3]
,return2
Explanation:
(0 means not taking this number)
0^0^0=0 X
0^0^3=3 √
0^2^0=2 X
0^2^3=1 X
1^0^0=1 X
1^0^3=2 X
1^2^0=3 √
1^2^3=0 X
There are 2 results that greater than m.
Givenn=3,m=0,arr=[1,1,1]
,return4
Explanation:
(0 means not taking this number)
0^0^0=0 X
0^0^1=1 √
0^1^0=1 √
0^1^1=0 X
1^0^0=1 √
1^0^1=0 X
1^1^0=0 X
1^1^1=1 √
There are 4 results that greater than m.
数据有范围,最大800,所以XOR最大1024,然后枚举一遍就好了
class Solution:
"""
@param n: The number of integers
@param m: The lim of xor values
@param arr: The integer values
@return: Please calculate the number of xor values that greater than m ,and output the answer modulo 1000000007
"""
def getAns(self, n, m, arr):
# Write your code here
dp=[0]*1024
dp[0]=1
dp2=list(dp)
for t in arr:
for i in range(1024):
dp2[i^t]+=dp[i]
dp=list(dp2)
return sum(dp[m+1:])%1000000007