zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    B/S学习总结
    鶴山 李正浩 博士(探访墓所)
    毕业工作五年的总结和感悟(上)
    NSURLConnection经常使用的代理方法
    14.2 事务的ACID属性
    Search a 2D Matrix -- LeetCode
    ios开发之-继承的实现运用
    【Android】读取sdcard上的图片
    BZOJ 2820 YY的GCD 莫比乌斯反演
    linux写环境变量对字符转义
  • 原文地址:https://www.cnblogs.com/herumw/p/9464527.html
Copyright © 2011-2022 走看看