首页 > 其他 > 详细

【Codeforces 723B】Text Document Analysis 模拟

时间:2016-10-04 07:26:41      阅读:292      评论:0      收藏:0      [点我收藏+]

求括号外最长单词长度,和括号里单词个数。

有限状态自动机处理一下。

http://codeforces.com/problemset/problem/723/B

Examples
input
37
_Hello_Vasya(and_Petya)__bye_(and_OK)
output
5 4


input
37
_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__
output
2 6


input
27
(LoooonG)__shOrt__(LoooonG)
output
5 2


input
5
(___)
output
0 0
#include<bits/stdc++.h>
using namespace std;
int a,f,k;
char s[300];
int ans1,ans2;
int main(){
	scanf("%d %s",&a,s);
	for(int i=0;s[i];i++){

		if(f==0){
			if(s[i]==‘(‘){
				f=1;
				ans1=max(ans1,k);
				k=0;
			}else if(s[i]==‘_‘){
				ans1=max(ans1,k);
				k=0;
			}else{
				k++;	
			}
		}else if(f==1){
			if(s[i]==‘)‘){
				f=0;	
			}else if(s[i]==‘_‘){
				
			}else if(s[i]<=‘Z‘&&s[i]>=‘A‘||s[i]<=‘z‘&&s[i]>=‘a‘){
				f=3;
				ans2++;
			}
		}else if(f==3){
			if(s[i]==‘)‘){
				f=0;
			}else if(s[i]==‘_‘){
				f=1;
			}
		}
	}
	ans1=max(ans1,k);
	printf("%d %d",ans1,ans2);
}#include<bits/stdc++.h>
using namespace std;
int a,f,k;
char s[300];
int ans1,ans2;
int main(){
	scanf("%d %s",&a,s);
	for(int i=0;s[i];i++){

		if(f==0){
			if(s[i]==‘(‘){
				f=1;
				ans1=max(ans1,k);
				k=0;
			}else if(s[i]==‘_‘){
				ans1=max(ans1,k);
				k=0;
			}else{
				k++;	
			}
		}else if(f==1){
			if(s[i]==‘)‘){
				f=0;	
			}else if(s[i]==‘_‘){
				
			}else if(s[i]<=‘Z‘&&s[i]>=‘A‘||s[i]<=‘z‘&&s[i]>=‘a‘){
				f=3;
				ans2++;
			}
		}else if(f==3){
			if(s[i]==‘)‘){
				f=0;
			}else if(s[i]==‘_‘){
				f=1;
			}
		}
	}
	ans1=max(ans1,k);//wa了一次因为没有计算到最后一个单词。
	printf("%d %d",ans1,ans2);
}

  

【Codeforces 723B】Text Document Analysis 模拟

原文:http://www.cnblogs.com/flipped/p/5929991.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!