多校第四场——1005 Equal Sentences

本文介绍了一种算法,用于计算与给定句子S相似的句子T的数量,包括S本身。相似性定义为单词的多重集相同,且相同单词在两句话中出现位置的索引差不超过1。通过分析相邻单词是否相同进行状态转移,最终得到所有可能的相似句子数量。
摘要由CSDN通过智能技术生成

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

题意:给一句话S,包含n个单词,求与其相似的句子T的个数,包含自己。相似满足两个条件:
①单词的多重集相同
②一个单词在S中出现的第i个词在T中出现第i个词的索引相差不超过1。

思路:如果相邻两单词相同,则交换后不改变,则f[i+1]=f[i];
如果不相等,则交换,那么f[i] = f[i-1] + f[i-2];一直到f[n]时算出结果。

代码:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int num[N];
string Str[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        num[0] = 1;
        num[1] = 1;
        for (int i = 1; i <= n; i++)
        {
            cin >> Str[i];
        }
        for (int i = 2; i <= n; i++)
        {
            if (Str[i] == Str[i - 1])
            {
                num[i] = num[i - 1];
            }
            else
            {
                num[i] = (num[i - 1] + num[i - 2]) % mod;
            }
        }
        cout << num[n] << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>