zoukankan      html  css  js  c++  java
  • E

    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;
    }
    

      

  • 相关阅读:
    python socket文件传输实现
    python 进程与线程(理论部分)
    python函数-基础篇
    python变量、注释、程序交互、格式化输入、基本运算符
    python基础数据篇
    python基础之从认识python到python的使用
    判断素数
    辗转相除法
    你了解gets()和scanf()吗
    密码破译
  • 原文地址:https://www.cnblogs.com/SutsuharaYuki/p/13429609.html
Copyright © 2011-2022 走看看