Contest3898 - 计科23级算法设计与分析平时作业-01

题目链接

A.DNA Sorting

题面

思路

题目意思就是说,如果一个字符串中前面的字符比后面的字符大,那么它的无序度就+1,根据这个给一组字符串从最有序到最无序依次输出。那么明白题目意思之后直接模拟即可。

示例代码

#include<bits/stdc++.h>

using namespace std;

#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'

const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<pair<int, string>> arr(m);
    fer(i, 0, m) cin >> arr[i].second;

    for(auto &p : arr){
        string s = p.second;
        fer(i, 0, s.size()){
            fer(j, i + 1, s.size()) p.first += (s[i] > s[j]);
        }
    }
    sort(all(arr));
    for(auto p : arr) cout << p.second << '\n';
    return 0;
}

B.Integer Inquiry

题面

思路

其实就是求多个数的高精度加法

示例代码

ans = 0
while True:
    n = int(input())
    if n == 0:
        break
    ans += n
print(ans)
#include<bits/stdc++.h>

using namespace std;

#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'

const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;

string add(string s1, string s2) {
    string res;
    vector<int> a(110), b(110), c(110);
    int n1 = s1.size(), n2 = s2.size();

    for(int i = 0, j = n1 - 1; i < n1; i++, j--) a[i] = s1[j] - '0';
    for(int i = 0, j = n2 - 1; i < n2; i++, j--) b[i] = s2[j] - '0';

    fer(i, 0, max(n1, n2)){
        c[i] += a[i] + b[i];
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
    }
    int len = max(n1, n2) + (c[max(n1, n2)] != 0);
    ferd(i, len - 1, 0) res += c[i] + '0';
    return res;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);

    string s;
    string ans = "";
    while(cin >> s, s != "0"){
        ans = add(ans, s);
    }
    cout << ans << '\n';
    return 0;
}

C.Word Amalgamation

题面

思路

题意是说先输入一组词典,词典中都是正确的单词,然后输入一组乱序的词,这些乱序的词可以经过重新将字母排序得到词典中的单词(或者词典中不存在)。如果存在就依次输出乱序单词对应的正确单词,不存在则另输出。
思路:我们只需要比较乱序单词和词典中的单词中的各字母的频次即可。

示例代码

#include<bits/stdc++.h>

using namespace std;

#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'

const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);

    string s;
    map<string, map<int, int>> mp1;
    map<string, map<int, int>> mp2;
    
    while(cin >> s, s != "XXXXXX"){
        for(auto c : s) mp1[s][c - 'a']++;
    }
    vector<string> arr;
    while(cin >> s, s != "XXXXXX"){
        arr.push_back(s);
        for(auto c : s) mp2[s][c - 'a']++;
    }
    for(auto x : arr){
        bool ok = false;
        for(auto [y, mp] : mp1){
            if(mp1[y] == mp2[x]){
                ok = true;
                cout << y << '\n';
            }
        }
        if(!ok) cout << "NOT A VALID WORD\n";
        cout << "******\n";
    }

    return 0;
}

D.Sorting It All Out

题面

思路

示例代码

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN=2500;
int n,m,cas,id;

struct edge
{
    int v,nx;
}set[MAXN];
int head[30],d[30],ok[30],write[30];
queue<int> Q;

void Addedge(int u,int v)
{
    id++;set[id].v=v;set[id].nx=head[u];
    head[u]=id;
}

int bfs()
{
    cas=0;
    while(!Q.empty())Q.pop();
    int out=1,t,u;t=0;
    for(int i=1;i<=n;i++)
        if(!ok[i])
        {
            t++;Q.push(i);
        }
    if(t>1)out=0;
    while(!Q.empty())
    {
        u=Q.front();Q.pop();t=0;write[++cas]=u;
        for(int k=head[u];k>0;k=set[k].nx)
        {
            ok[set[k].v]--;
            if(!ok[set[k].v])
            {
                t++;Q.push(set[k].v);
            }
        }
        if(t>1)out=0;
    }
    for(int i=1;i<=n;i++)if(ok[i]>0){out=-1;break;}
    return out;
}

char print()
{
    for(int i=1;i<=cas;i++)printf("%c",(char)(write[i]+64));
    return '.';
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(write,0,sizeof(write));
        memset(d,0,sizeof(d));
        memset(head,-1,sizeof(head));
        id=0;
        int now=0,loca;
        char a,b,c;
        if(n==0 && m==0)break;
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b>>c;
            if(b=='<')
            {
                Addedge(a-64,c-64);d[c-64]++;
            }
            else
            {
                Addedge(c-64,a-64);d[a-64]++;
            }
            if(now==0)
            {
                for(int j=1;j<=n;j++)ok[j]=d[j];
                now=bfs();loca=i;
            }
        }
        if(now==0)puts("Sorted sequence cannot be determined.");
        else if(now==1)printf("Sorted sequence determined after %d relations: ",loca),printf("%c\n",print());
        else printf("Inconsistency found after %d relations.\n",loca);
    }
    return 0;
}

E.Cable TV Network

题面

示例代码

#include<bits/stdc++.h>

using namespace std;

#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'

const int MOD = 1e9 + 7;
const int inf = 1e9;


const int N = 105, M = 10005;
int n, m, T, head[N], ver[M], nxt[M], edge[M], tot, que[N], l, r, s, t, level[N], ans;

void init() {
	tot = 1;
	memset(head, 0, sizeof(head));
}

bool bfs() {
	memset(level, 0, sizeof(level));
	l = r = 1, que[l] = s, level[s] = 1;
	while (l <= r) {
		int cur = que[l++];
		for (int i = head[cur]; i; i = nxt[i])
			if (edge[i] && !level[ver[i]]) {
				level[ver[i]] = level[cur] + 1, que[++r] = ver[i];
				if (ver[i] == t)
					return true;
			}
	}
	return false;
}

int dinic(int cur, int increase) {
	if (cur == t)
		return increase;
	int rest = increase;
	for (int i = head[cur]; i; i = nxt[i])
		if (edge[i] && level[ver[i]] == level[cur] + 1) {
			int inc = dinic(ver[i], min(rest, edge[i]));
			if (inc == 0) {
				level[ver[i]] = 0;
				continue;
			}
			rest -= inc, edge[i] -= inc, edge[i ^ 1] += inc;
		}
	return increase - rest;
}

int Dinic() {
	int maxflow = 0, tmp;
	while (bfs())
		do {
			tmp = dinic(s, 0x3f3f3f3f);
			maxflow += tmp;
			if (maxflow >= ans)
				return n;
		} while (tmp);
	return maxflow;
}

void restore() {
	for (int i = 1; i <= n + n; i++)
		for (int j = head[i]; j; j = nxt[j])
			if (!(j & 1))
				edge[j] += edge[j ^ 1], edge[j ^ 1] = 0;
}

void addedge(int u, int v, int w) {
	ver[++tot] = v, nxt[tot] = head[u], head[u] = tot, edge[tot] = w;
	ver[++tot] = u, nxt[tot] = head[v], head[v] = tot, edge[tot] = 0;
}

int main() {
	while (cin >> n >> m) {
		init();
		while (m--) {
			int x, y;
			scanf(" (%d,%d)", &x, &y);
			x++, y++;
			addedge(x, y + n, 0x3f3f3f3f), addedge(y, x + n, 0x3f3f3f3f);
		}
		for (int i = 1; i <= n; i++) addedge(i + n, i, 1);
		ans = n;
		for (s = 1; s <= n; s++)
			for (t = n + 1; t < n + s; t++) {
				restore();
				ans = min(ans, Dinic());
			}
		cout << ans << endl;
	}
	return 0;
}
posted @ 2025-03-15 15:34  Thin_time  阅读(10)  评论(0)    收藏  举报