哈希
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
import javax.swing.text.GapContent;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
char x[] = sc.readLine().toCharArray();
HashSet<Integer> set = new HashSet<Integer>();
String y = sc.readLine();
int a[] = new int[26];
int b[] = new int[26];
int xx[] = new int[1000010];
int yy[] = new int[1000010];
for (int i = 0; i < x.length; i++)
{
a[(x[i] - 'a')]++;
b[(y.charAt(i) - 'a')]++;
if (i == 0)
{
yy[0] = 1;
} else
{
xx[i] = xx[i - 1] * 131 + (y.charAt(i) - 'a');
yy[i] = yy[i - 1] * 131;
}
}
int len = x.length;
int ylen = y.length();
for (int i = len; i < ylen; i++)
{
b[y.charAt(i - x.length) - 'a']--;
b[y.charAt(i) - 'a']++;
xx[i] = xx[i - 1] * 131 + (y.charAt(i) - 'a');
yy[i] = yy[i - 1] * 131;
int gg = 0;
for (int j = 0; j < 26; j++)
{
if (a[j] == b[j])
{
gg++;
} else
{
break;
}
}
if (gg == 26)
{
set.add(xx[i] - xx[i - x.length] * yy[(i) - (i - x.length)]);
}
}
System.out.println(set.size());
}
}
在这里插入代码片