BAPC 2022 部分题解

F (6). Fastestest Function


题目描述
题目描述
You are working as a software developer for the Bug Acquisition Programming Company. They developed a specific piece of software called Program C that they sell to their clients. For the past weeks, you have been working on optimising a specific function foo in the
main code path in Program C. You have made it a lot faster and would like to show off to your boss about it.

Your IDE has a nice tool that allows you to profile your code and tell you what percentage of the total running time foo takes. You can run this on the version before your change and after your change. However, you think it looks a lot cooler if you can just tell your boss how
much faster you have made foo itself.

输入描述
The input consists of:

One line with two integers x
and y
(0 ), where x
is the percentage of the total running time that foo took before optimising and y
the percentage of the total running time it took after optimising.
输出描述
Output the factor of how much faster foo got after your optimization.

Your answer should have an absolute or relative error of at most 10−6
.

样例
输入 复制
75 50
输出 复制
3.0
输入 复制
50 75
输出 复制
0.3333333333333333
输入 复制
50 50
输出 复制
1.0
 

分析:有个程序叫C,C里有多个任务,由foo任务和其他任务组成。而我只对foo任务的部分进行优化,foo任务跑的部分变快了,但是其他任务跑的时间不变。

x是foo跑的时间占总时间的百分比

y是优化后foo跑的时间占优化后时间的百分比

#include 
using namespace std;
int main(){
    double a1,a2;   
    cin>>a1>>a2;   
    //foo部分占优化前总时间的百分比
    //foo部分占优化后总时间的百分比
    
    //假设优化前总时间为100s
    double b = 100-a1;//其他任务跑的时间(不变)
    double c = 100-a2;//其他任务跑的时间,占优化后总时间的百分比
    double after = b/(c*0.01);//优化后总时间
    double d = after*(a2*0.01);
    printf("%.7f",a1/d);
    
}

A (1). Abbreviated Aliases


题目
You are the owner of a large successful internet site with lots of users. All these users have chosen an alias of exactly L
characters for logging into the site. Recently, you noticed that you started running into disk space issues: storing all these aliases takes up a lot of data!

You do not have enough money to buy extra storage, so you are looking for ways to reduce the storage space needed. A friend gives you the following compression idea that might help: instead of storing the full alias for each user, you might get away with only storing a prefix of that alias, as long as no other alias has the same prefix. For example, if you just have the aliases james and jacob, you can store only jam and jac and still be able to identify them both.

This idea sounds quite interesting to you, and you are looking forward to finally having more space available on your disk again. You would like to find out how much space you need to store all aliases using this compression technique.

输入描述
The input consists of:

One line with two integers N,L(2≤N≤104,1≤L≤103
),the number of aliases and the length of each alias.
N
lines, each with an alias:a string consisting of exactly L
English lowercase characters(a-z), Each alias is unique.
输出描述
Output the total number of characters you still need to store if you apply this compression technique.

样例
输入 复制
2 5
james
jacob
输出 复制
6
输入 复制
4 4
xxxx
yxxx
xxyx
yxxy
输出 复制
14
 

分析:

要保证每个名字的前缀名不能和其他名字的前缀字符串一样,因此我们要比较最相似的字符串

所以用sort对这些字符串进行排序,这样的话,跟s[i]最相似的字符串就是s[i-1]和s[i+1]。

那么一个字符串改截取多少长度合适呢?

把s[i]跟s[i-1]和s[i+1]逐字符比较,一旦遇到不一样的,就break

但是,因为要对s[i-1]和s[i+1]同时满足前缀名不能和其他名字的前缀字符串一样 ,所以要选取max(temp1,temp2)

#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include 
#include
#include 
#include
using namespace std;
#define inf 0x3f3f3f3f
//#define ll long long 
#define int long long
#define lson x << 1
#define rson x << 1 | 1
const int N = 1e5 + 5;

string s[10005];
signed main()
{
	int n, l;
	cin >> n >> l;
	for (int i = 1; i <= n; i++) {
		cin >> s[i];
	}
	sort(s + 1, s + n + 1);
	
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		int a1 = 0, a2 = 0;
		for (int j = 0; j < l; j++) {
			if (s[i][j] != s[i + 1][j]) {
				a1 += j + 1;
				break;
			}
		}
		for (int j = 0; j < l; j++) {
			if (s[i][j] != s[i - 1][j]) {
				a2 += j + 1;
				break;
			}
		}
		ans += max(a1, a2);
	}
	
	//xxxx,3
	//xxyx,3
	//yxxx,4
	//yxxy,4

	cout << ans;
	return 0;
}

你可能感兴趣的:(算法)