Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
ac
b
aaazzz
kkk
abcdefgabcdefh
No such string
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.
题意是输出两个字符创字典序之间的任意字符串,没有则输出没有
将其中一个加1,从后向前扫,z。。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
char a[110],b[110];
int main()
{
cin>>a>>b;
int len=strlen(a);
a[len-1]++;
for(int i=len-1;i>=0;i--)
if(a[i]>'z')
{
a[i-1]++;
a[i]='a';
}
if(strcmp(a,b)==0)
cout<<"No such string";
else
cout<<a;
return 0;
}
Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The newspaper contains string t, consisting of uppercase and lowercase English letters. We know that the length of string t greater or equal to the length of the string s.
The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some n letters out of the newspaper and make a message of length exactly n, so that it looked as much as possible like s. If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS".
Tanya wants to make such message that lets her shout "YAY!" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says "WHOOPS". Your task is to help Tanya make the message.
The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message.
The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper.
Here |a| means the length of the string a.
Print two integers separated by a space:
- the first number is the number of times Tanya shouts "YAY!" while making the message,
- the second number is the number of times Tanya says "WHOOPS" while making the message.
AbC DCbA
3 0
ABC abc
0 3
abacaba AbaCaBA
3 4
注意审题,只有大小写不够用时才。。。。。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
char a[233333],b[233333];
int alpa[100]={};
int alpb[100]={};
int main()
{
cin>>a>>b;
int lena=strlen(a);
int lenb=strlen(b);
for(int i=0;i<lena;i++)
{
if(a[i]>='a') alpa[a[i]-'a'+1]++;
else alpa[a[i]-'A'+27]++;
}
for(int i=0;i<lenb;i++)
{
if(b[i]>='a') alpb[b[i]-'a'+1]++;
else alpb[b[i]-'A'+27]++;
}
int suma=0,sumb=0;
for(int i=1;i<=60;i++)
{
if(alpa[i]>alpb[i])
{
suma+=alpb[i];
alpa[i]-=alpb[i];
alpb[i]=0;
}
else
{
suma+=alpa[i];
alpb[i]-=alpa[i];
alpa[i]=0;
}
}
for(int i=1;i<=26;i++)
{
sumb+=min(alpa[i],alpb[i+26]);
}
for(int i=27;i<=52;i++)
{
sumb+=min(alpa[i],alpb[i-26]);
}
cout<<suma<<" "<<sumb;
return 0;
}
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).
Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture — press the icon of the required application exactly once to launch it.
After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.
All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.
Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order?
Note that one application may be launched multiple times.
The first line of the input contains three numbers n, m, k (1 ≤ n, m, k ≤ 105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.
The next line contains n integers, permutation a1, a2, ..., an — the initial order of icons from left to right in the menu (from the first to the last one), ai — is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai.
The third line contains m integers b1, b2, ..., bm(1 ≤ bi ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.
Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.
8 3 3 1 2 3 4 5 6 7 8 7 8 1
7
5 4 2 3 1 5 2 4 4 4 4 4
8
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8.
After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures.
After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures.
After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.
In total, Anya makes 7 gestures.
题意:滑动屏幕,对选择的图标与前一个交换,k个图标在一个屏幕里,给过程,求滑了多少下屏幕
用两个数组分别利用位置存值和用值存位置,一个一个进入。。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define Max 233333
long long pos[Max];
long long val[Max];
long long n,m,k,t,sum=0,po,va,tmp;
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
cin>>pos[i];
val[pos[i]]=i;
}
sum=0;
for(int i=1;i<=m;i++)
{
cin>>t;
if(val[t]==1)
{
sum+=1;
continue;
}
sum+=val[t]/k;
if(val[t]%k!=0) sum+=1;
po=val[t]-1;
va=pos[po];
val[va]+=1;
val[t]-=1;
tmp=pos[val[va]];
pos[val[va]]=pos[val[t]];
pos[val[t]]=tmp;//容易搞错。。。
}
cout<<sum;
return 0;
}
Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor.
Let's assume that n people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability p, or the first person in the queue doesn't move with probability (1 - p), paralyzed by his fear of escalators and making the whole queue wait behind him.
Formally speaking, the i-th person in the queue cannot enter the escalator until people with indices from 1 to i - 1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after t seconds.
Your task is to help him solve this complicated task.
The first line of the input contains three numbers n, p, t (1 ≤ n, t ≤ 2000, 0 ≤ p ≤ 1). Numbers n and t are integers, number p is real, given with exactly two digits after the decimal point.
Print a single real number — the expected number of people who will be standing on the escalator after t seconds. The absolute or relative error mustn't exceed 10 - 6.
1 0.50 1
0.5
1 0.50 4
0.9375
4 0.20 2
0.4
直接看代码吧
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
double dp[2333][2333]={},n,p,sum;
int t;
int main()
{
cin>>n>>p>>t;
dp[0][0]=1;//
for(int i=1;i<=t;i++)
for(int j=n;j>=0;j--)
{
if(j==n)
dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j];
else if(j==0)
dp[i][j]=dp[i-1][j]*(1-p);
else
dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1-p);
}
for(int i=1;i<=n;i++)
sum+=i*dp[t][i];
printf("%.7lf",sum);
return 0;
}
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n.
This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an), then those numbers will form strictly increasing sequence.
For example, for the following sample: n = 5, k = 3, a = (1, 2, 4, 5, 6) the sequence of numbers will look as follows: (1 + 2 + 4, 2 + 4 + 5, 4 + 5 + 6) = (7, 11, 15), that means that sequence a meets the described property.
Obviously the sequence of sums will have n - k + 1 elements.
Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai.
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively.
The next line contains n space-separated elements ai (1 ≤ i ≤ n).
If ai = ?, then the i-th element of Arthur's sequence was replaced by a question mark.
Otherwise, ai ( - 109 ≤ ai ≤ 109) is the i-th element of Arthur's sequence.
If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes).
Otherwise, print n integers — Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum|ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes.
3 2 ? 1 2
0 1 2
5 1 -10 -9 ? -7 -6
-10 -9 -8 -7 -6
5 3 4 6 7 2 9
Incorrect sequence
第一组是Σ{a1...ak}
类推最后一组是 Σ{an-k+1 ...an}
有一些ai的値不确定(是“?”)
求一个使得 Σ|ai|最小的方案。。
使得这k组,每组元素和是单调递增的。
就是第一组之和小于第二组小于第三组……
注意:可以得出严格a[i]<a[i+k] 再注意绝对值辣
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define INF 0x3fffffff
char s[2];
int a[200000];
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
{
scanf("%s",s);
if(s[0]=='?')
a[i]=INF;
else
sscanf(s,"%d",&a[i]);
}
for(int i=n;i<n+k;i++)
a[i]=INF+1;
for(int i=0;i<k;i++)
{
int mi=-INF,cnt=0;
for(int j=i;j<n+k;j+=k)
{
if(a[j]==INF)
cnt++;
else
{
if(a[j]-mi<=cnt)
{
cout<<"Incorrect sequence";
return 0;
}
int num=max(mi+1, min(a[j]-cnt,-cnt/2));
for(int tmp=cnt;tmp>0;tmp--)
{
int idx=j-tmp*k;
a[idx]=num++;
}
mi=a[j];
cnt=0;
}
}
}
for(int i=0;i<n;i++)
{
if(i==n-1) cout<<a[i];
else cout<<a[i]<<" ";
}
return 0;
}