题目
A - Happy Birthday, Polycarp!
题意
给定 n ,求1 ~ n 之间只有一个数字组成数,比如11,2222,3333等类似的数,求有多少这样数。
思路
枚举
,枚举每个数即可。
代码
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t;
cin >> t;
while(t -- )
{
int n;
cin >> n;
if(n < 10) cout << n << endl;
else
{
int sum = 0, x = 11, s = 0, p = 11;
sum += 9;
while(x <= n)
{
x = ++ s * p;
if(x > n) break;
if(s == 9)
{
x = p * 10 + 1;
p = x;
s = 0;
}
sum ++;
}
cout << sum << endl;
}
}
return 0;
}
B - Make Them Odd
题意
给定一个数组,可以进行将数组中的偶数除以2的操作,问让数组里都为奇数所操作的最小次数
思路
优先队列 + 哈希
,将所有的数存到优先队列和哈希表中,从队头开始,如果这个数为偶数,那么将这个数变成原来的一半,弹出,然后如果这个数没有在数组中出现过,那么就将这个数存到优先队列中,否则不用存,如果是奇数的话,就直接弹出,使答案加一。
代码
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 110;
int n;
unordered_map <int, bool> h;
priority_queue <int, vector <int>, less <int> > heap;
int main()
{
int T;
cin >> T;
while(T -- )
{
h.clear();
cin >> n;
for(int i = 1; i <= n; i ++)
{
int x;
cin >> x;
if(!h[x]) heap.push(x);
h[x] = true;
}
int ans = 0;
while(heap.size())
{
int t = heap.top();
heap.pop();
if(t % 2 == 0)
{
ans ++;
t /= 2;
if(!h[t]) heap.push(t);
}
}
cout << ans << endl;
}
return 0;
}
C - As Simple as One and Two
题意
Polycarp 不喜欢 “one” 和 “two” 这两个字符串,给定一个字符串,可以删除字符串中的任意一个位置的字符,问最少删除多少次,使得字符串中没有“one” “two” 这两个字符串。
思路
当碰到这两个字符串中的一个的时候,就将中间的字符删掉,然后再特判一下“twone”这种情况即可。
代码
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 12;
string s;
int cal(int i)
{
string st, sta;
st += s[i - 2];
st += s[i - 1];
st += s[i];
if(i + 2 < s.size())
{
sta += s[i + 1];
sta += s[i + 2];
}
string ss = st + sta;
if(ss == "twone")
return 1;
else
if(st == "one" || st == "two") return 2;
else
return 0;
}
int main()
{
int T;
cin >> T;
while(T -- )
{
int k = 0, ans[N] = {0};
cin >> s;
int n = s.size();
for(int i = 2; i < n; i ++)
{
int t = cal(i);
if(t == 1)
{
ans[k ++] = i;
s[i] = '-';
}
else if(t == 2)
{
ans[k ++] = i - 1;
}
}
if(!k) puts("0\n");
else
{
cout << k << endl;
for(int i = 0; i < k; i ++)
{
cout << ans[i] + 1<< ' ';
}
puts("");
}
}
return 0;
}
总结
其实C题想的还是有点复杂一开始,没有想象的那么难,走了一些弯路,最主要的是寻找一下里面隐藏的一些性质,审题还是关键!!