from collections import Counter

def get_beauty(string):
string = string.lower()

# Remove all characters other than letters
string = ''.join(x for x in string if 'a' <= x <= 'z' )

# Make a dictionary where the keys are letters and the values are counts
freq = Counter(string)

# Get the values (letter counts) and sort them in descending order
arr = freq.values()
arr.sort()
arr.reverse()

# 26 * (count of most common letter) + (25 * next most common) + ...
values_and_counts = zip(range(26, 0, -1), arr)
return sum(value * count for value, count in values_and_counts)

• 这段文本为空
• 有超过一个的英文字母，空格或者冒号
• 一个左括号，然后是一段“括号平衡”文本，接着一个右括号
• 一段“括号平衡”文本，接着又是一段“括号平衡”文本
• 一个笑脸符号 :) 或者哭脸符号 :(

def isBalanced(message):
minOpen = 0
maxOpen = 0

for i in xrange(len(message)):
if message[i] == '(':
maxOpen += 1
if i != 0 and message[i-1] != ':':
minOpen += 1
elif message[i] == ')':
minOpen = max(0, minOpen-1)
if i != 0 and message[i-1] != ':':
maxOpen -= 1
if maxOpen < 0:
break

if maxOpen >= 0 and minOpen == 0:
return "YES"
else:
return "NO"

M[0] = A
M[i] = (B * M[i - 1] + C) % r, 0 < i < K

（1） M[i] (i >=k) 的最大值不可能大于 k+1。所以即使前面K个数的值最大可以到10^9，最终答案也不可能大于k+1。

2） 根据上一点，M的后半段的值将在每k+1个数后重复。所以即使N很大，我们也只需要计算k+1个数，

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <queue>

using namespace std;

int M[200020];

int main() {
int N; cin >> N;
for(int t = 0; t < N; t++) {
int n, k; cin >> n >> k; n--;
int a, b, c, r; cin >> a >> b >> c >> r;

M[0] = a;
for(int i = 1; i < k; i++) {
M[i] = (1ll * b * M[i - 1] + c) % r; LZ不是写C的，顺便求教一下，1LL是把1转化为long的吗？ 乘以1LL又有什么用？
    }

set<int> st;
for(int i = 0; i <= k; i++) st.insert(i);
for(int i = 0; i < k; i++) st.erase(M[i]);

multiset<int> dupst;
for(int i = 0; i < k; i++) dupst.insert(M[i]);

for(int i = k; i <= 2 * k; i++) {
M[i] = *st.begin();
st.erase(st.begin());

if(i < 2 * k) {
dupst.erase(dupst.find(M[i - k]));
if(M[i - k] <= k && dupst.find(M[i - k]) == dupst.end()) {
st.insert(M[i - k]);
}
}
}

cout << "Case #" << (t + 1) << ": ";
if(n <= 2 * k) {
cout << M[n] << endl;
} else {
cout << M[k + (n - 2 * k - 1) % (k + 1)] << endl;
}
}
return 0;
}

posted @ 2013-01-31 06:28  爷爷泡的茶  阅读(5425)  评论(19编辑  收藏