A;
求前缀和 和面单词里的字典序最小的;
【思路】
暴力, 数据小,直接暴力所有情况
【代码实现】
#include
#include
#include
#include
typedef long long ll;
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
vector ans;
ans.clear();
for(int i=1;i<=a.length();i++)
{
for(int j=1;j<=b.length();j++)
{
string temp=a.substr(0,i)+b.substr(0,j);
//cout<
B;
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
123
【思路】
这道题是一道数学规律题,求最小的步数, 题意并没有给出具体的情况,只有
1->1 ,2->3 , 3->4, 4->6,
推,5->9 , 找规律 a[n]= 2a[n-1]-2a[n-3]+a[n-4]
【代码实现】
#include
#include
#include
#include
typedef long long ll;
using namespace std;
int ans[120];
int main()
{
int n;
ans[0]=0;
ans[1]=1;
ans[2]=2;
ans[3]=4;
ans[4]=6;
for(int i=5;i<=100;i++)
{
ans[i]=2*ans[i-1]-2*ans[i-3]+ans[i-4];
// cout<>n;
cout<
C
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
12
【题意】
python 没有缩进
现在给n个字符; f 代表是for循环 ,s代表是单语句。 f后面不能为空, f , s 缩进表示的形式有多少种
【思路】
DP
把表格写出了, 横为 缩进的个数0,1,2,3,4,5,6,
拿 ffsssfs 示例
0 1 2 3 4 5 6 7 8 9
1 f 1
2 f 0 1
3 s 0 0 1
4 s 1 1 1
5 s 3 2 1
6 f 6 3 1
7 s 0 6 3 1
答案为 0+6+3+1=10;
对于每行 如果上一个是s 可以发现, 上一个的后缀和
如果不是s 那么dp[i][j] = dp[i-1][j-1]
【代码实现】
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))
#define SHUT std::ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int MAXN=1e5+5;
const int MOD=1e9+7;
char a[MAXN];
int dp[5050][5050];
int main()
{
SHUT;
int n;
cin>>n;
mem(a,0);
vectorV[MAXN];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
if(a[i-1]=='s')
{
ll now=0;
for(int j=5050;j>=1;j--)
{
now=(now+dp[i-1][j])%MOD;
dp[i][j]=now;
}
}
else
{
for(int j=5050;j>=1;j--)
{
dp[i][j]=dp[i-1][j-1]%MOD;
}
}
}
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=10;j++)
{
cout<