Fang Fang hdoj 5455 (字符串)

211 篇文章 1 订阅
51 篇文章 0 订阅

 

Fang Fang
Time Limit : 1500/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 0
Problem Description
Fang Fang says she wants to be remembered.
I promise her. We define the sequence $F$ of strings.
$F_{0}\ =\ ``\texttt{f}",$
$F_{1}\ =\ ``\texttt{ff}",$
$F_{2}\ =\ ``\texttt{cff}",$
$F_{n}\ =\ F_{n-1}\ +\ ``f",\ for\ n\ >\ 2$
Write down a serenade as a lowercase string $S$ in a circle, in a loop that never ends.
Spell the serenade using the minimum number of strings in $F$, or nothing could be done but put her away in cold wilderness.

Input
An positive integer $T$, indicating there are $T$ test cases. Following are $T$ lines, each line contains an string $S$ as introduced above. The total length of strings for all test cases would not be larger than $10^6$.

Output
The output contains exactly $T$ lines. For each test case, if one can not spell the serenade by using the strings in $F$, output $-1$. Otherwise, output the minimum number of strings in $F$ to split $S$ according to aforementioned rules. Repetitive strings should be counted repeatedly.

Sample Input
  
  
8 ffcfffcffcff cffcfff cffcff cffcf ffffcffcfff cffcfffcffffcfffff cff cffc

Sample Output
  
  
Case #1: 3 Case #2: 2 Case #3: 2 Case #4: -1 Case #5: 2 Case #6: 4 Case #7: 1 Case #8: -1 [hint] Shift the string in the first test case, we will get the string "cffffcfffcff" and it can be split into "cffff", "cfff" and "cff". [/hint]
 
//题意:给定字符串,可以移动字符,但只能是从开头移到结尾,不能将其随意插入。
//问移动后有几个cff...字符串。
//但若出现情况1、2时输出-1.
// 1、字符串中有除c、f以外的字符
// 2、字符串中cff...个数小于2.
//还有,若字符串全为f,若f的个数num为奇数,输出(num/2)+1,否则输出num/2
 
//唉,技术就是不行啊!!!写了一晚上还是错的,明天继续。。。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2*1000010];
int b[1000100];
int main()
{
	int t,c1,f1,z,i,k,l1;
	int cot;	
	while(scanf("%d",&t)!=EOF)
	{
		int cnt=1;
		k=0;
		while(t--)
		{
			memset(b,0,sizeof(b));
			c1=f1=z=l1=k=0;
			cot=0;
			scanf("%s",a);
			int l=strlen(a);
			for(i=0;i<l;i++)
			{
				if(a[i]=='c')
					c1++;
				else if(a[i]=='f')
					f1++;
				else
					z++;
			}			
			printf("Case #%d: ",cnt++);
			if(z>0)
				printf("-1\n");
			else if(f1==l)
			{
				if(f1&1)
					printf("%d\n",l/2+1);
				else
					printf("%d\n",l/2);
			}
			else
			{
				for(i=0;i<l+l1;)
				{
					if(a[i]!='c')
					{
						a[l+l1]=a[i];
						l1++;
						i++;
					}
					else
						break;
				}
				for(i=l1;i<l+l1;)
				{
					i++;
					if(a[i]!='c')
					{
						b[k]++;
						i++;
					}
					else
					{
						if(b[k]>=2)
						{
							k++;
							i++;
							cot++;
						}
						else
						{
							printf("-1\n");
							break;
						}
					}
				}
				printf("%d\n",cot);
			}
		}
	}
	return 0;
}
 
//唉,终于对了,重新计数这块不行啊,还是不会灵活运用。
 
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2*1000010];
int main()
{
	int t,c,f,i,j,l1;
	int cnt=1;
	scanf("%d",&t);
	while(t--)
	{
		
		scanf("%s",a);
		printf("Case #%d: ",cnt++);
		int l=strlen(a);
		c=f=j=0;
		for(i=0;i<l;i++)
		{
			if(a[i]=='c')
				c++;
			else if(a[i]=='f')
				f++;
			else
				j++;
		}
		if(j!=0)
			printf("-1\n");
		else if(f==l)
		{
			if(f&1)
				printf("%d\n",f/2+1);
			else
				printf("%d\n",l/2);
		}
		else
		{
			for(i=0,l1=0;i<l+l1;)
			{
				if(a[i]!='c')
				{
					a[l+l1]=a[i];
					l1++;
					i++;
				}
				else
					break;
			}
			//for(i=l1;i<l+l1;i++)
			//	printf("%c",a[i]);
			//	printf("\n");
			int k=0,flag=1;			
			for(i=l1;i<l+l1;)
			{
				if(!flag)
					break;
				if(a[i]=='c')
				{
					int x=0;
					i++;
					while(a[i]=='f'&&i<l+l1)
					{
						x++;
						i++;
					}
					if(x<2)
					{
						flag=0;
						break;
					}
					k++;
				}
			}
			if(flag)
				printf("%d\n",k);
			else
				printf("-1\n");
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2*1000010];
int main()
{
	int t,c,f,i,j,l1;
	int cnt=1;
	scanf("%d",&t);
	while(t--)
	{
		
		scanf("%s",a);
		printf("Case #%d: ",cnt++);
		int l=strlen(a);
		c=f=j=0;
		for(i=0;i<l;i++)
		{
			if(a[i]=='c')
				c++;
			else if(a[i]=='f')
				f++;
			else
				j++;
		}
		if(j!=0)
			printf("-1\n");
		else if(f==l)
		{
			if(f&1)
				printf("%d\n",f/2+1);
			else
				printf("%d\n",l/2);
		}
		else
		{
			for(i=0,l1=0;i<l+l1;)
			{
				if(a[i]!='c')
				{
					a[l+l1]=a[i];
					l1++;
					i++;
				}
				else
					break;
			}
			//for(i=l1;i<l+l1;i++)
			//	printf("%c",a[i]);
			//	printf("\n");
			int k=0,flag=1;			
			for(i=l1;i<l+l1;)
			{
				if(!flag)
					break;
				if(a[i]=='c')
				{
					int x=0;
					i++;
					while(a[i]=='f'&&i<l+l1)
					{
						x++;
						i++;
					}
					if(x<2)
					{
						flag=0;
						break;
					}
					k++;
				}
			}
			if(flag)
				printf("%d\n",k);
			else
				printf("-1\n");
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值