zoukankan      html  css  js  c++  java
  • 7.24补前日


    今天还阔以


    链接:
    B题:
    题意:有一个字符串,里面可能有很多的happiness的单词,我们只能进行一次操作交换两个字母,要求将所有的happiness都删除掉,保证有解,输出交换的两个字母的位置

    解法:
    统计串中出现的happiness的数目,如果大于等于2的话就是无解的;
    之后,当字符串的长度小于9的时候,直接交换前两个就可以
    之后:
    1. 当有两个happiness的时候,我们交换第一个的h和第二个的i就行
    2. 当有一个的时候,我们交换第一个的h和i
    3. 当没有的时候,用一个map记录每个字母的位置,之后当遇到相同的时候,我们就交换这两个字母的下标(这样子相当于没有交换)

    #include<cstdio>
    #include<iostream>
    #include<string>
    #include<map>
    using namespace std;
    const int maxn = 2e5 + 5;
    int h[maxn], k[maxn], id = 1;
    string hap = "happiness";
    string s;
    map<char, int>mp;
    
    int main() {
        cin >> s;
        if (s.size() < 9) { cout << "YES" << endl << "1 2" << endl; return 0; }
        int sum = 0;
        for (int i = 0; i < s.size() - 8; i++) {
            if(hap==s.substr(i,9)){
                sum++;
                h[id++] = i;
            }
        }
        if (sum > 2) { cout << "NO" << endl;}
        else if (sum == 2) {
            cout << "YES" << endl;
            cout << h[1] + 1 << " " << h[2] + 5 << endl;
        }
        else if (sum == 1) {
            cout << "YES" << endl;
            cout << h[1] + 1 << " " << h[1] + 5;
        }
        else if (sum == 0) {
            cout << "YES" << endl;
            int left = 1, right = 2;
            mp.clear();
            for (int i = 0; i < s.length(); i++){
                if (mp[s[i]] == 0)mp[s[i]] = i + 1;
                else {
                    left = mp[s[i]];
                    right = i + 1;
                    break;
                }
            }
            cout << left << " " << right << endl;
        }
        return 0;
    }

    C:
    题意:有一个袋子,里面有a个红球,b个绿球,c个不知道颜色的球,问最多拿几个球可以保证拿出的红球不超过n个,绿球不超过m个

    解法:
    首先,当(a+c)> n 或者(b+c)> m 的时候有风险,所有我们的目标就是要让两个都没有风险,这样子就是取(a+c)和(b+c)的最小值,初始状态是(a+b+c),即三个球都要取。

    #include<cstdio>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    
    int main() {
        ll a, b, c;
        ll n, m;
        scanf("%lld%lld%lld", &a, &b, &c);
        ll ans = a + b + c;
        scanf("%lld%lld", &n, &m);
        if (a + c >= n+1)ans = ans < n ? ans : n;
        if (b + c >= m+1)ans = ans < m ? ans : m;
        printf("%lld
    ", ans);
        return 0;
    }

    D题:
    题意:有一只青蛙位于原点,之后他每次可以朝任意一个方向跳,跳的距离只能是a序列中的一个,给定一个中止坐标,问能不能跳到那个点

    解法:
    欧几里得定理(笑。
    对于方程:Ax1+Bx2+Cx3…+Nxn=res;这个方程有整数解的条件是res可以整除x所有系数的最大公约数。
    对于题目来说,有序每次的距离可以自己选,所有就相当于把上式中的A,B,C,D都换成数列中相对应的步子,最后问你有没有解罢了。

    #include<cstdio>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    const int maxn = 2e5 + 5;
    ll n, x, a[maxn];
    ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); }
    
    int main() {
        scanf("%lld%lld", &n, &x);
        for (int i = 0; i < n; i++) {
            scanf("%lld", &a[i]);
            if (a[i] < 0)a[i] = 0 - a[i];
        }
        ll ans = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] == 0)continue;
            ans = gcd(ans, a[i]);
        }
        if (x%ans == 0)printf("YES
    ");
        else printf("NO
    ");
        return 0;
    }

    G题:(求数组中一个元素上下左右四个区域的最大值分别是哪几个)
    题意,给定一个数组,要求删去任意一行和一列,要求剩下的元素中最大的值最小

    解法:算出每一个元素的↖↙↗↘四个方向的最大元素,之后枚举即可

    #include <iostream>
    #include<algorithm>
    using namespace std;
    const int maxn = 1002;
    int n, m, ans = 1e9 + 7;
    int a[maxn][maxn], l[maxn][maxn], r[maxn][maxn];
    int lb[maxn][maxn], ld[maxn][maxn], ru[maxn][maxn], rd[maxn][maxn];
    //左上,右上,左下,右下
    
    void process_1() {
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                l[i][j] = max(l[i][j - 1], a[i][j]);
            }
            for (int j = m; j >= 1; j--){
                r[i][j] = max(r[i][j + 1], a[i][j]);
            }
        }
        return;
    }
    
    void process_2() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                lb[i][j] = max(l[i][j], lb[i - 1][j]);
                ru[i][j] = max(r[i][j], ru[i - 1][j]);  
                //cout << lb[i][j] << " " << rb[i][j] << "#";
            }
            //cout<<endl;
        }
    
        for (int i = n; i >= 1; i--){
            for (int j = 1; j <= m; j++){
                ld[i][j] = max(l[i][j], ld[i + 1][j]);
                rd[i][j] = max(r[i][j], rd[i + 1][j]);
                //cout << lt[i][j] << " " << rt[i][j] << "#";
            }
            //cout << endl;
        }
        return;
    }
    int main()
    {
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                scanf("%d", &a[i][j]);
            }
        }
        process_1();
        process_2();
        int cx = -1, cy = -1;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                int t1 = max(lb[i - 1][j - 1], ru[i - 1][j + 1]);
                int t2 = max(ld[i + 1][j - 1], rd[i + 1][j + 1]);
                int t3 = max(t1, t2);
                if (t3 < ans){
                    cx = i, cy = j;
                    ans = t3;
                }
            }
        }
        printf("%d %d
    ", cx, cy);
        return 0;
    }
    

    Uva1610
    题意:给定一个元素的集合,要求一个串,这个串大于等于集合中一半的元素,小于另一半的元素,要求元素的串的长度尽可能的小,如果有多解,输出字典序最小的解

    解法:
    没想到还能这么往出算这个串。。。直接暴力枚举啊。。

    #include<cstdio>
    #include<iostream>
    #include<set>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef set<string>::iterator si;
    set<string>s;
    string tmp, ans;
    
    int main() {
        int n;
        while (scanf("%d", &n) && n) {
            ans.clear(); s.clear();
            for (int i = 0; i < n; i++) {
                cin >> tmp;
                s.insert(tmp);
            }
            si pre, nex;
            pre = nex = s.begin();
            advance(pre, s.size() / 2-1);
            advance(nex, s.size() / 2);
            string s1 = *pre, s2 = *nex;
            string s0;
            int cur = 0;
            while (1) {
                for (int i = 0; i < 26; i++) {
                    ans = s0;
                    ans += i + 'A';//不会累加,会将s0重新划归为s0
                    if (ans >= s1&&ans < s2)
                        goto END;
                }
                s0 += s1[cur++];
            }
        END:
            cout << ans << endl;
        }
        return 0;
    }

    Uva12545
    题意:给当两个串,有三种操作,一次是将一位上的0换成1,一次是将?换成任意一个数字,最后一个是任意交换两个数字,问最少需要多少次操作可以将第一个串变成第二个串的的样子

    解法:
    这道题没有写出来,参考了已有的写法,不过非常值得学习!

    #include<cstdio>
    #include<iostream>
    #include<string>
    using namespace std;
    string a, b;
    
    int main() {
        int T; scanf("%d", &T);
        int kase = 0;
        while (T--) {
            cin >> a >> b;
            int ans = 0, na1=0,nb1=0;
            string na, nb;
            for (int i = 0; i < a.size(); i++) {
                if (a[i] == b[i])continue;
                na.push_back(a[i]); nb.push_back(b[i]);
    
                if (a[i] == '1')na1 += 1;
                if (b[i] == '1')nb1 += 1;
    
            }
            if (na1 > nb1) { cout << "Case " << ++kase << ": " << -1 << endl; continue; }
            for (int i = 0; i < a.size(); i++) {
                if (a[i] != b[i] && a[i] != '?') {
                    for (int j = i + 1; j < a.size(); j++) {
                        if (a[i] == b[j] && b[i] == a[j]) {
                            swap(a[i], a[j]); ans++; 
                            break;
                        }
                    }
                }
            }
            //首先所有最有交换已经处理完了
            //这一步的关键是如果上面为1下面为0的话,交换的话需要+1
            //如果上面为0下面为1的话,如果交换需要+1,如果变换需要+1
            //如果上面为‘?’的话,变化需要+1,之后的交换上面都考虑到了
            //很精妙
            for (int i = 0; i < a.size(); i++) {
                if (a[i] != b[i])ans++;
            }
            cout << "Case " << ++kase << ": " << ans << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    Linux磁盘管理
    Linux系统中的计划任务与压缩归档简介------If you shed tears when you miss the sun, you also miss the stars.
    Linux系统之权限管理(所有者,所属组,其他;r,w,x;特殊权限)------If you shed tears when you miss the sun, you also miss the stars.
    linuxx系统中高级命令简介------If you shed tears when you miss the sun, you also miss the stars.
    IE11新特性 -- Internet Explorer 11:请不要再叫我IE
    SQL WHILE 循环中的游标 用例,SQL中实现循环操作
    group_concat函数使用
    Git 初始化配置
    WCF 、Web API 、 WCF REST 和 Web Service 的区别
    js 解析 json
  • 原文地址:https://www.cnblogs.com/romaLzhih/p/9489803.html
Copyright © 2011-2022 走看看