终于等到放寒假啦!寒假期间几乎每个工作日都有一些小练习,来恢复一下做题的感觉(bushi),博客基本上每天都会更新前一天的做题情况,维持一下自己“菜鸟”的水平。
A题
You are given nn integers a1,a2,…,an. You choose any subset of the given numbers (possibly, none or all numbers) and negate these numbers (i. e. change x→(−x)). What is the maximum number of different values in the array you can achieve?
Input
The first line of input contains one integer t (1≤t≤100): the number of test cases.
The next lines contain the description of the t test cases, two lines per a test case.
In the first line you are given one integer n (1≤n≤100): the number of integers in the array.
The second line contains nn integers a1,a2,…,an (−100≤ai≤100).
Output
For each test case, print one integer: the maximum number of different elements in the array that you can achieve negating numbers in the array.
Example
input
3 4 1 1 2 2 3 1 2 3 2 0 0
output
4 3 1
Note
In the first example we can, for example, negate the first and the last numbers, achieving the array [−1,1,2,−2] with four different values.
In the second example all three numbers are already different.
In the third example negation does not change anything.
题解:
这个题还是比较简单的,大意就是说给定一串序列的数字,可以进行相反数变换,问最多有几个不同的数字。思想也很简单,统计一下每个数字(绝对值)出现的次数是否超过两次,超过两次按两次计,其余按出现的次数统计,最后输出答案就行。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int a[105],b[105];
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
memset(b,0,sizeof(b));
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[abs(a[i])]++;
}
int sum=0;
for (int i=0;i<=100;i++)
{
if (b[i]!=0)
{
if (i==0)
sum++;
else if (b[i]>=2)
sum+=2;
else
sum+=b[i];
}
}
printf("%d\n",sum);
}
return 0;
}
B题
You have a string s1s2…sn and you stand on the left of the string looking right. You want to choose an index k (1≤k≤n) and place a mirror after the k-th letter, so that what you see is s1s2…sksksk−1…s1. What is the lexicographically smallest string you can see?
A string a is lexicographically smaller than a string b if and only if one of the following holds:
- a is a prefix of b, but a≠b;
- in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first line of input contains one integer t (1≤t≤10000): the number of test cases.
The next t lines contain the description of the test cases, two lines per a test case.
In the first line you are given one integer n (1≤n≤105): the length of the string.
The second line contains the string s consisting of n lowercase English characters.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case print the lexicographically smallest string you can see.
Example
input
4 10 codeforces 9 cbacbacba 3 aaa 4 bbaa
output
cc cbaabc aa bb
Note
In the first test case choose k=1 to obtain "cc".
In the second test case choose k=3 to obtain "cbaabc".
In the third test case choose k=1 to obtain "aa".
In the fourth test case choose k=1 to obtain "bb".
题解:
这个题也是比较简单的,大意就是说有一个字符串,要求从头开始选中一串字母,做镜像,将两个子串拼起来,使得这个子串在所有子串里是最小的,输出这个子串。相当于就是寻找一个最长的、递减的字符序列(注意特殊情况:当字符串前两位一样时,直接输出前两位就行,且当n=1时也要单独输出)。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n;
scanf("%d",&n);
string s;
cin>>s;
if (n==1)
cout<<s+s<<endl;
else
{
string tmp="";
bool f=0;
for (int i=1;i<n;i++)
{
if (i==1&&s[i]==s[i-1])
{
tmp+=s[i];
tmp+=s[i];
f=1;
break;
}
else
{
if (s[i]<=s[i-1])
{
tmp+=s[i-1];
if (i==n-1&&s[i]<=s[i-1])
tmp+=s[i];
}
else
{
tmp+=s[i-1];
break;
}
}
}
string tmp2=tmp;
reverse(tmp.begin(),tmp.end());
cout<<(f==1?tmp2:tmp2+tmp)<<endl;
}
}
return 0;
}
ps:本人实力较菜,只能做出这些简单题,后面的题如果有哪位大神可以解决可以私信我,如果这里面有讲的不清楚或者有错误的,望及时指出!