Round D APAC Test 2017--Problem C. Codejamon Cipher(dp)

Problem C. Codejamon Cipher

题意

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:

  1. Randomly shuffle each word in the sentence.
  2. 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]表示长度为ienciphered 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;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值