poj 1200 Crazy Search

简介:

   给字母编码后,用NC当进制进行hash,或者直接java水过

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define INF 1E9
using namespace std;
int N,NC;
char org[1000000];
int hash[10000000];
int v[200];
int find()
{
    int i=0,j=0,now=0,hh,ans=0,len=strlen(org);
    for(i=0,j=0;i<len;i++)
    {
        if(v[org[i]]==0)v[org[i]]=++j;
        if(j==NC)break;
    }
    for(i=0;i<N;i++) now=now*NC+v[org[i]];
    ans++;hash[now]=1;
    int t=pow(NC,N-1);
    for(j=0,i=N;i<len;i++,j++)
    {
        now-=t*v[org[j]];
        now=now*NC+v[org[i]];
        if(hash[now]!=0)continue;
        hash[now]=1;ans++;
    }
    return ans;
}
int main()
{
    scanf("%d%d%s",&N,&NC,org);
    printf("%d\n",find());
    return 0;
}

import java.util.HashMap;
import java.util.Scanner;


public class Main {
	private static Scanner in;

	public static void main(String[] args) {
		HashMap<String, Boolean> vis=new HashMap<String,Boolean>();
		in = new Scanner(System.in);
		@SuppressWarnings("unused")
		int N=in.nextInt(),NC=in.nextInt();
		String s=in.next(),t;
		int i,j,len=s.length(),ans=0;
	    for(i=N,j=0;i<=len;i++,j++)
	    {
	    
	    	t=s.substring(j, i);
	        if(vis.containsKey(t)==true) continue;
	        vis.put(t, true);
	        ans++;
	    }
	    System.out.println(ans);
	}

}


 

import java.util.HashMap;
import java.util.Scanner;


public class Main {
	private static Scanner in;

	public static void main(String[] args) {
		HashMap<String, Boolean> vis=new HashMap<String,Boolean>();
		in = new Scanner(System.in);
		@SuppressWarnings("unused")
		int N=in.nextInt(),NC=in.nextInt();
		String s=in.next(),t;
		int i,j,len=s.length(),ans=0;
	    for(i=N,j=0;i<=len;i++,j++)
	    {
	    
	    	t=s.substring(j, i);
	        if(vis.containsKey(t)==true) continue;
	        vis.put(t, true);
	        ans++;
	    }
	    System.out.println(ans);
	}

}


 

 

目录
相关文章
|
7月前
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
65 1
|
29天前
|
机器学习/深度学习 安全 Java
hdu-1596-find the safest road(dijkstra)
hdu-1596-find the safest road(dijkstra)
16 0
|
8月前
poj 2362 hdoj 1518 Square(搜索)
不难了解,小棒子的长度越长,其灵活性越差。例如长度为5的一根棒子的组合方式要比5根长度为1的棒子的组合方式少,这就是灵活性的体现。
31 0
|
10月前
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
|
索引
LeetCode 79. Word Search
给定一个2D板和一个单词,找出该单词是否存在于网格中。 该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元格不得多次使用。
54 0
LeetCode 79. Word Search
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
60 0
LeetCode 212. Word Search II
|
算法
LeetCode 240. Search a 2D Matrix II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。
61 0
LeetCode 240. Search a 2D Matrix II
|
算法
LeetCode 74. Search a 2D Matrix
编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.
59 0
LeetCode 74. Search a 2D Matrix