开学第三周(习题+感悟)

(一)UCF Local Programming Contest 2013(Practice)

A - Gas Price Is Going Up/Down

题目描述

The gas stations have billboards showing the gas prices (we’ll assume only three categories of gas: Regular, Plus, and Super). These billboards display the prices of gas, but the problem is that sometimes some digits are missing from the prices on the billboards!
The Problem:
Given prices for the three categories of gas with at most one digit missing from a given price, you are to determine the exact value of each price. We will assume that Regular is cheaper than Plus which is cheaper than Super. Also assume that Regular is at least $2.00 and Super is at most $5.00.

Input

There will be multiple billboards (test cases) in the input file. The first input line contains a positive integer nn, indicating the number of billboards to be processed. The billboards will be on the following nn input lines, each on a separate line. Each billboard contains three prices, the first showing Regular, the second representing Plus, and the last showing Super. The first price starts in column one, each price uses three columns (decimal points are not in the input), and there is exactly one space separating prices. The characters used in a price are only digits 0 through 9 and hyphen to indicate missing digit (there is at most one hyphen per price). Since gas is at least $2.00, the digits 0 or 1 will not appear as the first character for a price in the input. Similarly, the maximum gas price ($5.00) dictates possible valid values for the first character.

Output

At the beginning of each test case, output “Gas Station #g:” where gg is the test case number (starting from 1). For each gas station, print the input values and then the output values (each on a separate line and indented three columns). If there are multiple possible (valid) answers, use the lowest valid value for Regular. Then, with the lowest valid value for Regular, use the lowest valid value for Plus. Then, with the lowest valid value for Plus, use the lowest valid value for Super. Assume that input values will always result into at least one possible valid answer.
Leave a blank line after the output for each test case. Follow the format illustrated in Sample Output. Be sure to line up the output with spaces exactly as given in the Sample Output.

Sample Input

2
2-9 285 -99
-50 -99 -99

Sample Output

Gas Station #1:
Input: 2-9 285 -99
Output: 209 285 299

Gas Station #2:
Input: -50 -99 -99
Output: 250 299 399

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const double pi = 3.14159;
const int mod = 998244353;
int turn(string s,int r){
	int ans;
	if(s[0]!='-'&&s[1]!='-'&&s[2]!='-'){
		ans = (s[0]-'0')*100+(s[1]-'0')*10+(s[2]-'0');
		return ans;
	}
	if(s[0] == '-'){
		ans = (s[1]-'0')*10+(s[2]-'0');
		int k = 100;
		for(int i = 2;i <= 5;i++){
			int d = ans + k * i;
			if(d < 200 || d > 500) continue;
			if(d > r) return d;
		}
	}else if(s[1] =='-'){
		ans = (s[0]-'0')*100+(s[2]-'0');
		int k = 10;
		for(int i = 0;i <= 9;i++){
			int d = ans + k * i;
			if(d < 200 || d > 500) continue;
			if(d > r) return d;
		}
	}else if(s[2] =='-'){
		ans = (s[0]-'0')*100+(s[1]-'0')*10;
		for(int i = 0;i <= 9;i++){
			int d = ans + i;
			if(d < 200 || d > 500) continue;
			if(d > r) return d;
		}
	}
}
int main(){
	int cas;
	scanf("%d",&cas);
	for(int e = 1;e <= cas;e++){
		string a,b,c;
		cin >> a >> b >> c;
		int x = turn(a,199);
		int y = turn(b,x);
		int z = turn(c,y);
		printf("Gas Station #%d:\n",e);
		printf("   Input:  ");
		cout << a << " " << b << " " << c << endl;
		printf("   Output: ");
		cout << x << " " << y << " " << z << endl;
		printf("\n");
	} 
	return 0;
}

B - Cell Phone Contacts

题目描述

Modern cellular phones have a small chip, called a SIM card, that allows the phone to interface with the carrier’s network. SIM cards also have a small amount of memory onboard where the user can store data, such as names, phone numbers, and e-mail addresses. A common problem with SIM card memory is that it cannot group several numbers under the same name, as most cell phones can. This poses a problem when the user wishes to switch phones. The easiest way to transfer contacts is to copy them to the SIM card from the old phone, and then copy them back from the SIM card to the new phone. However, if the user has multiple phone numbers and/or e- mail addresses for a given name, the contact information will be broken up, with multiple entries of the same name, each associated with a different phone number or e-mail address.
Obviously, the user of the new phone would like for his or her contact data to be organized, with each contact’s name listed only once, and the various phone numbers and e-mail addresses for that contact organized under that single entry. You are to write a program to help with this.
The Problem:
Given the contact data from a SIM card, consisting of pairs of names and email addresses or names and phone numbers, generate organized input data for the new cell phone. The new cell phone has a strict data format that must be followed precisely (the format is described in The Output section below).

Input

There will be multiple contact lists (test cases) to process. The first line of each contact list contains a single integer, n ( 1 ≤ n ≤ 100), indicating the number of contact data entries on the SIM card. On each of the next nn lines will be a name, followed by either a phone number or an e-mail address. Names consist of exactly two strings of upper- and lower-case alphabetic characters (the first name followed by the last name). Each first (last) name is at least one character and at most 20 characters. There will be exactly one space between the first and last names in input, and one space between the last name and the phone number or e-mail address. A phone number will consist of exactly 10 digits (no hyphens). An e-mail address will consist of a string of letters and/or numbers separated by exactly one at (’@’) sign, and at least one dot (’.’). No input line will exceed 80 characters. There will be no leading or trailing whitespace on any line.
End of input is indicated by a value of 0 for nn. This case should not be processed.

Output

For each data set, print the heading “Contact list #d:” where dd is the set number in the input (starting with 1). Then, for each unique contact (by name) in the set, output a contact entry. Note that case is significant, so “John DOE” is a different contact from “John Doe”. Contact entries must be output in the following format:

Phone:
<Phone Number 1>
<Phone Number 2>


E-Mail:
<E-Mail Address 1>
<E-Mail Address 2>

Phone numbers should be printed in the format “(123)456-7890”. E-mail addresses should be printed exactly as input. Contact entries should be output in ascending ASCII order by last name, then first name. Within each contact, phone numbers should be output in numerical order, and e-mail addresses should be output in ascending ASCII order. There must be no leading or trailing spaces in the output. (Note that the built-in string comparison functions do compare strings in ASCII order.)
Leave a blank line after the output for each data set. Follow the format illustrated in Sample Output.

Sample Input

6
John Doe 4071234567
Bill Smith bsmith@somewhere.com
Bill Smith 1231231234
John Doe John.Doe@my.house.net
John Doe John.Doe@work.com
Bill Smith 1234567890
2
Bone Head 1231231234
Air Head airhead@my.house.net
0

Sample Output

Contact list #1:
John Doe
Phone:
(407)123-4567
E-Mail:
John.Doe@my.house.net
John.Doe@work.com

Bill Smith
Phone:
(123)123-1234
(123)456-7890
E-Mail:
bsmith@somewhere.com

Contact list #2:
Air Head
Phone:
E-Mail:
airhead@my.house.net

Bone Head
Phone:
(123)123-1234
E-Mail:

理解

其实只要记录下来sort就好了
值得注意的是名字排序的时候是先按姓再是名
没注意这个点wa了两发,血亏

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const double pi = 3.14159;
const int mod = 998244353;
string add[107][107];
string tele[107][107];
int ad[107];
int te[107];
struct node{
	string na;
	string xi;
	int num;
}name[107];
map<string,int> mp;
bool cmp(node a,node b){
	if(a.xi == b.xi) return a.na < b.na;
	return a.xi < b.xi;
}
int check(string ss){
	int fl = 0;
	for(int i = 0;i < ss.size();i++){
		if(ss[i] == '@') return 0;
	}
	return 1;
}
int main(){
	int n;
	int cas = 1;
	while(scanf("%d",&n) && n){
		memset(ad,0,sizeof(ad));
		memset(te,0,sizeof(te));
		int w = n;
		int idx = 0;
		while(w--){			
			string s,t1,t2,t3;
			cin >> t1 >> t2 >> t3;
			s = t1 + " " + t2;
			int k;
			if(mp[s] == 0){
				k = idx;
				name[idx].na = t1;
				name[idx].xi = t2;
				name[idx].num = idx;
				idx++;
				mp[s] = 1;
			}else{
				for(int i = 0;i < idx;i++){
					string tmp = name[i].na + " " + name[i].xi;
					if(tmp == s){
						k = name[i].num;
						break;
					}
				}
			}
			int x;
			if(check(t3)){
				x = te[k];
				tele[k][x] = t3;
				te[k]++;
			}else{
				x = ad[k];
				add[k][x] = t3;
				ad[k]++;
			}
			for(int i = 0;i < idx;i++){
				sort(tele[i],tele[i]+te[i]);
				sort(add[i],add[i]+ad[i]);
			}
		}
		printf("Contact list #%d:\n",cas++);
		sort(name,name+idx,cmp);
		for(int q = 0;q < idx;q++){
			cout << name[q].na <<" " << name[q].xi<< endl;
			printf("Phone:\n");
			int e = name[q].num;
			for(int i = 0;i < te[e];i++){
				string g = tele[e][i];
				cout << "("<< g.substr(0,3) << ")";
				cout << g.substr(3,3)<<"-";
				cout << g.substr(6,4);
				printf("\n");
			}
			printf("E-Mail:\n");
			for(int i = 0;i < ad[e];i++){
				string g = add[e][i];
				cout << g << endl;
			}
			printf("###\n");
		}
		printf("\n");
	}
	return 0;
}

C - Dr. Sukthankar’s Robot

题目描述

You are taking Dr. Sukthankar’s Introduction to Robotics course and are working on your first assignment, to program your new robot to bring Dr. S a cup of coffee. Unfortunately, a fellow student, who decided that he wants your coffee instead, has mis-programmed your robot. Luckily, your robot sends messages to your computer for every move it makes. Using this information, you can deduce where your robot is and direct it to the coffee station and then to Dr. S.
The Problem:
You will be given a listing of the robot’s movements on the Cartesian plane Initially, the robot is located at position (0,0), facing in the direction of the positive x-axis. You will also be given coordinates of both the coffee machine and Dr. S. Your goal will be to first follow the moves the robot has already made, and then give the robot instructions to travel from its current location to the coffee machine and then to Dr. S. The robot understands two types of instructions:

1)Turning (LEFT, RIGHT, UTURN)
2)Moving a specified length in the current direction the robot is facing.
There are further restrictions with respect to what the robot can do:

  1. After making the moves following the instructions given in the initial listing (by your mischievous classmate), when moving from the robot’s location to the coffee maker, the robot MUST first move in the direction of the x-axis (if necessary), THEN move in the direction of the y-axis (if necessary), to reach the coffee maker. This also applies when moving from the coffee maker to Dr. S.
  2. When moving from one location to another, the robot MUST minimize the distance it travels AND the number of commands it takes, given that restriction #1 is satisfied.
    The syntax for the instructions given to the robot is as follows:
    1)Each command should be on a line by itself.
    2)Commands for turning are LEFT, RIGHT and UTURN.
    3)The command for moving is MOVE mm, where mm represents the number of units to move (in the direction the robot is facing). Note: mm must be a positive integer.
    Note: The left and right turns are always 90 degree turns and the u-turn is 180 degrees. Also, assume that nothing blocks the robot, e.g., the robot can go through the location where Dr. S is to get to the coffee machine (i.e., the robot does not have to go around a block).

Input

There will be multiple test cases. The first line of input file contains a positive integer, nn, representing the number of test cases in the input. The test cases follow. The first line of each test case contains a single positive integer kk, representing the number of commands the robot has already executed from its starting position (the origin, facing in the direction of the positive x-axis). The next kk lines contain the commands the robot has executed. These commands start in column one and there is exactly one space separating the different parts on a line. The list of commands is followed by two ordered pairs on a line separated by spaces, representing the x-y coordinates of the coffee machine and Dr. S, respectively. These four numbers are integers. Assume that the locations of the coffee machine and Dr. S are distinct.

Output

At the beginning of each test case, output “Robot Program #p:”, where pp is the test case number (starting from 1). For each test case, follow all the commands the robot has already processed and print its location. Then, print (give) the commands necessary to take the robot to the coffee machine and then print (give) the commands necessary to take the robot from the coffee machine to Dr. S, complying to the restrictions in the problem statement. Leave a blank line after the output for each test case. Follow the format illustrated in Sample Output.

Sample Input

3
3
MOVE 100
LEFT
MOVE 50
100 175 200 200
4
UTURN
MOVE 50
LEFT
MOVE 100
-25 50 0 0
1
MOVE 1
2 0 3 0

Sample Output

Robot Program #1:
The robot is at (100,50)
MOVE 125
RIGHT
MOVE 100
LEFT
MOVE 25

Robot Program #2:
The robot is at (-50,-100)
LEFT
MOVE 25
LEFT
MOVE 150
RIGHT
MOVE 25
RIGHT
MOVE 50

Robot Program #3:
The robot is at (1,0)
MOVE 1
MOVE 1

理解

就是个模拟题,我还是写的太麻烦了,主要就是记录当前方向

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const double pi = 3.14159;
const int mod = 998244353;

int main(){
	int cas;
	scanf("%d",&cas);
	for(int e = 1;e <= cas;e++){
		int fl = 1;
		int sx = 0,sy = 0;
		int n;
		scanf("%d",&n);
		while(n--){
			string s;
			cin >> s;
			if(s == "MOVE"){
				int h;
				scanf("%d",&h);
				if(fl == 1) sx += h;
				if(fl == 2) sy += h;
				if(fl == -1) sx -= h;
				if(fl == -2) sy -= h;
			}
			if(s == "LEFT"){
				if(fl == 1) fl = 2;
				else if(fl == 2) fl = -1;
				else if(fl == -1) fl = -2;
				else if(fl == -2) fl = 1;
			}
			if(s == "RIGHT"){
				if(fl == 1) fl = -2;
				else if(fl == 2) fl = 1;
				else if(fl == -1) fl = 2;
				else if(fl == -2) fl = -1;
			}
			if(s == "UTURN"){
				if(fl == 1) fl = -1;
				else if(fl == 2) fl = -2;
				else if(fl == -1) fl = 1;
				else if(fl == -2) fl = 2;
			}			
		}
		int cx,cy,dx,dy;
		scanf("%d %d %d %d",&cx,&cy,&dx,&dy);
		printf("Robot Program #%d:\n",e);
		printf("The robot is at (%d,%d)\n",sx,sy);
		if(sx != cx){
			if(sx < cx){
				if(fl != 1){
					if(fl == 2) printf("RIGHT\n");
					if(fl == -2) printf("LEFT\n");
					if(fl == -1) printf("UTURN\n");
					fl = 1;
				}
				printf("MOVE %d\n",cx-sx);
			}else{
				if(fl != -1){
					if(fl == -2) printf("RIGHT\n");
					if(fl == 2) printf("LEFT\n");
					if(fl == 1) printf("UTURN\n");
					fl = -1;
				}
				printf("MOVE %d\n",sx-cx);
			}
		}
		if(sy != cy){
			if(sy < cy){
				if(fl != 2){
					if(fl == -1) printf("RIGHT\n");
					if(fl == 1) printf("LEFT\n");
					if(fl == -2) printf("UTURN\n");
					fl = 2;
				}
				printf("MOVE %d\n",cy-sy);
			}else{
				if(fl != -2){
					if(fl == 1) printf("RIGHT\n");
					if(fl == -1) printf("LEFT\n");
					if(fl == 2) printf("UTURN\n");
					fl = -2;
				}
				printf("MOVE %d\n",sy-cy);
			}
		}
		sx = cx,sy = cy;
		if(sx != dx){
			if(sx < dx){
				if(fl != 1){
					if(fl == 2) printf("RIGHT\n");
					if(fl == -2) printf("LEFT\n");
					if(fl == -1) printf("UTURN\n");
					fl = 1;
				}
				printf("MOVE %d\n",dx-sx);
			}else{
				if(fl != -1){
					if(fl == -2) printf("RIGHT\n");
					if(fl == 2) printf("LEFT\n");
					if(fl == 1) printf("UTURN\n");
					fl = -1;
				}
				printf("MOVE %d\n",sx-dx);
			}
		}
		if(sy != dy){
			if(sy < dy){
				if(fl != 2){
					if(fl == -1) printf("RIGHT\n");
					if(fl == 1) printf("LEFT\n");
					if(fl == -2) printf("UTURN\n");
					fl = 2;
				}
				printf("MOVE %d\n",dy-sy);
			}else{
				if(fl != -2){
					if(fl == 1) printf("RIGHT\n");
					if(fl == -1) printf("LEFT\n");
					if(fl == 2) printf("UTURN\n");
					fl = -2;
				}
				printf("MOVE %d\n",sy-dy);
			}
		}
		printf("\n");
	}
	return 0;
}

(二)Preliminaries for Benelux Algorithm Programming Contest 2019

A - Architecture

题目描述

Your brother has won an award at the recent Breakthroughs in Architectural Problems Conference and has been given the once in a lifetime opportunity of redesigning the city center of his favorite city Nijmegen. Since the most striking parts of a city’s layout are the skylines,your brother has started by drawing ideas for how he wants the northern and eastern skylines of Nijmegen to look. However, some of his proposals look rather outlandish, and you are starting to wonder whether his designs are possible.
For his design, your brother has put an R × C grid on the city. Each cell of the city will contain a building of a certain height. The eastern skyline is given by the tallest building in each of the R rows, and the northern skyline is given by the tallest building in each of the C columns.
A pair of your brother’s drawings of skylines is possible if and only if there exists some way of assigning building heights to the grid cells such that the resulting skylines match these drawings.
Figure A.1 shows a possible city with the northern and eastern skylines exactly as given in the input of the first sample.
在这里插入图片描述

Input

The first line consists of two integers 1 =<R,C =<100, the number of rows and columns in the grid.
The second line consists of R integers x1, . . . , xR describing the eastern skyline (0=<xi =<1000 for all i).
The third line consists of C integers y1, . . . , yC describing the northern skyline (0 =< yj =<1000 for all j).

Output

Output one line containing the string possible if there exists a city design that produces the specified skyline, and impossible otherwise.

Sample Input

样例1:
4 4
4 3 2 1
1 2 3 4
样例2:
4 4
1 2 3 4
1 2 3 2

Sample Output

样例1:
possible
样例2:
impossible

理解

本来以为要算各种数字出现的次数相同
后来发现只要要求的最大值一致即可
因为满足最大值后剩下的都可以用0填掉的

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int mod = 998244353;

int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	int m1 = 0,m2 = 0;
	for(int i = 1;i <= n;i++){
		int x;
		scanf("%d",&x);
		m1 = max(m1,x);
	}
	for(int i = 1;i <= m;i++){
		int x;
		scanf("%d",&x);
		m2 = max(m2,x);
	} 
	if(m1 == m2) printf("possible\n");
	else printf("impossible\n");
	return 0;
}

B - Deceptive Dice

题目描述

Recently your town has been infested by swindlers who convince unknowing tourists to play a simple dice game with them for money. The game works as follows: given is an n-sided die,whose sides have 1, 2, . . . , n pips, and a positive integer k. You then roll the die, and then have to make a choice. Option 1 is to stop rolling. Option 2 is to reroll the die, with the limitation that the die can only be rolled k times in total. Your score is the number of pips showing on your final roll.
Obviously the swindlers are better at this game than the tourists are. You, proud supporter of the Battle Against ProbabilisticCatastrophes, decide to fight this problem not by banning the swindlers but by arming the tourists with information.
You create pamphlets on which tourists can find the maximum expected score for many values of n and k. You are sure that the swindlers will soon stop their swindling if the tourists are better prepared than they are!
The layout of the flyers is done, and you have distribution channels set up. All that is left to do is to calculate the numbers to put on the pamphlet.
Given the number of sides of the die and the number of times you are allowed to roll, calculate the expected (that is, average) score when the game is played optimally.
在这里插入图片描述

Input

A single line with two integers 1 ≤ n ≤ 100, the number of sides of the die, and 1 ≤ k ≤ 100, the number of times the die may be rolled.

Output

Output the expected score when playing optimally. Your answer should have an absolute or relative error of at most 10^-7.
Sample Input 4:
8 9
Sample Output 4:
7.268955230712891

Sample Input

样例输入1
1 1
样例输入2
2 3
样例输入3
6 2

Sample Output

样例输出1
1
样例输出2
1.875
样例输出3
4.25

理解

起先以为是贪心,根据选择扔下一次或者不扔得到的贡献作比较,选更优解
然后wa了,题目给的sample4就没过,果然是思路不太行
看了题解(奇怪的知识增加了)原来是分数找规律题

在这里插入图片描述

AC代码

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main() {
    double n, k;
    double a = 0;
    cin >> n >> k;
    for (auto i = 0; i < k; i++) {
        auto h = floor(a);
        auto f = h / n;
        a = f * a + (1 - f) * ((h + n + 1) / 2);
    }
    cout << setprecision(9) << a << endl;
    return 0;
}

C - Exits in Excess

题目描述

You own a disco called the Boogie Always Persists Club.The club is famous for its multiple interconnected rooms to twist and shout in. The rooms and the corridors between them form a maze-like structure and for added bonus you have made all the corridors one-way. However, it turns out not everyone is as happy with your club as you are. Recently the fire safety inspectors came by and they were not amused by what they saw: if a fire were to break out in one of the rooms, people would have great difficulty finding the fire exits and might even start running around in circles! They find this completely unacceptable and order you to improve things as quickly as possible. They insist that you have to make sure that no one can run around in circles in the club by removing some of the corridors between the rooms.
You, on the other hand, want to retain the attractiveness of the rooms. You do not want to remove too many corridors, because then people will no longer visit your club. You decide that at most half of the corridors may be removed.
Given the layout of the club, remove at most half of the corridors so that no cycles remain
在这里插入图片描述

Input

One line containing the number of rooms 1 ≤ n ≤ 10^5 and the number of corridors 0 ≤ m ≤ 2 · 10^5.
Then follow m lines, each containing two different 1-based integers u and v indicating a corridor from room u to room v. There will be no corridor from a room to itself, nor will there be more than one corridor from one room to any other single room.

Output

On the first line, print a single integer 0 ≤ r ≤ m/2, the number of corridors to be removed.
Then print r lines containing the 1-based indices of the corridors that need to be removed to ensure that dancers cannot go around in circles in the disco anymore.
If there are multiple valid solutions, you may output any one of them.
在这里插入图片描述

Sample Input

样例输入1
2 2
1 2
2 1
样例输入2
3 3
1 2
2 3
3 1
样例输入3
4 5
1 2
1 3
3 2
2 4
3 4

Sample Output

样例输出1
1
2
样例输出2
1
1
样例输出3
0

理解

倒也不必想的那么麻烦,题目只说要求减掉不多于一半的路
所以

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const double pi = acos(-1);
const int mod = 998244353;
struct node{
	int u,v,id;
}edge[maxn];

int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	int x = 0,y = 0;
	for(int i = 0;i < m;i++){
		cin >> edge[i].u >> edge[i].v;
		if(edge[i].u > edge[i].v) x++;
		else y++;
	}
	if(x < y){
		printf("%d\n",x);
		for(int i = 0;i < m;++i){
			if(edge[i].u > edge[i].v) printf("%d\n",i+1);
		}
	}
	else{
		printf("%d\n",y);
		for(int i = 0;i < m;++i){
			if(edge[i].u <= edge[i].v) printf("%d\n",i+1);
		}
	}
	return 0;
}

(三)Atcoder - Panasonic Programming Contest 2020

E - Three Substrings

题目描述

Snuke has a string s. From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows:
Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with ?s.
For example, if s is mississippi, we can choose the substring ssissip and replace its 1-st and 3-rd characters with ? to obtain ?s?ssip.
You are given the strings a, b, and c. Find the minimum possible length of s.
Constraints
1≤|a|,|b|,|c|≤2000
a, b, and c consists of lowercase English letters and ?s.

Input

a
b
c

Output

Print the minimum possible length of s.

Sample Input

样例1:
a?c
der
cod
样例2:
atcoder
atcoder
???

Sample Output

样例1:
7
样例2:
7

理解

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 7;
const double pi = acos(-1);
const int mod = 998244353;
const double eps = 1e-8;
int ab[maxn*2],ac[maxn*2],bc[maxn*2];
int check(char x,char y){
	if(x == '?' || y == '?' || x == y) return 1;
	return 0;
}
void init(string a,string b,int n,int m,int *vis){
	for(int i = 0;i < n;i++){
		for(int j = 0;j < m;j++){
			// +maxn是为了吧反着可能出现负数的情况也记录 
			if(!check(a[i],b[j])) vis[i-j+maxn] = 1;
		}
	}
	return;
}
int main(){
	int M = 2e3;
//	ios::sync_with_stdio(false);
//	cin.tie(0),cout.tie(0);
	string a,b,c;
	cin >> a >> b >> c;
	int la = a.size(),lb = b.size(),lc = c.size();
	//预处理两两字符串之间可以重合的位置 
	init(a,b,la,lb,ab);
	init(a,c,la,lc,ac);
	init(b,c,lb,lc,bc);
	//暴力跑每一处循环 
	int ans = 3 * M;
	//i枚举ab之间的间隔 
	for(int i = -(M<<1);i <= (M<<1);i++){
		//j枚举ac 
		for(int j = -(M<<1);j <= (M<<1);j++){			 
			if(!ab[i+maxn] && !ac[j+maxn] && !bc[j-i+maxn]){
				int l = min(0,min(i,j));
				int r = max(la,max(lb+i,lc+j));
				ans = min(ans,r-l);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

(三)我的感想

萌新落泪

啊又水了一周呢(划掉),很难得的完成了一次ak?其实是因为那场的题目不难,主要是题目看起来复杂,普通方法都能AC的。。。and我好菜鸭,日常一叹
还有好多题目没补的说,(拖延症诶)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值