E - Equal Sentences

https://vjudge.net/contest/386568#problem/E

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  SS and TT are almost-equal if the two conditions holds:

1. The multiset of the words in SS is the same as the multiset of the words in TT.
2. For a word αα, its ithith occurrence in SS and its ithith occurrence in TT have indexes differing no more than 11. (The kthkth word in the sentence has index kk.) This holds for all αα and ii, as long as the word αα appears at least ii times in both sentences.

Please notice that "almost-equal" is not a equivalence relation, unlike its name. That is, if sentences AA and BB are almost-equal, BB and CC are almost-equal, it is possible that AA and CC are not almost-equal.

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

InputThe first line of the input gives the number of test cases, T(1T100)T(1≤T≤100). TT test cases follow.

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

The second line contains the sentence SS consisting of nn words separated by spaces. Each word consists of no more than 1010 lowercase English letters.

The sum of nn in all test cases doesn't exceed 2×1052×105.
OutputFor each test case, print a line with an integer, representing the answer, modulo 109+7109+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的个数(包含自己

  相似的两个条件:

    单词的多重集是一样的 多重集单词T

    词α 出现在S第i个词和在T中出现第i个词的索引相差不超过1。(句子中的第K单词索引为K)

      (这适用于所有单词α,只要这个词α至少两次出现在这两个句子

思路:

  计算一个数列

  这个数列记录前 i 个单词组成的相似字符串的个数

  如果有相同单词在相邻位置,则交换后不产生影响;
  如果相同的单词交换,产生前两种之和

  一直到所有完成。

代码:

  数字太大注意取模

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>())

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=2e5+10;
const int mod=1e9+7;

int sum[100005] = {0};
string s[100005]; 
int main()
{
	int T = 0;
	int n = 0;
	cin >> T;
	while(T--)
	{
		cin >> n;
		memset(sum , 0 , sizeof(sum));
		sum[0] = sum[1] = 1;
		for(int i = 1;i<=n;i++)
		{
			cin >> s[i];
		}
		for(int i = 2;i<=n;i++)
		{
			if(s[i] == s[i-1])
			{
				sum[i] = sum[i-1];
			}
			else
			{
				sum[i] = (sum[i-2] + sum[i-1]);
				sum[i] %= mod;
			}
		}
		cout << sum[n] <<endl;
	}
	return 0;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值