P8630 [蓝桥杯 2015 国 B] 密文搜索
题目描述
福尔摩斯从 X 星收到一份资料,全部是小写字母组成。
他的助手提供了另一份资料:许多长度为 88 的密码列表。
福尔摩斯发现,这些密码是被打乱后隐藏在先前那份资料中的。
请你编写一个程序,从第一份资料中搜索可能隐藏密码的位置。要考虑密码的所有排列可能性。
输入格式
输入第一行:一个字符串 s,全部由小写字母组成,长度小于 1024×1024。
紧接着一行是一个整数 n, 表示以下有 n 行密码,1≤n≤1000。
紧接着是 n 行字符串,都是小写字母组成,长度都为 88。
输出格式
一个整数,表示每行密码的所有排列在 s 中匹配次数的总和。
输入输出样例
输入 #1
aaaabbbbaabbcccc
2
aaaabbbb
abcabccc
输出 #1
4
说明/提示
第一个密码匹配了 33 次,第二个密码匹配了 11 次,一共 44 次。
时限 3 秒, 512M。蓝桥杯 2015 年第六届国赛
#include<stdio.h>
#include<string.h>
long long int hash(long long int l,char s[])
{
long long int sum=0;
for(int i=l;i<l+8;i++)
sum+=(s[i])*(s[i])*(s[i]);
return sum;
}
int main()
{
char str[1050000],str1[1005][10];
long long int i,j,k=0,len,n,num[1005]={0};
scanf("%s",str);
len=strlen(str);
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%s",str1[i]);
for(j=0;j<8;j++)
num[i]+=(str1[i][j])*(str1[i][j])*(str1[i][j]);
}
for(i=0;i<=len-8;i++)
{
long long int sum=hash(i,str);
for(j=1;j<=n;j++)
if(sum==num[j])
k++;
}
printf("%lld",k);
}
哈希的简单应用,把s字符串每八个字符合成哈希值,再进行比较是否相等。开始我只是把字符的ascll码相加作为哈希值,但是只有20分。这样的话错误率是非常高的,存在很大的偶然性。ascll码的平方也还是只有20分,然后我坚持不懈的试了ascll码的三次方,就麻溜的过啦。
A - 字符串问题
给定一个字符串s,和一个字符c,问你是否能通过以下操作将s变成c:
将s中连续的两个字符删除
"lemma" 通过一次操作你可以把它变成:"mma", "lma", "lea" or "lem" In
Input
第一行给定一个t(1<=t<=1e3)表示t组样例
对于每组样例给定s,和c,保证字符串s的长度为1-49之间的奇数,c为单个字符
The first line of input data contains an integer t (1≤t≤10^3) — the number of input test cases.
The descriptions of the t cases follow. Each test case is represented by two lines:
string s, which has an odd length from 11 to 4949 inclusive and consists of lowercase letters of the Latin alphabet;
是一个包含一个字母的字符串c哪里c是拉丁字母的小写字母。
Output
如果能通过操作s1多次(可能为0次)变成s2输出"YES",否则输出"NO"
For each test case in a separate line output:
YES, if the string s can be converted so thats=c is true;
NO otherwise.
You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).
Sample 1
Inputcopy | Outputcopy |
5 abcde c abcde b x y aaaaaaaaaaaaaaa a contest t | YES NO NO YES YES |
Note
In the first test case, s="abcde". You need to get s="c". For the first operation, delete the first two letters, we get s="cde". In the second operation, we delete the last two letters, so we get the expected value of s="c".
In the third test case, s="x", it is required to get s="y". Obviously, this cannot be done.
#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char s[55],c;
scanf("%s %c",s,&c);
int flag=0;
for(int i=0,len=strlen(s);i<len;i++)
{
if(s[i]==c&&i%2==0)
{flag=1;break;}
}
if(flag==0)
printf("NO\n");
else
printf("YES\n");
}
}
只需要判断是否存在s[i]=c并且在偶数位置就可以了。