zoukankan      html  css  js  c++  java
  • Codevs 1051 接龙游戏

    1051 接龙游戏

      时间限制: 1 s    空间限制: 128000 KB    题目等级 : 钻石 Diamond

     
    题目描述 Description

    给出了N个单词,已经按长度排好了序。如果某单词i是某单词j的前缀,i->j算一次接龙(两个相同的单词不能算接龙)。

    你的任务是:对于输入的单词,找出最长的龙。

    输入描述 Input Description

    第一行为N(1<=N<=105)。以下N行每行一个单词(由小写组成),已经按长度排序。(每个单词长度<50)

    输出描述 Output Description

    仅一个数,为最长的龙的长度。

    样例输入 Sample Input

    5

    i

    a

    int

    able

    inter

    样例输出 Sample Output

    3

    数据范围及提示 Data Size & Hint

    1<=N<=1050

    50分代码存档:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<stack>
     5 #include<algorithm>
     6 using namespace std;
     7 string s[200000];
     8 int n,ans;
     9 stack<string> st;
    10 int max(int a,int b)
    11 {
    12     if(a>b) return a;
    13     else return b;
    14 }
    15 int main()
    16 {
    17     scanf("%d",&n);
    18     for(int i=1;i<=n;i++)
    19       cin>>s[i]; 
    20     sort(s+1,s+n+1);
    21     for(int i=1;i<=n;i++)
    22     {
    23         string x=st.top();
    24         if(st.empty()){
    25             st.push(s[i]);
    26             ans=max(ans,st.size());
    27         }
    28         else if(s[i]==st.top())
    29         {
    30             continue;
    31         }
    32         else if(x[0]==s[i][0]&&s[i].size()>x.size())
    33         {
    34             st.push(s[i]);
    35             ans=max(ans,st.size());
    36         }
    37         else 
    38         {
    39             while(!st.empty())
    40               st.pop();
    41             st.push(s[i]);
    42         }
    43     }
    44     printf("%d",ans);
    45     return 0;
    46 }

    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 #include <stack>
     7 using namespace std;
     8 const int MAXN = 1e5 + 5;
     9 stack <string> s;
    10 string c[MAXN];
    11 bool can(string a,string b)
    12 {
    13     if(a == b)return false;
    14     if(a.length() > b.length())return false;
    15     return a == b.substr(0,a.length());
    16 }
    17 int cnt;
    18 void make_can(string a)
    19 {
    20     while(!s.empty() && !can(s.top(),a))
    21         s.pop(),cnt--;
    22     s.push(a);
    23     cnt++;
    24     return;
    25 }
    26 int ans;
    27 int n;
    28 int main()
    29 {
    30     scanf("%d",&n);
    31     for(int i = 1;i <= n;i ++)
    32         cin >> c[i];
    33     sort(c + 1,c + n + 1);
    34     n = unique(c + 1,c + n + 1) - (c + 1);
    35     for(int i = 1;i <= n;i ++)
    36         if(s.empty() || can(s.top(),c[i]))
    37             s.push(c[i]),ans = max(ans,++cnt);
    38         else
    39             make_can(c[i]);
    40     printf("%d
    ",ans);
    41     return 0;
    42 }
  • 相关阅读:
    JS创建对象的四种简单方式 (工厂模式和自定义构造函数创建对象的区别)
    对js原型对象、实例化对象及prototype属性的一些见解
    Javascript中的undefined、null、""、0值和false的区别总结
    new Function()语法
    声明函数的方法 之 语句定义法(函数声明法)和表达式定义法(函数表达式)
    匿名函数function前面的! ~等符号作用小解
    proxyTable设置代理解决跨域问题
    vue之递归组件实现树形目录
    关于页面出现弹窗时,页面还可以滚动问题
    倒计时功能
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6216674.html
Copyright © 2011-2022 走看看