OR63 删除公共字符(简单)(模拟)
删除公共字符_牛客题霸_牛客网 (nowcoder.com)
#include <iostream>
#include<string>
#include<unordered_set>
using namespace std;
int main() {
string str1;
unordered_set<char> myset;
getline(cin,str1);
char tmp;
while(cin>>tmp)
{
myset.insert(tmp);
}
for(auto e : str1)
{
if(myset.count(e)) continue;
cout<<e;
}
return 0;
}
// 64 位输出请用 printf("%lld")
JZ52 两个链表的第一个公共结点(中等)(链表)
两个链表的第一个公共结点_牛客题霸_牛客网 (nowcoder.com)
思路1:
链表1走:s1+con+s2
链表2走:s2+con+s1
他们最终会在交点相遇
思路2:
先让长的走到和短的一样长,再一起走判断相遇
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
if(pHead1==nullptr||pHead2==nullptr) return nullptr;
ListNode* p1=pHead1;
ListNode* p2=pHead2;
while(p1!=p2)
{
if(p1==nullptr) p1=pHead2;
else p1=p1->next;
if(p2==nullptr) p2=pHead1;
else p2=p2->next;
}
if(p1->val==p2->val)
return p1;
else return nullptr;
}
};
mari和shiny(简单)(线性dp)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
int n;
cin>>n;
string str;
cin>>str;
long long s=0;
long long sh=0;
long long shy=0;
if(str[0]=='s') s=1;
for(int i=1;i<n;i++)
{
if(str[i]=='s')
{
s += 1;
}
if(str[i]=='h')
{
sh+=s;
}
if(str[i]=='y')
{
shy+=sh;
}
}
cout<<shy;
return 0;
}