码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-14 10:59:04      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

传送门

1293 - Document Analyzer
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 challenge and you feel excited to solve those problems.

Recently your company is developing a project named Document Analyzer. 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 the words first. Words are consecutive sequences of lower case letters. After listing the words, in the order same as they occurred in the document, you have to number them from 1, 2, ..., n. After that you have to find the range p and q (p ≤ q) such that all kinds of words occur between p and q (inclusive). If there are multiple such solutions you have to find the one where the difference of p and q is smallest. If still there 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 a document. Each line contains no more than 100 characters. A document will contain either lowercase letters or numbers or punctuations. The last line of a document will contain the word ‘END‘ which is of course not the part of the document. You can assume that a document will contain between 1 and 50000 words (inclusive). Words may contain up to 10 characters. And a document can contain up to 5000 lines.

Output

For each case, print the case number and p and q as 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

a@#$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.

 

我也是醉了,输出少了个空格wa惨了,,,

题解:如标签,用两个指针start、end,往前扫,复杂度O(n)

484217 2015-03-14 08:05:52 1293 - Document Analyzer C++ 1.128 6928
Accepted 

 

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <stack>
  4 #include <vector>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <map>
  8 #include <string>
  9 
 10 #define ll long long
 11 int const N = 50005;
 12 int const M = 205;
 13 int const inf = 1000000000;
 14 ll const mod = 1000000007;
 15 
 16 using namespace std;
 17 
 18 int cnt,T;
 19 char s[500005];
 20 map<string,int>mp;
 21 int a[N];
 22 int len;
 23 int tot;
 24 int vis[N];
 25 int p,q;
 26 int l;
 27 char temp[N];
 28 
 29 void ini()
 30 {
 31     p=-1;q=100000000;
 32     int i,j;
 33     mp.clear();
 34     len=tot=0;
 35     memset(vis,0,sizeof(vis));
 36     while(scanf("%s",s)!=EOF && strcmp(s,"END")!=0){
 37         l=strlen(s);
 38        // printf(" %s\n",s);
 39         j=0;
 40         int ff=0;
 41         for(i=0;i<l;i++){
 42             if(s[i]>=a && s[i]<=z){
 43                 ff=1;
 44                 temp[j]=s[i];
 45                 j++;
 46             }
 47             else{
 48                 if(ff==1){
 49                     temp[j]=\0;
 50                     len++;
 51                     j=0;
 52                     ff=0;
 53                     if(!mp[temp]){
 54                         tot++;
 55                         mp[temp]=tot;
 56                     }
 57                     a[len]=mp[temp];
 58                 }
 59             }
 60         }
 61 
 62         if(ff==1){
 63             temp[j]=\0;
 64             len++;
 65             j=0;
 66             ff=0;
 67             if(mp[temp]==0){
 68                 tot++;
 69                 mp[temp]=tot;
 70             }
 71             a[len]=mp[temp];
 72         }
 73     }
 74     //printf("  len=%d tot=%d\n",len,tot);
 75     //for(i=1;i<=len;i++){
 76     //    printf("  i=%d a=%d\n",i,a[i]);
 77     //}
 78 }
 79 
 80 void solve()
 81 {
 82     int i,j;
 83     int now=0;
 84     i=1;
 85     j=1;
 86     while(i<=len)
 87     {
 88         //printf(" i=%d j=%d\n",i,j);
 89         for(;j<=len;j++){
 90             vis[ a[j] ]++;
 91             if(vis[ a[j] ]==1) now++;
 92             if(now==tot){
 93                 //printf(" i=%d j=%d p=%d q=%d\n",i,j,p,q);
 94                 for(;i<=j;i++){
 95                     if(vis[ a[i] ]==1){
 96                         //printf(" i=%d j=%d p=%d q=%d\n",i,j,p,q);
 97                         if(j-i<q-p){
 98                             p=i;q=j;
 99                         }
100                         else if(j-i==q-p){
101                             if(i<p){
102                                 p=i;q=j;
103                             }
104                         }
105                         vis[ a[i] ]--;
106                         now--;i++;j++;
107                         break;
108                     }
109                     vis[ a[i] ]--;
110                 }
111                 break;
112             }
113         }
114         if(j==len+1) break;
115     }
116 }
117 
118 void out()
119 {
120     printf("Case %d: %d %d\n",cnt,p,q);
121 }
122 
123 int main()
124 {
125     //freopen("data.in","r",stdin);
126     scanf("%d",&T);
127     for(cnt=1;cnt<=T;cnt++)
128     //while(scanf("%d%d",&n,&m)!=EOF)
129     {
130         ini();
131         solve();
132         out();
133     }
134 }

 

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

标签:

原文地址:http://www.cnblogs.com/njczy2010/p/4337078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!