[๋ฐฑ์ค€]15829.Hashing/Java

seeunยท2021๋…„ 8์›” 22์ผ
0

BaekJoon

๋ชฉ๋ก ๋ณด๊ธฐ
6/10
post-thumbnail
post-custom-banner

๐Ÿ“ƒHashing ๋งํฌ


๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปํ’€์ด

public class Hashing_15829 {
	static final int n = 31;
	static final int mod = 1234567891;
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int len = scan.nextInt();
		String s = scan.next();
		long pow = 1;
		long  answer = 0; 
		
		for(int i = 0; i < len; i++) {
			answer += (s.charAt(i) - 'a' + 1) * (pow % mod) ;
			pow *= n;
		}
		System.out.println(answer % mod);
	}
}

์ฒ˜์Œ๋ณด๋Š” 50์  ใ…‹ใ…‹ใ…‹ใ…‹ ๋ฌธ์ž์—ด ๊ธธ์ด๋ณ„๋กœ ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ œ์˜€๋‹ค ๋ฌธ์ œ ๊ผผ๊ผผํžˆ ์ฝ๊ณ  ํ’€์žใ…œใ…œ
๐Ÿ“Ž ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ์ตœ๋Œ€ 50์ด๋ฏ€๋กœ 31์˜ 50์ œ๊ณฑ์ด ๋ณ€์ˆ˜ pow์— ์ €์žฅ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋ฌด๋ ค 3.6990030696076007e+74๋ผ๋Š” ์ˆซ์ž์˜€๋‹ค long ๋ฒ”์œ„๋ฅผ ํ›จ์”ฌ ์ดˆ๊ณผํ–ˆ๋˜ ๊ฒƒ๐Ÿ™‰๐Ÿ™‰
mod ์—ฐ์‚ฐ์„ ๋จผ์ € ํ•ด์ฃผ๊ณ  ๋ฒ”์œ„ ์•ˆ์— ๋“ค์–ด์˜ค๋„๋ก ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค


public class Hashing_15829 {
	static final int n = 31;
	static final int mod = 1234567891;
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int len = scan.nextInt();
		String s = scan.next();
		long pow = 1;
		long  answer = 0; 
		
		for(int i = 0; i < len; i++) {
			answer += (s.charAt(i) - 'a' + 1) * pow ;
			pow = (pow *= n) % mod;
		}
		System.out.println(answer % mod);
	}
}
profile
๐Ÿคนโ€โ™‚๏ธ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€