lightoj 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
|
|---|
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int cnt,T;
char s[];
map<string,int>mp;
int a[N];
int len;
int tot;
int vis[N];
int p,q;
int l;
char temp[N]; void ini()
{
p=-;q=;
int i,j;
mp.clear();
len=tot=;
memset(vis,,sizeof(vis));
while(scanf("%s",s)!=EOF && strcmp(s,"END")!=){
l=strlen(s);
// printf(" %s\n",s);
j=;
int ff=;
for(i=;i<l;i++){
if(s[i]>='a' && s[i]<='z'){
ff=;
temp[j]=s[i];
j++;
}
else{
if(ff==){
temp[j]='\0';
len++;
j=;
ff=;
if(!mp[temp]){
tot++;
mp[temp]=tot;
}
a[len]=mp[temp];
}
}
} if(ff==){
temp[j]='\0';
len++;
j=;
ff=;
if(mp[temp]==){
tot++;
mp[temp]=tot;
}
a[len]=mp[temp];
}
}
//printf(" len=%d tot=%d\n",len,tot);
//for(i=1;i<=len;i++){
// printf(" i=%d a=%d\n",i,a[i]);
//}
} void solve()
{
int i,j;
int now=;
i=;
j=;
while(i<=len)
{
//printf(" i=%d j=%d\n",i,j);
for(;j<=len;j++){
vis[ a[j] ]++;
if(vis[ a[j] ]==) now++;
if(now==tot){
//printf(" i=%d j=%d p=%d q=%d\n",i,j,p,q);
for(;i<=j;i++){
if(vis[ a[i] ]==){
//printf(" i=%d j=%d p=%d q=%d\n",i,j,p,q);
if(j-i<q-p){
p=i;q=j;
}
else if(j-i==q-p){
if(i<p){
p=i;q=j;
}
}
vis[ a[i] ]--;
now--;i++;j++;
break;
}
vis[ a[i] ]--;
}
break;
}
}
if(j==len+) break;
}
} void out()
{
printf("Case %d: %d %d\n",cnt,p,q);
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(cnt=;cnt<=T;cnt++)
//while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
}
lightoj 1293 - Document Analyzer [ 两指针 + 字符串 ]的更多相关文章
- ytu 1052: 写一函数,将两个字符串连接(水题,指针练习)
1052: 写一函数,将两个字符串连接 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 343 Solved: 210[Submit][Status][ ...
- java中判断两个字符串是否相等的问题
我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...
- c语言中的利用函数实现交换两个字符,交换两个字符串
c语言交换两个字符: 方法一:利用指针传址,效率比较高 void swap(int *a,int *b) { int temp; temp = *a; *a = *b; *b = temp } 方法二 ...
- 【python】实例-python实现两个字符串中最大的公共子串
由于python中的for循环不像C++这么灵活,因此该用枚举法实现该算法: C="abcdefhe" D="cdefghe" m=0 n=len(C) E=[ ...
- shell比较两个字符串是否相等
比较两个字符串是否相等的办法是: if [ "$test"x = "test"x ]; then这里的关键有几点:1 使用单个等号2 注意到等号两边各有一个空格 ...
- js正则表达式的一些研究,截取两个字符串中间的字符串
一个最常用的场景 截取两个字符串中间的字符串 var str = "iid0000ffr"; var substr = str.match(/id(\S*)ff/); ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- Python 比较两个字符串大小
python 2中,有cmp(a,b)函数,用于比较两个字符串的大小. 如 >>>a='abc' >>>b='abd' >>>print cmp( ...
- 用Java编程找到两个字符串中共有的字符
这道题的算法思想是把字符串1中的每个字符与字符串2中的每个字符进行比较,遇到共同拥有的字符,放入另一个数组中,最后顺序输出即可 但是这道题的难点在于怎么排除重复的字符 public class bot ...
随机推荐
- iOS 动画(基于Lottie封装)
一般app中都会带有动画,而如果是一些复杂的动画,不但实现成本比较高,而且实现效果可能还不能达到UI想要的效果,于是我们可以借助lottie来完成我们想要的动画. lottie动画1.gif ...
- Xilinx器件原语
原语,其英文名为primitive,是FPGA厂商针对其器件特征开发的一系列常用模块的名称.原语是FPGA芯片中基本元件,代表FPGA中实际拥有的硬件逻辑单元,如LUT,D触发器,RAM等.相当于软件 ...
- chrome调试之Workspaces
可通过workspaces来编辑本地文件 workspaces是Chrome DevTools的一个强大功能,这使DevTools变成了一个真正的IDE.Workspaces会将Sources选项卡中 ...
- IE11/Flash页游白屏怎么办!立刻开启IE大地址模式!缓解浏览器白屏问题
您是否经常发现IE白屏了,具体表现为点开新网页时无法显示,只能切换标签,用任务管理器一看,内存1.2G之多. 这是因为IE11可能有内存泄露问题,内存不断增长以至于无法申请新的内存,于是IE就完蛋了! ...
- 【转载】SQL Server 2012 日志传送
SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...
- 云梯互联:所有主机已全面支持免费SSL!附小白配置教程。
HTTPS和HTTP的区别:1.HTTPS是加密传输协议,HTTP是名文传输协议;2.HTTPS需要用到SSL证书,而HTTP不用;3.HTTPS比HTTP更加安全,对搜索引擎更友好,利于SEO4. ...
- 将自己的数据制作成voc格式
VOCdevkit2007文件下只保存VOC2007,VOC2007下只保存Annotations ImageSets JPEGImages. JPEGImages存放所有的图片数据(即训练测试验证的 ...
- 日常[系统]:Linux新人报到(吐槽%&%……&¥……%
昨天换了系统,从win7换到了NOIP必须面对的Linux系统. 不得不说,真的很不适应.原本右上角的三个按钮变到了左上角. 可爱的DEVCPP被无情的抛弃了. 又用不惯guide,只好用文本编辑器写 ...
- Java 编程下 Eclipse/myeclipse 如何设置单行代码显示的最大宽度
http://www.cnblogs.com/sunzn/archive/2013/03/30/2990191.html 或 http://zhidao.baidu.com/link?url=67uy ...
- linux下tomcat服务的相关命令
一:Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/loca ...