先上题目,再上个人解法

16.(编程一)
给定一个字符串,输出所有长度至少为2的回文子串。回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:
aa、aba、abba、cccdeedccc都是回文字符串。
输入格式
一个字符串,由字母或数字组成。长度500以内。
输出格式:
输出所有的回文子串,每个子串一行
子串长度小的优先输出,若长度相等,则按照其在输入字符串中的出现位置靠左的优先输出。
输入样例1:
123321125775165561
输入样例2:
abbacddc123a321

输出样例1:
33

11

77

55
2332

2112

5775

6556
123321
165561
输出样例2:
bb
dd

3a3

abba

cddc
23a32
123a321

17.(编程二)

编写程序,从键盘上输入两个正整数,较大的数为m和较小的数为n,根据以下公式求P的值,并输出。(m和n为大于0且小于等于30的整数)
P =m!/(n! (m - n)!)
输入样例1:
12  8
输入样例2:
27  30
输出样例1:
495
输出样例2:
4060

18.(编程三)

小明玩网络游戏,现在总共有n个天赋点,需要分配给m项技能(0..i...m),其中第项技能所需的天赋点为ai,能够增加ei的战斗力。若是每项技能仅允许被加点一次,请问小明使用这n个天赋点,能够增加的最多的战斗力是?
输入格式:
第一行输入两个数字n和m,以空格间隔 (1<=n,m<=1000)之后m行依次输出第i项技能所需要的天赋点数和能够增加的战斗力,以空格间隔

输出格式:
输出小明增加的最多的战斗力
(1<=ai,ei<=1000)
输入样例1:
8 3
12 6
11 8
68
输入样例2:
6 3
2 10
4 5
1 4
输出样例1:
8
输出样例2:
15

19.(编程四)

某个交通网络为无向图,共有n个节点,m条边,节点编号从1开始。小明初始时在1号节点,他需要将物资一次性运送到n号节点。无向图的每条边有两个权值w和v,分别表示该边最多能通过的物资的重量和通过该边需要上缴的过路费。一旦小明运送的物资重量超过w或者小明剩余的资金不足v,都不能通过该边.现在小明的总预算为c,请问他最多能一次性运送多少物资从1号点到n号点?
(不必考虑自环与重边)
输入格式:
第一行依次输入三个数字n,m,c分别表示点数、边数和小明的总预算
(1<=ne=100,1<=m<=5001e=c<=100)接下来m行每行输入4个正整数秀x、y、w、v,空格间隔开,分别表示节点x到y有一条重为w过路费为v的边。
(1<=w,ve=100)

输出格式:
小明一次性最多能运送的物资重量
输入样例1:
5 6 5
1 2 3 6
1 5 2 1
1 3 2 4
2 4 5 5

3 5 8 3
4 5 6 1
输入样例2:
6 7 40
1 2 30 7
1 3 50 40
2 3 25 6
3 4 32 12
3 5 40 15
4 5 60 18
5 6 35 12

输出样例1:
2
输出样例2:
25

发一下自己的代码:
//题目16 #include <bits/stdc++.h> using namespace std; map<string, int> map1;
void extend(string &s, int i, int j, vector<string> &ans) { int res = 0; while
(i >= 0 && j < s.size() && s[i] == s[j]) { if (j > i) {
ans.push_back(s.substr(i, j - i + 1)); map1[s.substr(i, j - i + 1)] = i; }
res++; i--; j++; } } void countSubstrings(string s, vector<string> &ans) { int
n = s.size(); for (int i = 0; i < n; i++) { extend(s, i, i, ans); extend(s, i,
i + 1, ans); } } static bool cmp(string &a, string &b) { if (a.size() ==
b.size()) { return map1[a] < map1[b]; } else if (a.size() < b.size()) { return
true; } else { return false; } } int main() { string str; cin >> str;
vector<string> ans; // cout<<ans.size()<<endl; countSubstrings(str, ans);
sort(ans.begin(), ans.end(), cmp); for (int i = 0; i < ans.size(); i++) { cout
<< ans[i] << endl; } return 0; } //题目17 #include <bits/sdc++.h> using namespace
std; long long fac(int x) { register long long i,f=1; //定义寄存器变量
for(i=1;i<=x;i++) f*=i; return f; } int main(){ int a,b; cin>>a>>b; int n,m;
if(a<b){ n=a; m=b; }else{ n=b; m=a; } long long p=fac(m)/(fac(n)*fac(m-n));
cout<<p<<endl; return 0; } //题目18 #include <iostream> #include <algorithm>
using namespace std; const int N = 1010; int n, m; int a[N], e[N]; int f[N];
int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i] >> e[i];
} for (int i = 1; i <= m; i++) { for (int j = n; j >= a[i]; j--) { f[j] =
max(f[j], f[j - a[i]] + e[i]); } } cout << f[n] << endl; return 0; } //题目19
#include <iostream> #include <cstring> #include <algorithm> #include <queue>
using namespace std; const int N = 110, M = 510; int n, m, c; struct Edge { int
w, v, next; } edges[M]; int head[N], cnt; void add(int a, int b, int w, int v)
{ edges[cnt].w = w, edges[cnt].v = v; edges[cnt].next = head[a], head[a] = cnt
++ ; } struct Node { int v, w, c; bool operator < (const Node& t) const {
return v > t.v; } }; int dis[N]; bool st[N]; void dijkstra() { memset(dis,
0x3f, sizeof dis); dis[1] = 0; priority_queue<Node> q; q.push({0, 0, c}); while
(q.size()) { Node t = q.top(); q.pop(); int u = t.w; if (st[u]) continue; st[u]
= true; for (int i = head[u]; i != -1; i = edges[i].next) { int j = edges[i].v;
int w = edges[i].w; if (dis[j] > t.v + w) { dis[j] = t.v + w; q.push({dis[j],
j, t.c}); } } } } int main() { memset(head, -1, sizeof head); cin >> n >> m >>
c; while (m -- ) { int a, b, w, v; cin >> a >> b >> w >> v; add(a, b, w, v),
add(b, a, w, v); } dijkstra(); int res = 0; for (int i = head[1]; i != -1; i =
edges[i].next) { int j = edges[i].v; if (dis[j] < dis[n]) { int w = edges[i].w;
res += w; c -= edges[i].v; if (c < 0) break; } } cout << res << endl; return 0;
}

技术
今日推荐
PPT
阅读数 99
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信