链接:https://ac.nowcoder.com/acm/problem/21297
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
给你一个整数n表示手机号码的位数
再给你m个字符串表示保留的号码,比如911 110 120等
问你一共有多少的手机号码不以保留号码开头
输入描述:
第一行输入两个整数n, m (1 ≤ n ≤ 17, 0 ≤ m ≤ 50)
接下来m行每行输入一个数字串,长度为1到n
输出描述:
输出一个整数
示例1
输入
7 3
0
1
911
输出
7990000
示例2
输入
10 3
0
1
911
输出
7990000000
示例3
输入
8 3
1
12
123
输出
90000000
示例4
输入
9 3
12
13
14
输出
970000000
示例5
输入
3 1
411
输出
999
备注:
子任务1: n <= 8
子任务2: n <= 10
子任务3: 无限制
知识点:枚举和暴力
注意点:判断字符串的前缀关系
技巧:做减法而不是做加法,做加法需要分类讨论很可能考虑不全面,考虑起来也比较乱,做减法把不满足要求都减掉即可。
#include <bits/stdc++.h>
using namespace std;
vector<string> str;
typedef long long ll;
ll power(int x)
{
ll res = 1;
for (int i = 0; i < x; i++)
{
res *= 10;
}
return res;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++)
{
string s;
cin >> s;
str.push_back(s);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
if (i != j && str[i].find(str[j]) == 0)
{
str[i] = "none";
}
}
}
long long ans = power(n);
for (int i = 0; i < m; i++)
{
if (str[i] != "none")
ans -= power(n - str[i].length());
}
printf("%lld\n", ans);
//system("pause");
}