智算之道C 字符串 JAVA

哈希

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());

		}

	
}

在这里插入代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值