【强训笔记】day12

NO.1
在这里插入图片描述
思路:哈希表,建立bool数组,将要删除的字符串存入哈希表,并标为true,再遍历要做处理的字符串,如果在哈希表中为false,就输出。

代码实现:

#include <iostream>
#include<string>

using namespace std;

int main()
{
    string s,t;
    getline(cin,s);
    getline(cin,t);
    bool hash[300]={0};
    for(auto ch:t) hash[ch]=true;
    for(auto ch:s)
    {
        if(!hash[ch])
        {
            cout<<ch;
        }
    }
    return 0;
}

NO.2
在这里插入图片描述
思路:根据链表1和2的路径相同来解决,链表一走完就走链表2,链表2走完就走链表1,当它们相同的时候停下来就是公共的第一个节点,如果为空就返回空节点。

代码实现:

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        ListNode* cur1=pHead1;
		ListNode* cur2=pHead2;
		while(cur1!=cur2)
		{
			cur1=cur1!=NULL?cur1->next:pHead2;
			cur2=cur2!=NULL?cur2->next:pHead1;
		}
		return cur1;

    }
};

NO.3
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
思路:利用dp的方法,找s的个数,如果最后一个字符为s,就等于之前的s再加1,不是就为之前s的数量,最后一个为h,那么sh的数量就等于前面s的数量加上前面sh的数量,不是就是前面sh的数量,如果最后一个为y,那么shy的数量就等于前面sh的数量加上前面shy的数量,不是就等于前面shy的数量。最后输出shy的数量就行,最后考虑到空间优化,用字符代替dp数组。

代码实现:

#include <iostream>
 #include<string>
 using namespace std;
 
int main() {
    string str;
    int n;
    cin>>n>>str;
    long long s=0,h=0,y=0;
    for(int i=0;i<str.size();i++)
    {
        char ch=str[i];
        if(ch=='s') s++;
        else if(ch=='h') h+=s;
        else if(ch=='y') y+=h;
    }
    cout<<y<<endl;
    return 0;
}
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lehjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值