Reposts(dfs)

One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp’s joke to their news feed. Some of them reposted the reposts and so on.

These events are given as a sequence of strings “name1 reposted name2”, where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string “name1 reposted name2” user “name1” didn’t have the joke in his feed yet, and “name2” already had it in his feed by the moment of repost. Polycarp was registered as “Polycarp” and initially the joke was only in his feed.

Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp’s joke.

Input
The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as “name1 reposted name2”. All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.

We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Output
Print a single integer — the maximum length of a repost chain.

Examples
Input
5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya
Output
6
题目分析:
这道题只要一开始全部字母处理成小写或大写,数字和‘/’不用管的。还有一点要注意的是他们转发的顺序可能不是连续的,可能隔了好多个人又转发了一连串,只要注意这一点就行了。深搜很轻松。
代码:

#include<iostream>
#include<string>
using namespace std;
int shu[201],n;
string a[201],b,c[201];
int dfs(string ok,int len,int st)
{
    int l=len;
    for(int i=st+1;i<=n;i++)
    {
        if(c[i]==ok)
        {
            l=max(l,dfs(a[i],len+1,i));//取最大值
        }
    }
    return l;
}
int main()
{
    int ans=1;
    cin>>n;
    string t="polycarp";
    int l=1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        for(int j=0;j<a[i].size();j++)
            if(a[i][j]>='A'&&a[i][j]<='Z') a[i][j]+=32;
        cin>>b;
        cin>>c[i];
        for(int j=0;j<c[i].size();j++)
            if(c[i][j]>='A'&&c[i][j]<='Z') c[i][j]+=32;
        if(c[i]==t) {
            shu[l++]=i;
        }
    }
    int len;
    string ok;
    for(int i=1;i<=l-1;i++)
    {
        len=2;
        ok=a[shu[i]];
        ans=max(dfs(ok,len,shu[i]),ans);//取最大值
    }
    cout<<ans;
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import re import json import time import requests import datetime import pymysql import selenium from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from selenium.webdriver import Edge, EdgeOptions # 创建浏览器对象 options = EdgeOptions() options.use_chromium = True options.binary_location = r'C:\Users\邓枫林\PycharmProjects\pythonProject\edgedriver_win64\msedgedriver.exe' browser = Edge(options=options) wait = WebDriverWait(browser, 10) # 打开微博话题页面 url = 'https://weibo.com/n/%E4%B8%AD%E5%9B%BD%E9%A3%9F%E5%93%81%E5%8D%AB%E7%94%9F?from=feed&loc=at&nick=%E4%B8%AD%E5%9B%BD%E9%A3%9F%E5%93%81%E5%8D%AB%E7%94%9F&order=hot' browser.get(url) # 等待页面加载完成 wait.until(lambda driver: driver.execute_script("return document.readyState") == "complete") browser = selenium.webdriver.Edge(executable_path='C:/Users/邓枫林/PycharmProjects/pythonProject/edgedriver_win64/msedgedriver.exe') wait = selenium.webdriver.support.ui.WebDriverWait(browser, 10) # 监测页面是否包含“高校类”敏感词汇 if '高校类' in browser.page_source: # 获取原始微博 weibo = browser.find_element_by_css_selector('.WB_feed_detail .WB_text.W_f14').text # 获取转发该微博的用户昵称和转发内容 reposts = [] repost_items = browser.find_elements_by_css_selector('.list_ul .list_li') for item in repost_items: nickname = item.find_element_by_css_selector('.WB_text.W_f14').text content = item.find_element_by_css_selector('.WB_text.W_f14 + .comment_txt').text reposts.append({'nickname': nickname, 'content': content}) # 关闭浏览器 browser.quit() # 将微博和转发内容存入MySQL数据库中 Base = declarative_base() class Weibo(Base): tablename = 'weibo_user' id = Column(Integer, primary_key=True) content = Column(Text) create_time = Column(DateTime) class Repost(Base): tablename = 'weibo_repost' id = Column(Integer, primary_key=True) weibo_id = Column(Integer) nickname = Column(String(50)) content = Column(Text) engine = create_engine('mysql+pymysql://root:root@hostname:port/weibo?charset=utf8mb4') Session = sessionmaker(bind=engine) session = Session() now = datetime.datetime.now() weibo_obj = Weibo(content=weibo, create_time=now) session.add(weibo_obj) session.commit() for repost in reposts: repost_obj = Repost(weibo_id=weibo_obj.id, nickname=repost['nickname'], content=repost['content']) session.add(repost_obj) session.commit() session.close() else: # 关闭浏览器 browser.quit()
最新发布
06-12

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值