ZOJ 3813 Pretty Poem (暴力)

Pretty Poem


Time Limit: 2 Seconds     
Memory Limit: 65536 KB



Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can even write poetic code. Some poems has a strict
rhyme scheme like "ABABA" or "ABABCAB". For example, "niconiconi" is composed of a rhyme scheme "ABABA" with A = "ni" and B = "co".

More technically, we call a poem pretty if it can be decomposed into one of the following rhyme scheme: "ABABA" or "ABABCAB". The symbol
A, B and C are different continuous non-empty substrings of the poem. By the way, punctuation characters should be ignored when considering the rhyme scheme.

You are given a line of poem, please determine whether it is pretty or not.

Input

There are multiple test cases. The first line of input contains an integer
T indicating the number of test cases. For each test case:

There is a line of poem S (1 <= length(S) <= 50).
S will only contains alphabet characters or punctuation characters.

Output

For each test case, output "Yes" if the poem is pretty, or "No" if not.

Sample Input

3
niconiconi~
pettan,pettan,tsurupettan
wafuwafu

Sample Output

Yes
Yes
No

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3818

解题思路:字符串处理,直接暴力枚举A,B,C,直接判断是否满足两种情况。注意A,B,C各不相同,我是两种情况分类开处理,第二种 情况时把AB看成一个子串直接判断了,但还要判断AB能分解成两个不同子串子串A,B且不和C相同。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <string>
using namespace std;
//const int maxn=205;
int main(void)
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		string s,a;
		cin>>s;
		int p=0,n=0;
		int len=s.length();
		for(int i=0;i<len;i++)
			if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')
			{
				a+=s[i];
				n++;
			}
		for(int l1=1;l1<=n;l1++)  //第一种情况ABABA的判断
		{
			string A=a.substr(0,l1);   //子串A总是从第一个位置开始的
			for(int l2=1;l2<=n;l2++)
			{
				if(l1+l2+l1+l2+l1>n)    //长度和大于主串,不必再往后枚举
					break;
				string B=a.substr(l1,l2);  //子串B总是紧挨着子串A
				if(a==A+B+A+B+A&&A!=B)
				{
					p=1;
					break;
				}
			}
			if(p)
				break;
		}
		if(p)
		{
			printf("Yes\n");
			continue;
		}
		for(int l1=2;l1<=n;l1++)    //第二种情况ABABCAB的判断
		{
			string AB=a.substr(0,l1);   //把AB看成一个子串
			for(int l2=1;l2<=n;l2++)
			{
				if(l1+l1+l2+l1>n)break;
				string C=a.substr(2*l1,l2);
				if(a==AB+AB+C+AB)       //满足条件时进一步判断AB能分解成不同的子串A和B
				{
					int lab=AB.length();
					for(int k=1;k<lab;k++)
					{
						string A=AB.substr(0,k);
						string B=AB.substr(k,lab-k);
						if(A!=C&&A!=B&&B!=C)
						{
							p=1;
							break;
						}
					}
					if(p)
						break;
				}
			}
			if(p)break;
		}
		if(p)
			printf("Yes\n");
		else
			printf("No\n");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 20:31:35

ZOJ 3813 Pretty Poem (暴力)的相关文章

ZOJ 3818 Pretty Poem (暴力模拟 string(substr))

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

ZOJ 3813 Alternating Sum (牡丹江网络赛E题)

ZOJ 3813 Alternating Sum 题目链接 赛后补题中,这题真心恶心爆了 先推下公式,发现是隔一个位置,长度从最长每次减2,这样累加起来的和,然后就可以利用线段树维护,记录4个值,奇数和,偶数和,奇数答案和,偶数答案和,这样pushup的时候,对应要乘系数其实就是加上左边奇(偶)和乘上右边长度,线段树处理完,还有个问题就是查询可能横跨很多个区间,这样一来就要把区间进行分段,分成3段,然后和上面一样的方法合并即可,注意中间一段很长,不能一一去合并,可以推成等差数列,利用前n项和去搞

zoj 3813 Alternating Sum(线段树)

题目链接:zoj 3813 Alternating Sum 题目大意:给定一个P,S是以P为循环的无限串,定义G(i,j),现在有两种操作: 1 x d:将P中x的位置变为d 2 l r:查询S中l-r之间所有的G(i, j)的和 解题思路:线段树的区间查询点修改. 根据G(i,j)的公式可以推导出:每次查询l~r这段区间的答案为: 奇数:sl?len+sl+2?(len?2)+sl+4?(len?4)+?+sr?1 偶数:sl?len+sl+2?(len?2)+sl+4?(len?4)+?+s

zoj 3818 Pretty Poem(暴力处理字符串)2014年牡丹江赛区网络赛

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

zoj 3818 Pretty Poem (模拟)

ZOJ Problem Set - 3818 Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM

ZOJ 3818 Pretty Poem

暴力模拟 细节处理很重要... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 char s1[100],s[100]; 7 int len1,len; 8 int t; 9 char a[55],b[55],c[55]; 10 11 int solved1 (int l,int len){ 12 int temp=0,j; 13 for

ZOJ3818-Pretty Poem(暴力枚举)

题目链接 题意:求所给字符串是否符合ABABA或者ABABCAB的形式,如果可以的话输出Yes,不可以的话为No. 思路:暴力枚举A和B的长度,再用从长度减去3倍的AB长度,即为C的长度,看组合而成的字符串是否与给定的相等.在这里string中的substr函数是个好东西. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <a

【瞎搞】ZOJ 3818 Pretty Poem 牡丹江网络赛J题

第一种情况:ABABA. 先判断开头的A与结尾的A,得到A的长度,接着判断ABAB 中的AB与AB是否相同(ABAB的长度一定为偶数) 已经知道了A长度,AB的长度 接着判断下A 与B是否相同 第二种情况:ABABCAB-可先讲AB看成整体即DDCD 若存在一个D满足条件 可得到C的长度和位置再判断A-B是否相同A-C是否相同 B-C是否相同(暴力取A的长度咯) #include <stdio.h> #include <string.h> #include <stdlib.h

ZOJ 2856 Happy Life 暴力求解

因为是Special Judge 的题目,只要输出正确答案即可,不唯一 暴力力求解, 只要每次改变 happiness 值为负的人的符号即可. 如果计算出当前人的 happiness 值为负,那么将其 p(i) 值赋值为-p(i) 即可这题是保证有解的,至至于为何难以证明. Source Code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #incl