1. 程式人生 > 實用技巧 >E - Equal Sentences

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 sentencesSSandTTare almost-equal if the two conditions holds:

1. The multiset of the words inSSis the same as the multiset of the words inTT.
2. For a wordαα, itsithithoccurrence inSSand itsithithoccurrence inTThave indexes differing no more than
11. (Thekthkthword in the sentence has indexkk.) This holds for allααandii, as long as the wordααappears at leastiitimes in both sentences.

Please notice that "almost-equal" is not a equivalence relation, unlike its name. That is, if sentencesAAandBBare almost-equal,BBandCCare almost-equal, it is possible that
AAandCCare not almost-equal.

Zhang3 has a sentenceSSconsisting ofnnwords. She wants to know how many different sentences there are, which are almost-equal toSS, includingSSitself. Two sentences are considered different, if and only if there is a numberiisuch that theithithword in the two sentences are different. As the answer can be very large, please help her calculate the answer modulo109+7109+7.

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

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

The second line contains the sentenceSSconsisting ofnnwords separated by spaces. Each word consists of no more than1010lowercase English letters.

The sum ofnnin all test cases doesn't exceed2×1052×105.
OutputFor each test case, print a line with an integer, representing the answer, modulo109+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;
}