C - Choosing Symbol Pairs
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that1. 1 ≤ i, j ≤ N2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.
Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.
Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.Examples
Input
great10
Output
7
Input
aaaaaaaaaa
Output
100
理解:
在一串字符串寻找符合1<=i,j<=n,且符合a[i]=a[j]的有多少个,且(i,j)和(j,i)属于两种。
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
int main()
{
map<char,int>mp;
char a[100010];
scanf("%s",a);
int L=strlen(a);
long long count=0;
for(int i=0;i<L;i++)
//计算和当前字符相同的个数
mp[a[i]]++;
for(int i=0;i<L;i++)
//当前字符和与它相同的字符(包括它本身)形成的对数
count+=mp[a[i]];
printf("%lld",count);
return 0;
}
J - Second Order Statistics
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 ≤ n ≤ 100) — amount of numbers in the sequence. The second line contains n space-separated integer numbers — elements of the sequence. These numbers don’t exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2
理解:
大概就是排好序之后找第二大的数(不是第二个数!)。数组 排序 从第二个往后找,如果比第一个大就输入,然后结束。
也可以用set,然后直接取第二个数。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int a[110];
int sum=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
int flag=0;
for(int i=2;i<=n;i++)
{
if(a[i]!=a[1])
{
printf("%d",a[i]);
flag=1;
break;
}
}
if(flag==0)
printf("NO");
return 0;
}