Aizu 1370 Hidden Anagrams 字符串哈希
来源:互联网 发布:java 泛型和通配符 编辑:程序博客网 时间:2024/06/08 01:49
Hidden Anagrams
Time Limit : 10 sec, Memory Limit : 262144 KBProblem D Hidden Anagrams
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
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
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.
Output
Output the length of the longest hidden anagrams of
Sample Input 1
anagramgrandmother
Sample Output 1
4
Sample Input 2
williamshakespeareiamaweakishspeller
Sample Output 2
18
Sample Input 3
aaaaaaaabbbbbbbbxxxxxabababxxxxxabab
Sample Output 3
6
Sample Input 4
abababacdcdcdefefefghghghghgh
Sample Output 4
0
给两个长度最多4000的字符串,判断它们的所有子串中,每个字母出现次数相同的子串的最大长度。
从刘大爷那里学到了一个奇技淫巧,同时也十分玄学的算法:字符串哈希。
对于这题,统计子串中每个字母出现的次数,算出哈希函数之后用map判重即可。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef unsigned long long ull;typedef long double ld;const int maxn=5005,inf=0x3f3f3f3f;char s[maxn],t[maxn];int a[26];ull seed1=26,seed2=1e9+7;ull hash() {ull sum=seed1;for (int i=0;i<26;i++) {sum*=seed2;sum+=a[i];}return sum;}int main() {map<ull,int> HashTable;scanf("%s",s);scanf("%s",t);int lena,lenb,i,j,k,ans;lena=strlen(s),lenb=strlen(t);int len=min(lena,lenb);ans=0;for (i=1;i<=len;i++) {HashTable.clear();mem0(a);for (j=0;j<i;j++) a[s[j]-'a']++;for (j=0;j<lena-i;j++) {HashTable[hash()]++;a[s[j]-'a']--;a[s[j+i]-'a']++;}//cout << hash() << endl;HashTable[hash()]++;mem0(a);for (j=0;j<i;j++) a[t[j]-'a']++;for (j=0;j<lenb-i;j++) {//cout << hash() << endl;if (HashTable[hash()]) ans=max(ans,i);a[t[j]-'a']--;a[t[j+i]-'a']++;}if (HashTable[hash()]) ans=max(ans,i);}printf("%d\n",ans);return 0;}
- Aizu 1370 Hidden Anagrams 字符串哈希
- CFgym:Hidden Anagrams(Hash)
- 【字符串】Anagrams
- 字符串 Anagrams
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- 49:Anagrams【哈希】【字符串】
- leetcode:字符串之Anagrams
- LeetCode练习-字符串-anagrams
- leetcode---anagrams---字符串
- python_爬取音乐部落
- css盒模型
- 从分布式到zookeeper(1)
- 利用Vue.js来制作一个小的Demo1
- PHP动态网页设计与制作案例教程pdf
- Aizu 1370 Hidden Anagrams 字符串哈希
- java三大特征之封装
- poj3274Gold Balanced Lineup(哈希表)
- POJ 3078 Q 笔记
- poj3274Gold Balanced Lineup(哈希表)
- Hadoop
- Android APP开发警告大全
- leetcode 移动零,数组反转
- php生成二维码