您的位置:首页 > 其它

poj1200 2010.2.20

2016-02-05 14:29 197 查看
poj1200 2010.2.20

nc进制+hash

首先,这个题说有nc个字母,但出现的字母并没有什么规律,如'a'、'b'、'c'....(可能出现有c却没有b的情况)因此给这些字母编号,不能简单的用" -'a' "直接得到,因此,怎么给这些字母编号成了一个问题,呵呵,这一点是看网上的

#include<stdlib.h>
//这题将字符串转换为一个nc进制的数,判断这个
//数是否重复出现即可
#include<memory.h>
#include<stdio.h>
#include<iostream>
using namespace std;
#define MAX 20000000
bool c[MAX];
char x[MAX];
//将字母转换为对应的数字,因为字母可能不是连续的
//如nc = 3时可能会有aeccc这种字符串
//处理后a,c,e分别代表0,1,2
int ascii[256];
int main()
{
int n,nc;
while(scanf("%d%d\n",&n,&nc)!=EOF)
{
memset(c,0,sizeof(c));
memset(ascii,0,sizeof(ascii));
int tmp,res(0);
scanf("%s",x);
int len = strlen(x);
//将出现过的字母都标记为-1(true)
for(int i = 0; i < len; i++)
ascii[x[i]] = -1;
int zz = 0;
//为出现过的字母分配数字
for(int i = 0; i < 256; i++)
if(ascii[i])
ascii[i] = zz++;
bool flag;
len = len - n + 1;
for(int i = 0; i < len; i++) {
flag = true;
tmp = 0;
//将子字符串转换为nc进制数
for(int j = 0; j < n; j++)
tmp = tmp*nc + ascii[x[i+j]];
tmp = abs(tmp)%MAX;
if(!c[tmp]){
res++;
c[tmp] = true;
}
}
printf("%d\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: