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;
    }
  • 相关阅读:
    一意孤行
    叶子书签
    漫舞
    男朋友
    ubuntu自动关机命令,ubuntu 无法关机解决方法
    情人节
    生死由天
    春暖花开
    android开发学习中的问题:error: device not found解决办法
    暗恋
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4864320.html
Copyright © 2011-2022 走看看