目录
1. 交错字符串 🌟🌟
2. 最短回文串 🌟🌟
3. 分段函数计算 ※
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
1. 交错字符串
给定三个字符串 s1
、s2
、s3
,请你帮忙验证 s3
是否是由 s1
和 s2
交错 组成的。
两个字符串 s
和 t
交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
-
交错 是
s1 + t1 + s2 + t2 + s3 + t3 + ...
或者t1 + s1 + t2 + s2 + t3 + s3 + ...
提示:a + b
意味着字符串 a
和 b
连接。
示例 1:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" 输出:true
示例 2:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" 输出:false
示例 3:
输入:s1 = "", s2 = "", s3 = "" 输出:true
提示:
0 <= s1.length, s2.length <= 100
0 <= s3.length <= 200
-
s1
、s2
、和s3
都由小写英文字母组成
以下程序实现了这一功能,请你填补空白处内容:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isInterleave(char *s1, char *s2, char *s3)
{
int i, j;
int len1 = strlen(s1);
int len2 = strlen(s2);
int len3 = strlen(s3);
if (len1 + len2 != len3)
{
return false;
}
bool *table = malloc((len1 + 1) * (len2 + 1) * sizeof(bool));
bool **dp = malloc((len1 + 1) * sizeof(bool *));
for (i = 0; i < len1 + 1; i++)
{
dp[i] = &table[i * (len2 + 1)];
}
dp[0][0] = true;
for (i = 1; i < len1 + 1; i++)
{
dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];
}
for (i = 1; i < len2 + 1; i++)
{
____________________;
}
for (i = 1; i < len1 + 1; i++)
{
for (j = 1; j < len2 + 1; j++)
{
bool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1];
bool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1];
dp[i][j] = up || left;
}
}
return dp[len1][len2];
}
int main(int argc, char **argv)
{
if (argc != 4)
{
fprintf(stderr, "Usage: ./test s1 s2 s3\n");
exit(-1);
}
printf("%s\n", isInterleave(argv[1], argv[2], argv[3]) ? "true" : "false");
return 0;
}
```
出处:
https://edu.csdn.net/practice/25658351
代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isInterleave(char *s1, char *s2, char *s3)
{
int i, j;
int len1 = strlen(s1);
int len2 = strlen(s2);
int len3 = strlen(s3);
if (len1 + len2 != len3)
{
return false;
}
bool *table = (bool*)malloc((len1 + 1) * (len2 + 1) * sizeof(bool));
bool **dp = (bool**)malloc((len1 + 1) * sizeof(bool *));
for (i = 0; i < len1 + 1; i++)
{
dp[i] = &table[i * (len2 + 1)];
}
dp[0][0] = true;
for (i = 1; i < len1 + 1; i++)
{
dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];
}
for (i = 1; i < len2 + 1; i++)
{
dp[0][i] = dp[0][i - 1] && s2[i - 1] == s3[i - 1];
}
for (i = 1; i < len1 + 1; i++)
{
for (j = 1; j < len2 + 1; j++)
{
bool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1];
bool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1];
dp[i][j] = up || left;
}
}
return dp[len1][len2];
}
int main()
{
char *s1 = (char*)"aabcc";
char *s2 = (char*)"dbbca";
char *s3 = (char*)"aadbbcbcac";
printf(isInterleave(s1,s2,s3) ? "true" : "false");
return 0;
}
输出:
true
2. 最短回文串
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
示例 1:
输入:s = "aacecaaa" 输出:"aaacecaaa"
示例 2:
输入:s = "abcd" 输出:"dcbabcd"
提示:
0 <= s.length <= 5 * 10^4
-
s
仅由小写英文字母组成
出处:
https://edu.csdn.net/practice/25658352
代码:
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
string shortestPalindrome(string s)
{
string rev(s);
reverse(rev.begin(), rev.end());
string combine = s + "#" + rev;
vector<int> lps(combine.length(), 0);
int remove = getLPS(combine, lps);
string prepend = rev.substr(0, rev.length() - remove);
return prepend + s;
}
int getLPS(string s, vector<int> &lps)
{
int j = 0, i = 1;
while (i < s.length())
{
if (s[i] == s[j])
{
lps[i] = j + 1;
i++;
j++;
}
else
{
if (j != 0)
{
j = lps[j - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
return lps[lps.size() - 1];
}
};
int main()
{
Solution s;
cout << s.shortestPalindrome("aacecaaa") << endl;
cout << s.shortestPalindrome("abcd") << endl;
return 0;
}
输出:
aaacecaaa
dcbabcd
3. 分段函数计算
编程输入实数x,计算下面函数的值,并输出y的值,并输出y的值;
x^2 x<1
x-1 1≦x≦10
x/5 x>10
出处:
https://edu.csdn.net/practice/25658353
代码:
# include<stdio.h>
# include<stdlib.h>
int main(void)
{
float x,y;
printf("请输入x的值:\n");
scanf("%f",&x);
if(x<1)
{
y = x * x;
}
else if(x<=10)
{
y=3*x-1;
}
else
{
y= x / 5;
}
printf("y的值为:%f\n",y);
system("pause");
return 0;
}
输出:
略
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富! 文章来源:https://www.toymoban.com/news/detail-414028.html
☸ 主页:https://hannyang.blog.csdn.net/文章来源地址https://www.toymoban.com/news/detail-414028.html
Golang每日一练 专栏 |
|
Python每日一练 专栏 |
|
C/C++每日一练 专栏 |
|
Java每日一练 专栏 |
到了这里,关于C/C++每日一练(20230415)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!