Supplementary questions of TIANTI competition of Shanghai University of Technology

Posted by dukeu03 on Sat, 29 Jan 2022 10:31:15 +0100

Meaning of question A

Give you a string and let you calculate how many pairs of adjacent letters there are,
For example, a and b, b and c are two pairs of adjacent letters, while a and c, a and z are not. (Note: the letters on any two subscripts can become adjacent pairs. For example, for the string "adfb", the character 'a' with subscript 00 and the character 'b' with subscript 33 are also a pair of adjacent letters)

Problem solving ideas

We just need to find the number of times each letter appears, then multiply the number of adjacent letters and add them.

#include<bits/stdc++.h>

using namespace std;
int n,lab[100];
char s[100010];
int main()
{

	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}

	for(int i=0;s[i] > 0;i++)
	{
		int t=s[i]-'a';
		lab[t]++;
	}

	long long int ans=0;
	for(int i=0;i<=24;i++)
	{
		ans=ans+lab[i]*lab[i+1];
	}
	cout<<ans << endl;
	return 0;
}

Meaning of question B:

Give you a number n, which represents the maximum length of the match. Let you find all three numbers within 1~n, so that these three numbers can form a right triangle, and find the number that can form different triangles.

thinking

Since the given range is 1 < = n < = 10000, the complexity of O(n*n) can still float. So we use a map to store the squares of all numbers from 1 to N, and then we enumerate the two edges to calculate whether their square sum is in the map. If it is, we will add 1 to the result, and the result is the result.

code

#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;

const int maxn = 2e5 + 10;
int n,i,j;
long long a[maxn],x,y,ans, cnt = 0;
unordered_map<long long , long long> mp;
int main(){
	cin >> n;
    for(int i = 1; i <= n; i++)
        mp[i*i]++;
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n; j++){
            ans = i*i + j*j;
            if(mp.count(ans)){
                cnt++;
                //cout << ans << endl;
            }
        }
    }
    cout << cnt << endl;
	return 0;
}

Solution to question C

Link: https://ac.nowcoder.com/acm/contest/14302/C
Source: niuke.com

This is an online observation to test your IQ. Given a positive integer sequence aa with length N, please find a "different" element in the nn number and output the subscript i of the element (1 < = I < = n).
Enter Description:
In the first line, enter a positive integer n, indicating the number of elements of sequence a (3 < = n < = 1000)
On the second line, enter n positive integers ai, (1 < = ai < = 10100, 1 < = I < = n)
Output Description:
Output the subscript of the "different" number (the answer guarantees uniqueness)

< input example 1:
3
1 10 2
Output:
3

Input example 2:
5
23 56 32 131 41
Output:
2

We can look at the size of the input data, 1 < = AI < = 10100, so it's easy to find that we definitely need to input with a string, so we write it as a string and find the law. We soon found that what we require is the sum of all characters in the string and the subscript of only one in all given strings.

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<stdio.h>
#include<iterator>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;

int n ;
string str;
set<int>num;
map<int,int>mp;
map<int,int>mi;

int main(){
    cin>>n;
    for(int i = 1;i<=n;++i){
        cin>>str;
        int ans = 0;
        for(int j = 0;j<str.size();++j){
            ans+=str[j] - '0';
        }
        mp[ans]++;
        mi[ans] = i;
        num.insert(ans);
    }
    for(set<int>::iterator it = num.begin();it != num.end();++it){
        if(mp[*it] == 1){
            cout<<mi[*it]<<endl;
            break;
        }
    }
}

Solution to question D

meaning of the title

When the prefix and the suffix and are the same, and the prefix and the number obtained by the prefix and the suffix and cannot have the intersection prefix and maximum value, directly use two map s to save the prefix and the subscript corresponding to the suffix and, and then use double pointers.

#include <iostream>
#include<unordered_map>
#include<algorithm>
using namespace std;

typedef long long ll;

const int mxn = 2e5 + 5;
int a[mxn];
unordered_map<long long, long long> mp;
unordered_map<long long, long long> mp1;
bool ok[200010] = {false};

ll read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch > '0' && ch <= '9'){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x*f;
}

int main()
{
    ll n, sum = 0; cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum += a[i];
        mp[i] = sum;
    }
    sum = 0;
    for(int i = n; i >= 1; i--){
        sum += a[i];
        mp1[i] = sum;
        //cout << mp1[i] << endl;
    }
    ll i = 1, j = n, maxn = 0;
    while(i < j){
        if(mp[i] < mp1[j]) i++;
        else if(mp[i] > mp1[j]) j--;
        else{
            maxn = max(mp[i], maxn);
            i++;
            j--;
        }

    }
    cout << maxn << endl;
	return 0;
}


Solution to question E

Topics: Algorithm acm