天梯赛模拟赛4(补题)

7-1 吃火锅


这道题考查stl中的find函数,不嫌麻烦的话可以手写kmp
如果在主串中没有子串,则返回-1,否则返回第一个子串的位置

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string a;
    int cnt = 0, res = 0, t;
    string b = "chi1 huo3 guo1";
    while(getline(cin, a))
    {
        if(a == ".") break;
        cnt++;
        if(a.find(b) != -1 && res == 0)
            t = cnt;
        if(a.find(b) != -1)
            res++;
        
    }
    cout << cnt << endl;
    if(res == 0)
        cout << "-_-#\n";
    else
        cout << t << ' ' << res << endl;
    return 0;
}

前世档案


这道题一开始以为要建树,但是最后一层的节点数有\(2^{30}\)个节点,不可能建树,比赛中只得了13分
看了题解才知道用二分的思想来做

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n, m;
    string a;
    cin >> n >> m;
    int nn = pow(2, n);
    while(m--)
    {
        cin >> a;
        int l = 1, r = nn;
        for(int i = 0; i < a.size(); i++)
            if(a[i] == 'y')
                r = l + (r - l) / 2;
            else 
                l = r - (r - l) / 2;
        cout << l << endl;
    }
    return 0;
}
posted @ 2022-04-07 23:39  Flying_bullet  阅读(30)  评论(0)    收藏  举报