The preferred way to generate user login in Polygon is to concatenate a prefix of the user's first name and a prefix of their last name, in that order. Each prefix must be non-empty, and any of the prefixes can be the full name. Typically there are multiple possible logins for each person.
You are given the first and the last name of a user. Return the alphabetically earliest login they can get (regardless of other potential Polygon users).
As a reminder, a prefix of a string s is its substring which occurs at the beginning of s: "a", "ab", "abc" etc. are prefixes of string "{abcdef}" but "b" and 'bc" are not. A string a is alphabetically earlier than a string b, if a is a prefix of b, or a and b coincide up to some position, and then a has a letter that is alphabetically earlier than the corresponding letter in b: "a" and "ab" are alphabetically earlier than "ac" but "b" and "ba" are alphabetically later than "ac".
The input consists of a single line containing two space-separated strings: the first and the last names. Each character of each string is a lowercase English letter. The length of each string is between 1 and 10, inclusive.
Output a single string — alphabetically earliest possible login formed from these names. The output should be given in lowercase as well.
harry potter
hap
tom riddle
tomr
暴力一遍找到最小的前缀
1 #include2 #include 3 4 using namespace std; 5 string s,ss; 6 7 int k[26]; 8 int a[26]; 9 int main(){ 10 cin>>s>>ss; 11 int slen = s.length(); 12 int sslen = ss.length(); 13 string s1="zzzzzzzzzz",s2; 14 for(int i=0;i ){ 15 for(int j=0;j ){ 16 s2 = s.substr(0,i+1)+ss.substr(0,j+1); 17 s1 = min(s1,s2); 18 // cout< 19 } 20 } 21 cout< 22 return 0; 23 }endl;
You are given an integer N. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be of them.
You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis.
Find the minimal number of layers you have to use for the given N.
The only input line contains a single integer N (1 ≤ N ≤ 100).
Output a single integer - the minimal number of layers required to draw the segments for the given N.
2
2
3
4
4
6
As an example, here are the segments and their optimal arrangement into layers for N = 4.
其实只要两次循环并且自加,其实这种题应该是有公式直接套的。
(公式的话(n+1)/2*(n-(n+1)/2))
但是那样感觉没啥意思。
1 #include2 #include 3 #define N 105 4 using namespace std; 5 6 int vis[N]; 7 int n; 8 int main(){ 9 while(cin>>n){ 10 memset(vis,0,sizeof(vis)); 11 for(int i=1;i<=n;i++){ 12 for(int j=i;j<=n;j++){ 13 for(int l=i;l<=j;l++){ 14 vis[l]++; 15 } 16 } 17 } 18 int cnt = 0; 19 for(int i=1;i<105;i++){ 20 cnt = max(cnt,vis[i]); 21 } 22 cout< endl; 23 } 24 return 0; 25 }
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation.
We will consider an extremely simplified subset of Python with only two types of statements.
Simple statements are written in a single line, one per line. An example of a simple statement is assignment.
For statements are compound statements: they contain one or several other statements. For statement consists of a header written in a separate line which starts with "for" prefix, and loop body. Loop body is a block of statements indented one level further than the header of the loop. Loop body can contain both types of statements. Loop body can't be empty.
You are given a sequence of statements without indentation. Find the number of ways in which the statements can be indented to form a valid Python program.
The first line contains a single integer N (1 ≤ N ≤ 5000) — the number of commands in the program. N lines of the program follow, each line describing a single command. Each command is either "f" (denoting "for statement") or "s" ("simple statement"). It is guaranteed that the last line is a simple statement.
Output one line containing an integer - the number of ways the given sequence of statements can be indented modulo 109 + 7.
4
s
f
f
s
1
4
f
s
f
s
2
In the first test case, there is only one way to indent the program: the second for statement must be part of the body of the first one.
simple statement
for statement
for statement
simple statement
In the second test case, there are two ways to indent the program: the second for statement can either be part of the first one's body or a separate statement following the first one.
for statement
simple statement
for statement
simple statement
or
for statement
simple statement
for statement
simple statement
题意:python 里面,给出 n 个 for 循环或陈述语句,'f' 里面必须要有语句。按 python 缩进的方式组合成合法的程序,问有多少种可能方案。
tags: dp
dp[i][j] 表示第 i 个语句缩进为 j 时的可能方案数, 转移:
1】 如果第 i 个是 'f' , 则第 i+1 个肯定要比第 i 个多缩进一个单位,即 dp[i+1][j+1] = dp[i][j]。
2】如果第 i 个是 's' ,则第 i+1 个可以属于前面任何一个 for 循环,也就是说第 i+1 个的缩进要 <= 第 i 个的缩进,即 dp[i+1][j] = dp[i][k], j<=k<=n 。
复杂度 O(n^2)。
1 #include2 #include 3 #define N 5005 4 typedef long long int ll; 5 #define mod 1000000007 6 using namespace std; 7 int n; 8 char c; 9 ll dp[N][N]; 10 int main(){ 11 ios::sync_with_stdio(false); 12 cin.tie(0); 13 cin>>n; 14 memset(dp,0,sizeof(dp)); 15 dp[1][0]=1; 16 for(int i = 1;i < n;i++){ 17 cin>>c; 18 if(c == 'f'){ 19 for(int j=0;j<=n;j++){ 20 dp[i+1][j+1] = dp[i][j]; 21 } 22 }else{ 23 ll sum = 0; 24 for(int j = n;j >= 0;j--){ 25 sum += dp[i][j]; 26 sum %= mod; 27 dp[i+1][j] = sum; 28 } 29 } 30 } 31 cin>>c; 32 ll ans = 0; 33 for(int i=0;i<=n;i++){ 34 ans += dp[n][i]; 35 ans%=mod; 36 } 37 cout< endl; 38 return 0; 39 }