Codeforces educational round #67 div 2

A

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long

int T;
ll n, s, t;

int main()
{
    cin >> T;
    while(T)
    {
        cin >> n >> s >> t;
        cout << n - min(s, t) + 1 << endl;
        T--;
    }
    return 0;
}

B

自己做的时候考虑到了:
①最终取的长度一定等于ti字符串中满足数量时在s中最靠后的那个字母在s中的位置。由此可以遍历ti,ans = max(ans, xxx)
②因为确定一定是26个小写字母组成,所以可以用a[‘s[i]’]的形式存储,简洁明了

但是没有想出xxx的表达式,也没想出具体怎么用数组存储。当时的想法是开个二维向量,con[i][j],i表示位数,j表示字母,觉得太复杂了。但是其实不用这样,不需要记录“s每一位是哪个字母,是第几个这个字母”,只要把这个字母出现的所有位置记录就好(所以遍历一遍就可以了)。
对于ti的处理,不需要精确到每一位是哪个字母。只要知道26个字母中都有哪些,每一个都需要多少个即可。之后对照上面步骤,找出最靠后的位置即可。

  • 初始化con[26],以con[s[i]-‘a’]读入
  • vector< int > tcnt(26)表示tcnt这个向量初始有26个元素
    vector< int > tcnt[26]表示这是一个二维向量,每一维有26个,相当于<vector < vector < int > > tcnt
  • for(auto &c : s) 与 for(auto c : s)
    string s("hello world");
    for(auto c:s)
    c='t';
    cout<<s<<endl;//结果为hello world
    for(auto &c:s)
    c='t';
    cout<<s<<endl; //结果为ttttttttttt

标程中通过for(auto &c : t)遍历ti,记录每种字母的数目

#define forn(i, n) for(int i = 0; i < int(n); i++)

forn(i, m){
		cin >> t;
		vector<int> cnt(26);
		for (auto &c : t)
			++cnt[c - 'a'];
		int ans = -1;
		forn(j, 26) if (cnt[j] > 0)
			ans = max(ans, pos[j][cnt[j] - 1]);
		cout << ans << "\n";
	}
  • 注意对下标的处理,题目中是自1开始
#include <bits/stdc++.h>

using namespace std;

int n, m;// n s的长度 m 测试的总个数
string s, t;

/*对每个t,统计26个字母中每个字母的个数
**对s,统计出每个字母的位置*/

vector<int> scon[26]; //用来存放s中每个字母的位置

int main()
{
    cin >> n >> s >> m;

    //先处理s
    for(int i = 0;i < n;i++)
    {
        scon[s[i]-'a'].push_back(i+1);
        //or push_back(i) 对照样例可得
    }

    while(m)
    {
        cin >> t;
        vector<int> tcnt(26);
        for(int i = 0;i < t.length();i++)
        {
            tcnt[t[i]-'a'] += 1;;
        }
        int ans = -1;
        for(int i = 0;i < 26;i++)
        {
            if(tcnt[i])
            {
                ans = max(ans, scon[i][tcnt[i]-1]);
            }
        }
        cout << ans << endl;

        m--;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值