• zjnuSAVEZ (字符串hash)


    Description


    There are eight planets and one planetoid in the Solar system. It is not a well known fact that there is a secret planet S4 inhabited by small creatures similar to bears, their codename being Lodas. Although this fact is well hidden from the public, the association Savez sent a team lead by general Henrik to study the Lodas. It has been discovered that Lodas have the ability of teleportation and he wants to hire them in his army. One Lod consists of N strings where the ith string is denoted by xi . Research has shown that the number of teleportations a Loda can make depends on one special subsequence (not necessarily consecutive) of these strings. Strings xi and xj (i < j) can both be in that sequence if and only if string xj both starts with and ends with string xi . The number of teleportations a Loda can make is the length of the longest described subsequence. Determine the number of teleportations.


    Input


    The first line of input contains of the integer N, the number of strings. Each of the following N lines contains one string consisting of uppercase letters of the English alphabet. The input data will be such that there will be less than two million characters in total.


    Output


    The first and only line of output must contain the number of teleportations a Loda can make.


    Sample Input




    5
    A
    B
    AA
    BBB
    AAA
    5
    A
    ABA
    BBB
    ABABA
    AAAAAB
    6
    A
    B
    A
    B
    A
    B
    Sample Output






    3

    题意:给你几个字符串,让你找到最长不下降子序列,使得前一个字符串是后一个字符串的开头和结尾。

    思路:用字符串hash做。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    #define inf 99999999
    #define pi acos(-1.0)
    #define MOD 1000000007
    #define maxn 2000050
    #define mod1 31
    #define mod2 1000000007
    ll md[maxn];
    map<ll,int>mp;
    map<ll,int>::iterator it;
    
    
    void init()
    {
        int i;
        md[0]=1;
        for(i=1;i<=2000000;i++){
            md[i]=(md[i-1]*mod1)%mod2;
        }
    }
    char s[2000060];
    
    
    
    
    int main()
    {
        int n,m,i,j,len;
        init();
        while(scanf("%d",&n)!=EOF)
        {
            if(n==0){
                printf("0
    ");continue;
            }
            mp.clear();
            int ans=1;
            for(i=1;i<=n;i++){
                scanf("%s",s);
                len=strlen(s);
                int now=0;
                ll num1=0,num2=0;
                for(j=0;j<len;j++){
                    num1=(num1+md[j]*(s[j]-'A'+1) )%mod2;
                    num2=(num2*mod1+(s[len-1-j]-'A'+1))%mod2; //这里是关键
    
                    if(num1==num2){
                        now=max(now,mp[num1]);
                    }
                }
                ll num=0;
                for(j=0;j<len;j++){
                    num=(num+(s[j]-'A'+1)*md[j])%mod2;
                }
                mp[num]=max(mp[num],now+1);
                ans=max(ans,now+1);
    
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    


  • 相关阅读:
    MathType输入框怎么调整
    几何画板中去除画出的线段的教程
    MathType怎么编辑半开半闭区间
    几何画板给月牙图形填充颜色的技巧
    MathType调整矩阵分隔线粗细的方法
    帮你深入理解OAuth2.0协议
    phalapi
    Spring松耦合实例
    让前端工程师糟心的正则表达式到底是什么?
    composer安装
  • 原文地址:https://www.cnblogs.com/herumw/p/9464527.html
Copyright © 2020-2023  润新知