2020杭电多校第四场 Equal Sentences(DP)

Problem Description
Sometimes, changing the order of the words in a sentence doesn’t influence understanding. For example, if we change “what time is it”, into “what time it is”; or change “orz zhang three ak world final”, into “zhang orz three world ak final”, the meaning of the whole sentence doesn’t change a lot, and most people can also understand the changed sentences well.

Formally, we define a sentence as a sequence of words. Two sentences S and T are almost-equal if the two conditions holds:

  1. The multiset of the words in S is the same as the multiset of the words in T.
  2. For a word α, its ith occurrence in S and its ith occurrence in T have indexes differing no more than 1. (The kth word in the sentence has index k.) This holds for all α and i, as long as the word α appears at least i times in both sentences.

Please notice that “almost-equal” is not a equivalence relation, unlike its name. That is, if sentences A and B are almost-equal, B and C are almost-equal, it is possible that A and C are not almost-equal.

Zhang3 has a sentence S consisting of n words. She wants to know how many different sentences there are, which are almost-equal to S, including S itself. Two sentences are considered different, if and only if there is a number i such that the ith word in the two sentences are different. As the answer can be very large, please help her calculate the answer modulo 109+7.

Input
The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow.

For each test case, the first line contains an integer n(1≤n≤105), the number of words in the sentence.

The second line contains the sentence S consisting of n words separated by spaces. Each word consists of no more than 10 lowercase English letters.

The sum of n in all test cases doesn’t exceed 2×105.

Output
For each test case, print a line with an integer, representing the answer, modulo 109+7.

Sample Input
2
6
he he zhou is watching you
13
yi yi si wu yi si yi jiu yi jiu ba yao ling

Sample Output
8
233

Source
2020 Multi-University Training Contest 4

题意:
一些单词组成的句子,可以交换相邻两个单词。
两个句子相同的条件是单词数目种类相同,且同一个单词在两个句子中的下标差不大于1,这个相等不能传递,求与原句子相等的句子数目。

思路:
按照题意,相邻两个单词肯定只能交换一次。则设 f [ i ] f[i] 代表前 i i 个单词的等价句子数目。要是交换第 i i 个单词和第 i 1 i-1 个单词,则方案数为 f [ i 2 ] f[i-2] ,要是不交换,则方案数为 f [ i 1 ] f[i-1] ,转移就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include <map>
#include <string>

using namespace std;
typedef long long ll;

typedef long long ll;
const int maxn = 1e5 + 7;
const int mod = 1e9 + 7;

string a[maxn];
ll f[maxn];

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        int n;scanf("%d",&n);
        for(int i = 1;i <= n;i++) cin >> a[i];
        f[0] = 1;f[1] = 1;
        for(int i = 2;i <= n;i++) {
            if(a[i] == a[i - 1]) {
                f[i] = f[i - 1];
            } else {
                f[i] = (f[i - 1] + f[i - 2]) % mod;
            }
        }
        printf("%lld\n",f[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/107828281
今日推荐