B1029/A1084 20’ 20min 散列 medium
- ASCII码的大小就是128 所以hash的长度为128
- hash[c1] c1是char,而hash的[]中只能放数字 所以c1以%d传入
- 子串找到头了还没找到相同的 说明这个是坏的键盘
- 避免重复输出 所以判断是否为false 输出一次就置为true 不能再输出了
#include <iostream>
#include <string>
using namespace std;
void test()
{
string a;
string b;
cin >> a >> b;
int lena = a.length();
int lenb = b.length();
bool hash[128] = { false };
for (int i = 0; i < lena; ++i)
{
int j;
char c1, c2;
for (j = 0; j < lenb; ++j)
{
c1 = a[i];
c2 = b[j];
if (c1 <= 'z'&&c1 >= 'a')
c1 -= 32;
if (c2 <= 'z'&&c2 >= 'a')
c2 -= 32;
if (c1 == c2)
break;
}
if (j == lenb && hash[c1] == false)
{
cout << c1;
hash[c1] = true;
}
}
}
int main()
{
test();
system("pause");
return 0;
}
B1033 20’ 70min 散列 medium
- 利用find函数完成对字符串的查找操作
- 查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是strA.find(strB) != string::npos
- 其中string:npos是个特殊值,说明查找没有匹配
- 对int或者bool的数组初始化注意事项
- 如果不赋值 里面存的乱码
- 如果
bool hash[256]={true};
只有第一个为true/1剩下的都是false/0
- 如果
bool hash[256]={false};
全部为false / 0
- cin与getline的使用
- 本题需要输入两个字符串 有种情况是 第一个字符串不输入
- 如果用cin会一直等待输入2个字符串 即使按回车也无法跳过
- 如果同getline会识别换行符 按了换行就执行下一条命令 使得是空字符串的a通过
#include <iostream>
#include <string>
using namespace std;
const int maxs = 100010;
void test()
{
bool hash[256];
memset(hash,true,sizeof(hash));
string a, b;
getline(cin,a);
getline(cin,b);
int lena = a.length();
int lenb = b.length();
for (int i = 0; i < lena; ++i)
{
if (a[i] >= 'A'&&a[i] <= 'Z')
a[i] = a[i] + 32;
hash[a[i]] = false;
}
for (int j = 0; j < lenb; ++j)
{
if (b[j] >= 'A'&&b[j] <= 'Z')
{
if (hash[b[j] + 32] == true && hash['+'] == true)
{
printf("%c",b[j]);
}
}
else if (hash[b[j]] == true)
{
printf("%c", b[j]);
}
}
printf("\n");
}
int main()
{
test();
system("pause");
return 0;
}
B1038 20 散列 easy
#include <iostream>
using namespace std;
int a[100010];
void test()
{
memset(a,0,sizeof(a));
int n;
cin >> n;
while (n--)
{
int id;
scanf("%d", &id);
a[id]++;
}
int m;
cin >> m;
int idd;
cin >> idd;
cout << a[idd];
while(--m)
{
int idd;
scanf("%d", &idd);
printf(" %d",a[idd]);
}
}
int main()
{
test();
system("pause");
return 0;
}