17.Hidden Anagrams

Description

An anagram is a word or a phrase that is formed by rearranging the letters of another. For instance, by rearranging the letters of “William Shakespeare,” we can have its anagrams “I am a weakish speller,” “I’ll make a wise phrase,” and so on. Note that when A is an anagram of B, B is an anagram of A. In the above examples, differences in letter cases are ignored, and word spaces and punctuation symbols are freely inserted and/or removed. These rules are common but not applied here; only exact matching of the letters is considered. For two strings s1 and s2 of letters, if a substring s 0 1 of s1 is an anagram of a substring s 0 2 of s2, we call s 0 1 a hidden anagram of the two strings, s1 and s2. Of course, s 0 2 is also a hidden anagram of them. Your task is to write a program that, for given two strings, computes the length of the longest hidden anagrams of them. Suppose, for instance, that “anagram” and “grandmother” are given. Their substrings “nagr” and “gran” are hidden anagrams since by moving letters you can have one from the other. They are the longest since any substrings of “grandmother” of lengths five or more must contain “d” or “o” that “anagram” does not. In this case, therefore, the length of the longest hidden anagrams is four. Note that a substring must be a sequence of letters occurring consecutively in the original string and so “nagrm” and “granm” are not hidden anagrams.

Input

The input consists of a single test case in two lines.
s1
s2
s1 and s2 are strings consisting of lowercase letters (a through z) and their lengths are between 1 and 4000, inclusive

Output

Output the length of the longest hidden anagrams of s1 and s2. If there are no hidden anagrams, print a zero.

Sample Input

anagram
grandmother

Sample Output

4

题意:

给出两个字符串,找出能使得s1,s2重组之后能够相同的最大子串

思路:

统计子串每个字母出现次数,得出hash值用map判重

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5005;
const int INF=0x3f3f3f3f;
char s[maxn],t[maxn];
int a[30];
ll gethash()
{
    ll sum=INF;
    for (int i=0; i<26; i++)
    {
        sum*=10;
        sum+=a[i];
    }
    return sum;
}

int main()
{
    map<ll,int> sh;
    scanf("%s %s",s,t);
    int len1=strlen(s);
    int len2=strlen(t);
    int len=min(len1,len2);
    int ans=0;
    int i;
    for(i=1; i<=len; i++)
    {
        sh.clear();
        memset(a,0,sizeof(a));

        for (int j=0; j<i; j++)
            a[s[j]-'a']++;
        for (int j=0; j<len1-i; j++)
        {
            sh[gethash()]++;
            a[s[j]-'a']--;
            a[s[j+i]-'a']++;
        }
        sh[gethash()]++;
//        printf("%d\n",gethash());
        memset(a,0,sizeof(a));
        for (int j=0; j<i; j++)
            a[t[j]-'a']++;
        for (int j=0; j<len2-i; j++)
        {
            if (sh[gethash()])
                ans=max(ans,i);
            a[t[j]-'a']--;
            a[t[j+i]-'a']++;
        }

        if (sh[gethash()])
            ans=max(ans,i);
//       printf("%d\n",ans);
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值