题意
The Codejamon monsters talk in enciphered messages. Here is how it works:
Each kind of monster has its own unique vocabulary: a list of V different words consisting only of lowercase English letters. When a monster speaks, it first forms a sentence of words in its vocabulary; the same word may appear multiple times in a sentence. Then, it turns the sentence into an enciphered string, as follows:
- Randomly shuffle each word in the sentence.
- Remove all spaces.
Understanding the monsters can bring you huge advantages, so you are building a tool to do that. As the first step, you want to be able to take an enciphered string and determine how many possible original sentences could have generated that enciphered string. For example, if a monster’s vocabulary is [“this”, “is”, “a”, “monster”, “retsnom”], and it speaks the enciphered string “ishtsiarestmon”, there are four possible original sentences:
- “is this a monster”
- “is this a retsnom”
- “this is a monster”
- “this is a retsnom”
You have S enciphered strings from the same monster. For each one, can you figure out the number of possible original sentences?
题解
- 每一个单词可以随机打乱排列,用一个数组
vector<int> a(26)
,统计单词中每个字符的个数,可以唯一标识一个单词。 - 用
map
统计单词的个数。 - 状态:
dp[i]
表示长度为i
的enciphered string
的可能的原始句子个数 - 转移:
dp[i] += dp[i - k] * mp[s[i - k...i ]]
,0 <= k <= i
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4000 + 5;
const int mod = 1000000000 + 7;
map<vector<int>, int> mp;
int dp[maxn];
string s;
int T, V, S;
int main(){
freopen("C-large-practice.in", "r", stdin);
freopen("C-large-practice_2.txt", "w", stdout);
cin >> T;
for(int _ = 1; _ <= T; ++_){
cin >> V >> S;
mp.clear();
for(int i = 0; i < V; ++i){
cin >> s;
vector<int> a(26);
for(int i = 0; i < s.length(); ++i) a[s[i] - 'a']++;
mp[a]++;
}
printf("Case #%d:", _);
for(int i = 0; i < S; ++i){
cin >> s;
dp[0] = 1;
for(int j = 0; j < s.length(); ++j){
dp[j + 1] = 0;
vector<int> a(26);
for(int k = 0; k < 20 && j - k >= 0; ++k){
a[s[j - k] - 'a']++;
auto it = mp.find(a);
if(it != mp.end()){
dp[j + 1] = (dp[j + 1] + (long long )dp[j - k] * it->second) % mod;
}
}
}
printf(" %d", dp[s.length()]);
}
printf("\n");
}
return 0;
}