标签:def substr put 图片 missing multiple only eth 种类型
A - Complete the Word(暴力)
Description
ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice.
Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him?
Input
The first and only line of the input contains a single string s (1 ≤ |s| ≤ 50 000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet (‘A‘-‘Z‘) or is a question mark (‘?‘), where the question marks denotes the letters that ZS the Coder can‘t remember.
Output
If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print - 1 in the only line.
Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters.
If there are multiple solutions, you may print any of them.
Sample Input
ABC??FGHIJK???OPQR?TUVWXY?
ABCDEFGHIJKLMNOPQRZTUVWXYS
WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO
-1
??????????????????????????
MNBVCXZLKJHGFDSAQPWOEIRUYT
AABCDEFGHIJKLMNOPQRSTUVW??M
-1
Hint
In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.
In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 that contains all the letters of the alphabet, so the answer is - 1.
In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer.
题意:给你一个字符串,字符串由26个大写字母和?组成,可以将?替换成任何大写字母,问有没有子串只有26个不同的大写字母,有则输出原串,且?被替换掉,否则输出-1
分析:暴力即可,遍历所有的子串,如果某个子串中的大写字母出现了一次以上,则遍历下一个子串,如果某个子串满足条件,替换子串中的?替换成相应的大写字母,并且将原串中其他的?替换成大写字母
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int main() 6 { 7 char s[50005]; 8 while(~scanf("%s",s)) 9 { 10 int len = strlen(s); 11 int a[26],i;//a标记子串中出现的大写字母 12 if (len < 26) { 13 printf("-1\n"); 14 continue; 15 }//如果所给的原串的长度小于26直接输出-1 16 for (i = 0; i < len-25; i++) 17 { 18 memset(a,0,sizeof(a)); 19 int flag = 0; 20 for (int j = i; j <= i+25; j++) 21 { 22 if (s[j] == ‘?‘) 23 continue;//当碰到?时继续遍历 24 if (a[s[j]-65] == 0) { 25 a[s[j]-65] = 1; 26 } else {//当子串中的某个大写字母出现了一次以上,遍历下一个子串 27 flag = 1; 28 break; 29 } 30 } 31 if(flag == 1) 32 continue; 33 int b[26]={0};//标记没有出现的字符 34 int k=0; 35 for (int j = 0; j < 26; j++) 36 { 37 if(a[j] == 0) 38 b[k++] = j; 39 } 40 k = 0; 41 for (int j = i; j <= i+25; j++) 42 { 43 if (s[j] == ‘?‘) s[j] = b[k++] + 65;//将子串中的?替换成相应的大写字母 44 } 45 for (int j = 0; j < len; j++) 46 { 47 if (s[j] == ‘?‘) { 48 printf("A");//将原串中其他的?用大写字母输出 49 } else { 50 printf("%c",s[j]); 51 } 52 } 53 printf("\n"); 54 break; 55 } 56 if(i == len-25) 57 printf("-1\n")//没有遍历到符合条件的子串 则输出-1; 58 } 59 return 0; 60 }
C - Anatoly and Cockroaches
Description
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly‘s room.
Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it‘s color.
Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.
The second line contains a string of length n, consisting of characters ‘b‘ and ‘r‘ that denote black cockroach and red cockroach respectively.
Output
Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.
Sample Input
5
rbbrr
1
5
bbbbb
2
3
rbr
0
Hint
In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.
In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.
In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.
题意:给你一个字符串只由‘r‘和‘b‘组成,你可以对字符进行改变和交换操作,每个操作花费1,问如何使这个字符串变成交替的,并且花费最少
分析:只可能有rbrbrbr 和 brbrbrb两种类型,让字符串与这两种类型的比较对照
#include<algorithm> #include<cstdio> using namespace std; int main() { int n; char s[100005]; while(~scanf("%d",&n)) { scanf("%s",s); int sum1 = 0, sum2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] != ‘r‘) sum1++; } else { if (s[i] != ‘b‘) sum2++; } }//比较对照 int ans=0; ans = abs(sum2-sum1) + min(sum1,sum2);//min(sum1,sum2)让不在位置上的r,b进行交换,ans(sum1,sum2)表示剩余的没有交换的r或b sum1 = 0, sum2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0){ if (s[i] != ‘b‘) sum1++; } else { if (s[i] != ‘r‘) sum2++; } }//两种类型的字符串 ans = min(ans,abs(sum2-sum1) + min(sum1,sum2)); printf("%d\n",ans); } return 0; }
D - Efim and Strange Grade
Description
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer).
There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.
In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.
For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.
Input
The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim‘s grade and the number of seconds till the end of the break respectively.
The second line contains the grade itself. It‘s guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it‘s representation doesn‘t finish with 0.
Output
Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.
Sample Input
6 1
10.245
10.25
6 2
10.245
10.3
3 100
9.2
9.2
Hint
In the first two samples Efim initially has grade 10.245.
During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.
In the third sample the optimal strategy is to not perform any rounding at all.
E - Vitya in the Countryside
Description
Every summer Vitya comes to visit his grandmother in the countryside. This summer, he got a huge wart. Every grandma knows that one should treat warts when the moon goes down. Thus, Vitya has to catch the moment when the moon is down.
Moon cycle lasts 30 days. The size of the visible part of the moon (in Vitya‘s units) for each day is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, and then cycle repeats, thus after the second 1 again goes 0.
As there is no internet in the countryside, Vitya has been watching the moon for n consecutive days and for each of these days he wrote down the size of the visible part of the moon. Help him find out whether the moon will be up or down next day, or this cannot be determined by the data he has.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 92) — the number of consecutive days Vitya was watching the size of the visible part of the moon.
The second line contains n integers ai (0 ≤ ai ≤ 15) — Vitya‘s records.
It‘s guaranteed that the input data is consistent.
Output
If Vitya can be sure that the size of visible part of the moon on day n + 1 will be less than the size of the visible part on day n, then print "DOWN" at the only line of the output. If he might be sure that the size of the visible part will increase, then print "UP". If it‘s impossible to determine what exactly will happen with the moon, print -1.
Sample Input
5
3 4 5 6 7
UP
7
12 13 14 15 14 13 12
DOWN
1
8
-1
Hint
In the first sample, the size of the moon on the next day will be equal to 8, thus the answer is "UP".
In the second sample, the size of the moon on the next day will be 11, thus the answer is "DOWN".
In the third sample, there is no way to determine whether the size of the moon on the next day will be 7 or 9, thus the answer is -1.
题意:vitya在记录每晚的月亮,每天晚上可见部分的月亮的大小有着30天的周期,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1。
1的后面紧接着的是0。vitya一共要观察n天,她想知道第n+1天的月亮是上升还是下降,给出这n天里月亮的大小。
分析:如果n=1
当a[n]=0时,第n+1天一定是上升
当a[n]=15时,第n+1天一定是下降
其余情况不确定
当n>1时
当a[n]>a[n-1] 如果a[n]=15,那么第n+1天一定是下降;其余情况,一定是上升
当a[n]<a[n-1] 如果a[n]=0,那么第n+1天一定是上升;其余情况,一定是下降。
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cmath> 6 #include<map> 7 using namespace std; 8 int main() 9 { 10 int n,a[100]; 11 while(~scanf("%d",&n)) 12 { 13 for(int i=1;i<=n;i++) 14 scanf("%d",&a[i]); 15 if(n==1) 16 { 17 if(a[n]==0) 18 printf("UP\n"); 19 else if(a[n]==15) 20 printf("DOWN\n"); 21 else 22 printf("-1\n"); 23 } 24 else 25 { 26 if(a[n]>a[n-1]) 27 { 28 if(a[n]==15) 29 printf("DOWN\n"); 30 else 31 printf("UP\n"); 32 } 33 else if(a[n]<a[n-1]) 34 { 35 if(a[n]==0) 36 printf("UP\n"); 37 else 38 printf("DOWN\n"); 39 } 40 else 41 { 42 printf("-1\n"); 43 } 44 } 45 } 46 return 0; 47 }
标签:def substr put 图片 missing multiple only eth 种类型
原文地址:https://www.cnblogs.com/LLLAIH/p/9768198.html