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 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

[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.

我也是醉了,输出少了个空格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 }
时间: 2024-10-23 23:20:19

lightoj 1293 - Document Analyzer [ 两指针 + 字符串 ]的相关文章

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

JavaScript基础 使用+号连接两个字符串

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut

计算两个字符串最大公有子串

*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0; } /* HEAD

7.28 指针 字符串 字符串库函数

验证大小端存储 : int main() { int a = 0x12345678; char *p = (char *)&a; printf("%x\n", *p); // 78 表示这是小端存储 return 0; } 字符数组: char name[] = { 'h', 'e', 'l', 'l', 'o' };//错误的 因为后面没有\0 char name[] = "liuwei"; char name[100] = {'h','e','l','l

黑马程序员_日记25_Java两个字符串的最大相同子串

--- android培训.java培训.期待与您交流! ---- /* 获取两个字符串中最大相同子串.第一个动作:将短的那个串进行长度一次递减的子串打印. "abcwerthelloyuiodef" "cvhellobnm" 模拟一下: 第一趟: 最大子串:cvhellobnm ↑--------↑ 在长字符串中查找 abcwerthelloyuiodef ↑--------↑ abcwerthelloyuiodef ↑--------↑ abcwerthello

关于两个字符串的kmp比对算法

关于两个字符串的kmp比对算法 假设有字符串X和Y,满足len(X)>len(Y),要比对这两个字符串. 我们知道,最朴实的方法,就是现将二者对齐,然后依次比对对应位置的字符.如果能匹配到Y最后位置,则匹配成功:如果匹配失败,则将Y右移一位,再从头进行匹配. 设字符串X为dababeabafdababcg:字符串Y为ababc. 这种比对方法如下所示: 起始时,二者对其,第一个字符不匹配 :| :dababeabafdababcg :ababc 右移一位,比对位置移动到Y起始位置 : | :da

【python】实例-python实现两个字符串中最大的公共子串

由于python中的for循环不像C++这么灵活,因此该用枚举法实现该算法: C="abcdefhe" D="cdefghe" m=0 n=len(C) E=[] b=0 while(m<n): i=n-m while(i>=0): E.append(C[m:m+i]) i-=1 m+=1 for x in E: a=0 if x in D: a=len(x) c=E.index(x) if a > b:#保存符合要求的最长字符串长度和地址 b=a

实现两个字符串连接

char *Mystrcat(char*str1, char* str2){ if (str1 == NULL || str2 == NULL)  return NULL; char*temp = str1;    //申请指向字符的指针 while (*str1 != '\0') {  str1++;  //让指针指向字符串的尾部 } while (*str2 != '\0') {  *str1++ = *str2++;    //从尾部开始循环赋值 } *str1 = '\0'; retur

C#通过编辑距离计算两个字符串的相似度的代码

将开发过程中较好的一些代码段备份一下,下面的代码是关于C#通过编辑距离计算两个字符串的相似度的代码,应该能对码农们有些帮助. using System; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Levenshtein { public delegate void AnalyzerCompletedHander(double sim); public class Levenshtei