hdu 6863 Isomorphic Strings hash + common factor


t group input, each group of data input an integer n, representing the length of the string. Next, enter another string

You need to judge whether the string can be divided into more than 1 segment, and the minimum representation of these segments is the same

For example: abccab, which can be divided into two segments, abc and cab. They both use the minimum representation (that is, the minimum representation of dictionary order) to represent abc, so the two strings are the same



Because you need to divide the string into several segments. Suppose it is divided into x segments, then x must satisfy n%x==0


There are two methods here. The first is to enumerate the factor of n, and then judge whether this factor is OK or not

The other is: if there is a factor X that can meet the meaning of the question (that is, the length of a paragraph is x, and each paragraph is equal), then it is divided into n/x segments, and the number of each letter in each paragraph must remain the same

Then we can find the maximum common factor of the number of letters, set it as ans, and then enumerate it in 1-ans. This is less complex than the factor of de violence enumeration n


How do we judge whether a factor x can be unsatisfied?

For a string abccab, we can find that the number of letters a, b and c is 2, so the greatest common factor between them and n is 2

Then the string with length n can be divided into 2 segments at most, and the length of each segment is n/2

Then we start to enumerate the factors in the interval [1,2). First, we need to judge that n%x==0, which is not equal to 0

This enumeration is to merge several n/2, for example, 1 satisfies n%x==0

At this time, abc is a segment and cab is a segment (if x==2, then two n/2 are combined into one segment, that is, abccab is a segment. Because the topic requires that the strings must be separated, 2 is not allowed)

For the first abc segment, we first mark the hash value of abc, then mark the hash value of bca, and then mark the hash value of cab

Then judge whether the hash value of the following segment has been marked. If it has been marked, it will be fine. If it has not been marked, it will prove that this factor is not good



using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int blo=13331;
const int maxn = 5e6+5;
ll xp[maxn],hash_1[maxn],num[maxn];
void init()
    for(ll i=1; i<maxn; i++)
ll make_hash(char str[],ll hash_[],ll len)
    for(ll i=1; i<=len; i++)
        //cout<<hash_[i]<<" ";
    return len;
char str[maxn];
int main()

    ll t;
        ll n,flag=1;
        for(ll i=1;i<=n;++i)
        ll ans=n;
        for(ll i=1;i<=26;++i)  //If the minimum representation of each string is the same after grouping, the number of each letter is the same, so we can
        {           //To start with
            ans=__gcd(ans,num[i]);  //this ans What's in it is the maximum number of groups you can be divided into
        ll u=n/ans;  //According to the maximum score ans Groups, length of each group
        if(u==1 && n!=1)
            for(ll k=1;k<ans;++k)  //Enumeration judgment
                ll len=k*u;
                ll temp=hash_1[len]-hash_1[0]*xp[len];
                for(ll i=1;i<=len;++i)  //The hash values of various types of strings of a string are calculated and marked
                { //For example, string abc,You don't just have to figure it out abc And calculate the hash value of bca,cab Hash value of
                for(ll j = 1; j * len <= n; j++)  //Determine whether the latter groups are the same as the first group

                    if(r[hash_1[j*len]-hash_1[(j - 1)*len]*xp[len]]==0)
                        flag = 1;
                if (flag == 0)

        else printf("No\n");
    return 0;


Tags: Hash Algorithm

Posted by colinexl on Mon, 23 May 2022 13:40:28 +0300