• CodeForces 522A


    A. Reposts
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Sample test(s)
    input
    5
    tourist reposted Polycarp
    Petr reposted Tourist
    WJMZBMR reposted Petr
    sdya reposted wjmzbmr
    vepifanov reposted sdya
    output
    6
    input
    6
    Mike reposted Polycarp
    Max reposted Polycarp
    EveryOne reposted Polycarp
    111 reposted Polycarp
    VkCup reposted Polycarp
    Codeforces reposted Polycarp
    output
    2
    input
    1
    SoMeStRaNgEgUe reposted PoLyCaRp
    output
    2
    题目意思:给定一系列的字符串,然后用这些字符串相连接,找一下最长能够连多长(前一个字符串的第三个单词和下一个字符串的第一个单词相等的时候可以连接,字符串中的大写和小写是一样对待的)!
    解题思路:
    方法一:搜索,这题测试数据相当水,可以水过;
    方法二:动态规划,用map<string,int>,dp[str3]=dp[str1]+1;
    附搜索的代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<ctime>
     5 #include<algorithm>
     6 #include <map>
     7 #include <queue>
     8 using namespace std;
     9 char s1[205][30],s2[205][30];
    10 int n;
    11 char s[30];
    12 int ans;
    13 bool vis[205];
    14 void cha(int k)
    15 {
    16     int l1=strlen(s1[k]),l2=strlen(s2[k]);
    17     for(int i=0;i<l1;i++)
    18     {
    19         if(s1[k][i]>='A'&&s1[k][i]<='Z') s1[k][i]=s1[k][i]-'A'+'a';
    20     }
    21     for(int i=0;i<l2;i++)
    22     {
    23         if(s2[k][i]>='A'&&s2[k][i]<='Z') s2[k][i]=s2[k][i]-'A'+'a';
    24     }
    25 }
    26 void dfs(int num,int cnt)
    27 {
    28     if(cnt>ans) ans=cnt;
    29     for(int i=0;i<n;i++)
    30     {
    31         if(vis[i]==1) continue;
    32         if(num==-1||strcmp(s1[i],s2[num])==0){
    33             vis[i]=1;
    34             dfs(i,cnt+1);
    35             vis[i]=0;
    36         }
    37     }
    38 }
    39 int main()
    40 {
    41    while(~scanf("%d",&n)){
    42          ans=0;
    43          for(int i=0;i<n;i++)
    44             {
    45                 scanf(" %s%s%s",s1[i],s,s2[i]);
    46                 cha(i);
    47             }
    48             memset(vis,0,sizeof(vis));
    49             dfs(-1,1);
    50             printf("%d
    ",ans);
    51          }
    52     return 0;
    53 }
  • 相关阅读:
    WordPress The Plus Addons for Elementor插件身份验证绕过漏洞复现分析
    ThinkPHP 5日志文件包含trick
    JavaScript对称数字金字塔(机考)
    css绘制三角箭头
    element-ui table 多列数据动态排序(前后台交互)
    Animate.css
    Normalize.css
    CMake笔记
    时间对齐——用 FFT 加速互相关
    g2o 代码学习—— exp map and log map for SE(3), SIM(3)
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4358020.html
Copyright © 2020-2023  润新知