新生训练赛001题解

C - Choosing Symbol Pairs CodeForces - 50B

题目

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 that

  1. 1 ≤ i, j ≤ N

  2. 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

题意

找到不同的pair对数,(1,3)和(3,1)算两组,pair是两个字母相同

思路

求出每个字母的个数,由于自己和自己也算一组,所以直接用当前字母的个数乘以个数就行了。
举个例子aaa ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) (1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3) (1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3)这样就是匹配了 3 ∗ 3 3*3 33

代码

#include <bits/stdc++.h>
using namespace std;
map<char,long long>mp;
int main()
{
    string s;
    cin>>s;
    int len = s.size();
    for(int i=0;i<len;i++)
    {
        mp[s[i]]++;
    }
    long long ans=0;
    for(int i=0;i<len;i++)
    {
        if(mp[s[i]])
        {
            ans+=mp[s[i]]*mp[s[i]];
            mp[s[i]]=0;
        }
    }
    printf("%lld",ans);
}

D - Sum CodeForces - 49B

题目

Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying a + b = ?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base p and regard the expression as written in the base p positional notation. Vasya understood that he can get different results with different bases, and some bases are even invalid. For example, expression 78 + 87 in the base 16 positional notation is equal to FF16, in the base 15 positional notation it is equal to 11015, in the base 10 one — to 16510, in the base 9 one — to 1769, and in the base 8 or lesser-based positional notations the expression is invalid as all the numbers should be strictly less than the positional notation base. Vasya got interested in what is the length of the longest possible expression value. Help him to find this length.

The length of a number should be understood as the number of numeric characters in it. For example, the length of the longest answer for 78 + 87 = ? is 3. It is calculated like that in the base 15 (11015), base 10 (16510), base 9 (1769) positional notations, for example, and in some other ones.

Input
The first letter contains two space-separated numbers a and b (1 ≤ a, b ≤ 1000) which represent the given summands.

Output
Print a single number — the length of the longest answer.

Examples
Input
78 87
Output
3
Input
1 1
Output
2

题意

给你两个数字,从第一个比数字里每一位中最大的数大的进制开始,到16进制,在当前的进制下进行加法,求出结果以后求长度的最大值。

思路

用java 大数直接转进制即可

代码

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		int a = cin.nextInt();
		int b = cin.nextInt();
		int n = a, m = b;
	    int maxx = 0;
	    while(a!=0)
	    {
	        maxx = Math.max(maxx,a%10);
	        a/=10;
	    }
	    while(b!=0)
	    {
	        maxx = Math.max(maxx,b%10);
	        b/=10;
	    }
//	    System.out.println(maxx);
	    int ans = 0;
	    for(int i=maxx+1;i<=16;i++)
	    {
	    	String nn = Integer.toString(n);
	    	String mm = Integer.toString(m);
	        BigInteger s1 = new BigInteger(nn,i);
	        BigInteger s2 = new BigInteger(mm,i);
//	        System.out.println(s1 + " " + s2 + " "+s1.add(s2).toString(i));
	        ans = Math.max(ans, s1.add(s2).toString(i).length());
	    }
	    System.out.println(ans);
	}
}

G - You’re Given a String… CodeForces - 23A

题目

You’re given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).

Input
The first input line contains the string. It’s guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn’t exceed 100.

Output
Output one number — length of the longest substring that can be met in the string at least twice.

Examples
Input
abcd
Output
0
Input
ababa
Output
3
Input
zzz
Output
2

题意

求主串中出现过两次的串的最大长度

思路

我比赛的时候看了一眼n<=100 直接写了一发n^3大暴力,一发过。
之后想用kmp做,写了一发wa4 想了一会儿发现我这样只能算从头开始的
所以一顿乱搞把每一个子串找了一遍,然后每次跑一次kmp,
时间复杂度大概是n^2/4.

在这里插入图片描述

代码

大暴力:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000+10;
string ans[maxn];
map<string,long long>mp;
int main()
{
    string s;
    cin>>s;
    int len = s.size();
    int cnt=0;
    for(int i=1;i<=len;i++)
    {
        for(int j=0;j<len;j++)
        {
            string now="";
            for(int o=j;o<min(len,j+i);o++)
            {
                if(len-j>=i)
                now+=s[o];
            }
            if(now.size())
            ans[cnt++]=now,mp[now]++;
        }
    }
    // cout<<cnt<<endl;
    int maxx = 0;
    for(int i=0;i<cnt;i++)
    if(mp[ans[i]]>1)
    {
        int len = ans[i].size();
        maxx = max(maxx,len);
    }
    printf("%d",maxx);
}

kmp:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
char s[maxn],b[maxn];
int next1[maxn];
int main()
{
    scanf("%s",s+1);
    int l = strlen(s+1);
    int maxx = 0;
    int L = l;
    for(int k=1;k<=l;k++)
    {
        int len = 1;
        for(int j=k;j<=k+L;j++)
        b[len++]=s[j];
        L--;
        len--;len--;
        int j=0;
        // printf("%s %d\n",b+1,len);
        for(int i=2;i<=len;i++)
        {
            while(j && b[i]!=b[j+1])
            j=next1[j];
            if(b[i]==b[j+1])
            j++;
            next1[i]=j;
        }
        // for(int i=1;i<=len;i++)
        // printf("%d ",next1[i]);
        for(int i=1;i<=len;i++)
        maxx=max(maxx,next1[i]);
    }
    printf("%d\n",maxx);
}

H - Party CodeForces - 23B

题目

n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, …, n - 1 friends among those who stayed by the moment of their leaving, did the same.

What is the maximum amount of people that could stay at the party in the end?

Input
The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).

Output
For each test output in a separate line one number — the maximum amount of people that could stay in the end.

Examples
Input
1
3
Output
1

题意

n个人参加派对,没有朋友的先走了,然后是1个朋友的 ,然后两个朋友的

思路

多画几个图就会发现,三个的时候先让一个人认识两个,剩下两个认识一个,然后两个只认识一个的人走了,剩下的那个人就谁也不认识,根据题意也不会走了。也就是每一个n都让其他人都互相认识,但是这两个人互相不认识。刚开始一定是这两个人走(因为他们认识的人最少) 然后剩下的人就会少两个认识的人,根据题意的话就不会走了,所以答案是n-2.

代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        if(n-2>=0)
        printf("%d\n",n-2);
        else
        printf("0\n");
    }
}

I - System Administrator

题目

Bob got a job as a system administrator in X corporation. His first task was to connect n servers with the help of m two-way direct connection so that it becomes possible to transmit data from one server to any other server via these connections. Each direct connection has to link two different servers, each pair of servers should have at most one direct connection. Y corporation, a business rival of X corporation, made Bob an offer that he couldn’t refuse: Bob was asked to connect the servers in such a way, that when server with index v fails, the transmission of data between some other two servers becomes impossible, i.e. the system stops being connected. Help Bob connect the servers.

Input
The first input line contains 3 space-separated integer numbers n, m, v (3 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ v ≤ n), n — amount of servers, m — amount of direct connections, v — index of the server that fails and leads to the failure of the whole system.

Output
If it is impossible to connect the servers in the required way, output -1. Otherwise output m lines with 2 numbers each — description of all the direct connections in the system. Each direct connection is described by two numbers — indexes of two servers, linked by this direct connection. The servers are numbered from 1. If the answer is not unique, output any.

Examples
Input
5 6 3
Output
1 2
2 3
3 4
4 5
1 3
3 5
Input
6 100 1
Output
-1

题意

构造一个无向图,这个图如果把v去掉就变成非联通图。

思路

以v为割点构造图。让v左边连一个点,然后右边连够m-1条边(这样构造的好处是比较好构造,当然别的方法也是可以的)。
1.n个顶点最多连 ( n − 1 ) ∗ ( n − 2 ) / 2 (n-1)*(n-2)/2 n1n2/2 如果右边能连的最大边数都没有m-1多的话,就不可能建成图,因为边数不够。
2.还有一个极限情况:给定的m如果恰好等于n-1的话可以建一棵树,如果m再少的话就不能建成一个连通图了。

代码

#include <bits/stdc++.h>
using namespace std;
map<int, map<int,int > >mp;
int main()
{
    int n,m,v;
    scanf("%d%d%d",&n,&m,&v);
    int maxn = (n-1)+(n-2)*(n-3)/2+1;
    if(m>=maxn || m<n-1)
    return printf("-1\n"),0;
    int u;
    for(int i=1;i<=n;i++)
    {
        if(i!=v)
        {
            mp[i][v]=mp[v][i]=1;
            u=i;
            printf("%d %d\n",i,v);
            m--;
            break;
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i!=j && i!=u && j!=u && !mp[i][j] && !mp[j][i])
            {
                mp[i][j]=mp[j][i]=1;
                printf("%d %d\n",i,j);
                m--;
            }
            if(m==0) break;
        }
        if(m==0) break;
    }
}

J - Second Order Statistics CodeForces - 22A

题目

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

题意

找到一个严格比最小值大的数

思路

水题 没啥好说的

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
int a[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    int minn = a[1];
    for(int i=2;i<=n;i++)
    {
        if(a[i]>minn)
        return printf("%d\n",a[i]),0;
    }
    printf("NO\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值