YBTOJ 2.1字符串处理

A.数字反转

image
image

记得特判第一位是负号的情况 然后倒着输出就行了

点击查看代码
#include <bits/stdc++.h>
using namespace std;

const int N = 0721;
char s[N];

int main() {
	
	char c;
	scanf("%c",&c);
	
	if (c == '-') {
		scanf("%s",s);
		printf("-");
		int len = strlen(s);
		bool flag = 1;
		for (int i = len - 1; i >= 0; --i) {
			if (flag && s[i] == '0' && i != 0)
				continue;
			flag = 0;
			printf("%c",s[i]);
		}
	}
	else {
		s[0] = c;
		scanf("%s",s + 1);
		int len = strlen(s);
		bool flag = 1;
		for (int i = len - 1; i >= 0; --i) {
			if (flag && s[i] == '0' && i != 0)
				continue;
			flag = 0;
			printf("%c",s[i]);
		}
	}
	
	
	
	return 0;
}

B.移位包含

image
image

直接使用使用 \(STL\) 暴力草过()
如果不用 \(STL\)\(KMP\) 或者暴力匹配也可以 反正数据范围不大

点击查看代码
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
const int N = 31;
char s1[N], s2[N], s1_[N];
int main() {
    cin >> s1 >> s2;
    int len = strlen(s1);
    int len_ = strlen(s2);
    memcpy(s1_, s1, sizeof(s1));

    if (strstr(s1, s2)) {
        printf("true");
        return 0;
    }

    for (int i = 1; i <= len; i++) {
        s1[len] = s1[0];
        for (int j = 0; j < len; j++) s1[j] = s1[j + 1];
        s1[len] = '\0';
        if (strstr(s1, s2)) {
            printf("true");
            return 0;
        }
    }

    if (strstr(s2, s1_)) {
        printf("true");
        return 0;
    }

    for (int i = 1; i <= len_; i++) {
        s2[len_] = s2[0];
        for (int j = 0; j < len_; j++) s2[j] = s2[j + 1];
        s2[len_] = '\0';
        if (strstr(s2, s1_)) {
            printf("true");
            return 0;
        }
    }

    printf("false");

    return 0;
}

C.单词替换

image
image

使用 \(cin\)\(scanf\) 的各种玄学字符串输入 直接过去了(

点击查看代码
#include <bits/stdc++.h>
using namespace std;

const int N = 521;
string s[N], bth, th;
int cnt;

int main() {
    while (1) {
        ++cnt;
        cin >> s[cnt];
        char c;
        scanf("%c", &c);
        if (c != ' ') break;
    }

    cin >> bth;
    cin >> th;
    for (int i = 1; i <= cnt; ++i) {
        if (s[i] == bth) cout << th;
        else cout << s[i];
        cout << ' ';
    }

    return 0;
}

D.字符串环

image
image

非常谔谔的一道题
我们将每个字符串复制一份接在后面模拟环
然后暴力匹配

点击查看代码
#include <bits/stdc++.h>
using namespace std;
int main() {
    string a, b;
    int ans = 0, tmp = 0, x;
    cin >> a >> b;
    int lena = a.length();
    int lenb = b.length();
    a += a;
    b += b;
    x = min(lena, lenb);
    ans = 0;
    for (int i = 0; i < lena; i++) {
        for (int j = 0; j < lenb; j++) {
            while (a[i + tmp] == b[j + tmp] && tmp < x) tmp++;
            ans = max(ans, tmp);
            tmp = 0;
        }
    }

    printf("%d\n", ans);

    return 0;
}

E.生日相同

image
image

使用 \(string\) 自带的比较大小就能过
但是注意 \(string\) 在比较两个长度不同的字符串的时候似乎会出锅 所以建议手动特判下

点击查看代码
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 0721;
struct node {
    int m, d;
    string name;
    friend bool operator<(node x, node y) {
        if (x.m != y.m) return x.m < y.m;
        else if (x.d != y.d) return x.d < y.d;
        else if (x.name.size() != y.name.size()) return x.name.size() < y.name.size();
        else return x.name < y.name;
    }
} a[N];
int n;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].name;
        scanf("%d%d", &a[i].m, &a[i].d);
    }
    sort(a + 1, a + 1 + n);

    int lm = a[1].m, ld = a[1].d;
    printf("%d %d ", lm, ld);
    cout << a[1].name << " ";
    for (int i = 2; i <= n; ++i) {
        if (a[i].m == lm && a[i].d == ld) cout << a[i].name << " ";
        else {
            printf("\n");
            lm = a[i].m, ld = a[i].d;
            printf("%d %d ", lm, ld);
            cout << a[i].name << " ";
        }
    }

    return 0;
}
posted @ 2023-06-28 15:55  Steven24  阅读(67)  评论(0编辑  收藏  举报