zoukankan      html  css  js  c++  java
  • HDU 2668 Daydream

    用一个队列来维护

    每次加入一个字符,如果字符没有在队列里,那么直接入队,并且检查更新队列大小。

    如果加入的字符在队列里了,那么要一直弹出队首,直到弹出的字符和当前要插入的一样。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int maxn=10000000+10;
    queue<int>Q;
    char s[maxn];
    int flag[500];
    int ans1,ans2,ans3;
    int n;
    
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(flag,0,sizeof flag);
            while(!Q.empty()) Q.pop();
            scanf("%s",s);
            ans1=-1;
    
            for(int i=0; i<n; i++)
            {
                if(flag[s[i]]==0)
                {
                    flag[s[i]]=1;
                    Q.push(i);
                    if(i-Q.front()+1>ans1)
                    {
                        ans1=i-Q.front()+1;
                        ans2=Q.front();
                        ans3=i;
                    }
                }
                else if(flag[s[i]]==1)
                {
                    while(1)
                    {
                        int h=Q.front();
                        Q.pop();
                        flag[s[h]]=0;
                        if(s[h]==s[i]) break;
                    }
                    Q.push(i);
                    flag[s[i]]=1;
                }
            }
    
            printf("%d %d %d
    ",ans1,ans2,ans3);
        }
        return 0;
    }
  • 相关阅读:
    mysql慢查询
    linux查找文件
    ss安装教程
    node_module删除
    api的错误代码设计
    es6的Promise
    vue后台项目
    vue的main.js
    vue的过渡效果
    【Linux网络基础】网络拓扑、OSI层次模型、TCP/IP协议簇
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4864320.html
Copyright © 2011-2022 走看看