LightOJ 1293 Document Analyzer (map+两点法)

1293 - Document Analyzer

PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB

You work in a leading software development company. As youare great in coding, most of the critical tasks are allotted for you. You likethe challenge and you feel excited to solve those problems.

Recently your company is developing a project named DocumentAnalyzer. In this project you are assigned a task; of course a critical task.The task is that you are given a document consisting of lowercase letters,numbers and punctuations.
You have to analyze the document and separate thewords first. Words are consecutive sequences of lower case letters. Afterlisting the words, in the order same as they occurred in the document, you haveto number them from
1, 2, ..., n. After that you have to find the range pand
q (p ≤ q) such that all kinds of words occur between pand
q (inclusive). If there are multiple such solutions you have to findthe one where the difference of
p and q is smallest. If stillthere is a tie, then find the solution where
p is smallest.

Input

Input starts with an integer T (≤ 15),denoting the number of test cases.

Each case will be denoted by one or more lines denoting adocument. Each line contains no more than
100 characters. A documentwill contain either lowercase letters or numbers or punctuations. The last lineof a document will contain the word
‘END‘ which is of course not thepart of the document. You can assume that a document will contain between
1and 50000 words (inclusive). Words may contain up to
10characters. And a document can contain up to 5000 lines.

Output

For each case, print the case number and
p
and qas described above.

Sample Input

Output for Sample Input


3

1. a case is a case,

2. case is not a case~

END

a b c d e

END

[email protected]#$a^%a a a

b b----b b++12b

END


Case 1: 6 9

Case 2: 1 5

Case 3: 5 6

Note

Dataset is huge, use faster I/O methods.

题目链接:http://lightoj.com/volume_showproblem.php?problem=1293

题目大意:求最小的覆盖了所有英文字符串的区间的端点值

题目分析:先将单独的英文单词或字母map成离散的数字,然后就是两点法,先确定最小的右端点使得区间包含全部不重复的数字记录此时区间的左右端点,然后移动左端点,若此时区间不包含全部数字则再移动右端点,这样反复移动左右端点并更新区间端点值,直到再向后找不到合法区间为止

#include <cstdio>
#include <string>
#include <cstring>
#include <map>
using namespace std;
int const MAX = 500005;
int const INF = 0x3fffffff;
char s[MAX];
bool hash[MAX];
int a[MAX];
map <string, int> mp;

int main()
{
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        mp.clear();
        map <int, int> h;
        memset(hash, false, sizeof(hash));
        memset(a, 0, sizeof(a));
        int cnt = 0, num = 0;
        while(gets(s))
        {
            string tmp = "";
            if(strcmp(s, "END") == 0)
                break;
            int len = strlen(s);
            for(int i = 0; i <= len; i++)
            {
                if(s[i] >= 'a' && s[i] <= 'z')
                    tmp += s[i];
                else
                {
                    if(tmp != "")
                    {
                        if(!hash[mp[tmp]])
                        {
                            num ++;
                            mp[tmp] = id++;
                            hash[mp[tmp]] = true;
                        }
                        a[cnt++] = mp[tmp];
                        tmp = "";
                    }
                }
            }
        }
        int st = 0, ed = 0, num2 = 0;
        int ansst, ansed, mi = INF;
        while(true)
        {
            while(ed < cnt && num2 < num)
                if(h[a[ed++]] ++ == 0)
                    num2 ++;
            if(num2 < num)
                break;
            if(ed - st < mi)
            {
                ansed = ed;
                ansst = st;
                mi = ed - st;
            }
            if(-- h[a[st++]] == 0)
                num2 --;
        }
        printf("Case %d: %d %d\n", ca, ansst + 1, ansed);
    }
}
时间: 2024-11-08 20:29:34

LightOJ 1293 Document Analyzer (map+两点法)的相关文章

lightoj 1293 - Document Analyzer [ 两指针 + 字符串 ]

传送门 1293 - Document Analyzer   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB You work in a leading software development company. As you are great in coding, most of the critical tasks are allotted for you. You like the ch

NOJ 1972 炒股票的女巫璐璐 &amp;&amp; NOJ 1974 BRN (浅谈两点法)

两点法是通常用来求解简单的区间问题的O(n)算法,两点法顾名思义有两个点,一个起点一个终点,在区间上维护这两个点,动态更新题目要求的值的大小 这里举出NOJ两道较简单且有代表性的题,一道是数字一道是字符串 炒股票的女巫璐璐 时间限制(普通/Java):1000MS/3000MS         运行内存限制:65536KByte 总提交:674          测试通过:40 题目描述 女巫璐璐生活在美丽的仙林.璐璐突发奇想想到了炒股.璐璐运用了她的神奇魔法获取了一支股票未来连续N天的价格.

Jackson 处理复杂类型(List,map)两种方法

方法一: String jsonString="[{'id':'1'},{'id':'2'}]"; ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class); //如果是Map类型 mapper.getTypeFactory().constructParametricType(H

Map两种遍历方式与TreeSet两种排序依据

集合:可以存储不同类型的多个对象,随着存储对象的个数的增加而自动扩大容量   体系结构: Collection<E>   |----List:存入的对象是有序的,且可以重复           ArrayList:底层使用的数据结构是数组,线程不安全的,查找速度快,增删速度慢           Vector:底层使用的数据结构是数组,线程安全的,查找速度快,增删速度慢           LinkedList:底层使用的数据结构是链表,线程不安全的,查找速度慢,增删速度快     |----

Angularjs 实现 $(document).ready()的两种方法

1.在controller里面利用$on或者$watch bookControllers.controller('bookctrl_test', ['$scope', '$routeParams', function($scope, $routeParams) { $scope.$on('$viewContentLoaded', function() { alert('1'); }); alert('2'); }]); bookControllers.controller('bookctrl_t

Map的两种遍历方式

********************************************************************************* *****************************Map两种遍历方式******************************* ********************************************************************************* 1 package ccms;

java容器的两大类Collection和Map

java容器包括Collection和Map两种,Collection储存着对象的集合,而Map储存着键值对(两个对象)的映射表. Collection: 1)Set ·TreeSet ·HashSet ·LinkedHashSet 2)List ·ArrayList(基于动态数组实现,线程不安全) ·Vector (线程安全的,同步的,开销必ArrayList大,访问速度更忙.Vector 每次扩容请求其大小的 2 倍空间,而 ArrayList 是 1.5 倍) ·LinkedList(基于

如何将经纬度利用Google Map API显示C# VS2005 Sample Code

原文 如何将经纬度利用Google Map API显示C# VS2005 Sample Code 日前写了一篇如何用GPS抓取目前所在,并回传至资料库储存,这篇将会利用这些回报的资料,将它显示在地图上,这个做法有两种,最简单的就是直接传值到Google Maps上. 举例来说,当我们知道经纬度后,只要将数据套到以下网址即可. http://maps.google.com/maps?q=25.048346%2c121.516396 在参数q=后面,就可以加上经纬度了. 25.048346是Lati

初识SFDC创建一个google map(添加了marker小图标让他可以去到你输入的经纬度上)

1 <apex:page > 2 <head> 3 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 4 <script type="text/javascript" src="http://maps.google.cn/maps/api/js?sensor=false"></scr