ZOJ 3810 Pretty Poem 分类: ACM 2015-05-17 14:40 83人阅读 评论(0) 收藏
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 symbolA,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 integerT 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
开始听了LJH的方法把ABABA和ABABCAB 转换成(AB()AB)A和(AB)(AB)(CAB)。就变成了:AA+A的前缀和AA+一个包含A后缀的C结果发现不行,前者还比较好弄,后者没办法把AB分开所以也就不能将A和B相等的情况否定。肯定会将一些AB相等的算作Yes
看了AC的正确代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map> using namespace std; void judge(string str) {
string sa1 , sa2 , sa3 , sb1 , sb2 , sb3 , sc ;
for(int la = 1 ; la < (int)str.length() ; ++la) {
for(int lb = 1 ; lb < (int)str.length() ; ++lb) {
if(3*la + 2*lb == (int)str.length()) {
sa1 = str.substr(0 , la) ;
sa2 = str.substr(la+lb , la) ;
sa3 = str.substr(2*(la+lb) , la) ;
sb1 = str.substr(la , lb) ;
sb2 = str.substr(2*la+lb , lb) ;
if(sa1 == sa2 && sa2 == sa3 && sb1 == sb2 && sa1 != sb1) {
printf("Yes\n") ;
return ;
}
}
if(3*la + 3*lb < (int)str.length()) {
int lc = (int)str.length()-3*la-3*lb ;
sa1 = str.substr(0 , la) ;
sa2 = str.substr(la+lb , la) ;
sa3 = str.substr(2*la+2*lb+lc , la) ;
sb1 = str.substr(la , lb) ;
sb2 = str.substr(2*la+lb , lb) ;
sb3 = str.substr(3*la+2*lb+lc , lb) ;
sc = str.substr(2*la+2*lb , lc) ;
if(sa1 == sa2 && sa1 == sa3 && sb1 == sb2 && sb1 == sb3 && sa1 != sb1 && sa1 != sc && sb1 != sc) {
printf("Yes\n") ;
return ;
}
}
}
}
printf("No\n") ;
} int main()
{
int t ;
scanf("%d" ,&t) ;
while(t--) {
string str ;
cin>>str ;
for(int i = 0 ; i < (int)str.length() ; ++i) {
if(isalpha(str[i]) == false) {
str.replace(i , 1 ,"") ;
--i ;
}
}
judge(str) ;
}
return 0;
}
用了STRING,但我对STRING还不是很熟,就想写一个不用STRING的,也用他们的这个思想。
但是写的过程出现了很多问题。
首先是模仿写一个str.substr(0 , la) ;开始写了一个这样的sub
char* sub(char* s,int a,int b)
{
char str[155];
int k=0;
for(int i=a;i<=b;i++)
{
str[k++]=s[i];
}
str[k]='\0';
return str;
}
但是编译器有一个警告,而且最后也没有AC我不知道原因是什么,自己试的结果都正确
警告的原因是char str[155];申请的空间会在函数结束销毁,正确的写法是
char * substr(const char * s, int n1, int n2) /*从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址*/
{
char * sp = (char*)malloc(sizeof(char) * (n2 - n1 + 2));
int i, j = 0; for (i = n1; i <= n2; i++)
{
sp[j++] = s[i];
}
sp[j] = 0;
return sp;
}</span>
这样申请堆空间,最后用完之后要手动释放
sub = substr(s, 0, 5); /*提取s[0]~s[5]元素组成新子串,并保存到sub中*/
free(sub);/*释放sub所占用的空间*/
</span>
更多有关返回一个字符串的方法参考另一篇文章:http://blog.csdn.net/turkeyzhou/article/details/6104135
最后我使用了函数参数而不适用返回值来得到字串
void sub(char str[],char s[],int a,int b)
{
int k=0;
for(int i=a;i<b+a;i++)
{
str[k++]=s[i];
}
str[k]='\0';
}</span>
最后也AC了。
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1555 void sub(char str[],char s[],int a,int b)
{
int k=0;
for(int i=a;i<b+a;i++)
{
str[k++]=s[i];
}
str[k]='\0';
} int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
char a[N],b[N];
cin>>b;
int k=0;
for(int i=0;i<(int)strlen(b);i++)
{
if( (b[i]>='A'&&b[i]<='Z') || (b[i]>='a'&&b[i]<='z'))
a[k++]=b[i];
}
a[k]='\0';
char a1[N],a2[N],a3[N],b1[N],b2[N],b3[N],c[N];
int len=strlen(a);
int flag=0;
for(int i=1;i<=len/2;i++){
if(flag)break;
for(int j=1;j<=len/2;j++){
if(3*i+2*j==len){
sub(a1,a,0,i);
sub(b1,a,i,j);
sub(a2,a,i+j,i);
sub(b2,a,i+j+i,j);
sub(a3,a,i+j+i+j,i);
if(strcmp(a1,a2)==0&&strcmp(a2,a3)==0&&strcmp(b1,b2)==0&&strcmp(a1,b1)!=0){
flag=1;
break;
}
}
}
}
for(int i=1;i<=len/2;i++){
if(flag==2)break;
for(int j=1;j<=len/2;j++){
if(flag==2)break;
for(int k=1;k<=len/2;k++){
if(3*i+3*j+k==len){
sub(a1,a,0,i);
sub(b1,a,i,j);
sub(a2,a,i+j,i);
sub(b2,a,i+j+i,j);
sub(c,a,2*i+2*j,k);
sub(a3,a,2*i+2*j+k,i);
sub(b3,a,3*i+2*j+k,j);
if(strcmp(a1,a2)==0&&strcmp(a2,a3)==0&&strcmp(b1,b2)==0&&strcmp(b2,b3)==0&&strcmp(a1,b1)!=0&&strcmp(a1,c)!=0&&strcmp(b1,c)!=0){
flag=2;
break;
}
}
}
}
}
if(flag)puts("Yes");
else puts("No");
}
return 0;
}
不过AC之前我还犯了一个错误,再判断ABABCAB这种时,我只比较了b1和b2相同,没有比较b2和b3相同,结果抓耳挠腮了半天不知道WA的原因,还是不够仔细。
if(strcmp(a1,a2)==0&&strcmp(a2,a3)==0&&strcmp(b1,b2)==0&&strcmp(b2,b3)==0&&strcmp(a1,b1)!=0&&strcmp(a1,c)!=0&&strcmp(b1,c)!=0){
开始少了&&strcmp(b2,b3)==0这句!
版权声明:本文为博主原创文章,未经博主允许不得转载。
ZOJ 3810 Pretty Poem 分类: ACM 2015-05-17 14:40 83人阅读 评论(0) 收藏的更多相关文章
- iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏
1. 问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Beautiful People 分类: Brush Mode 2014-10-01 14:33 100人阅读 评论(0) 收藏
Beautiful People Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 《将博客搬至CSDN》 分类: 勉励自己 2014-09-05 14:29 43人阅读 评论(0) 收藏
搬家啦,上博客园关注我哦http://www.cnblogs.com/AsuraRoute 版权声明:本文为博主原创文章,未经博主允许不得转载.
- sql 视图 按where条件多个字段取一个 分类: SQL Server 2014-12-01 14:09 308人阅读 评论(0) 收藏
首先介绍一下 Case ..When...Then..End 的用法: CASEJiXiaoFind_RowID WHEN '1' THENJiXiao_Money1 WHEN '2' THEN ...
随机推荐
- OpenStack介绍
简介 OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管 ...
- MFC弹出菜单隐藏解决
http://social.msdn.microsoft.com/Forums/en-US/5482103e-272b-4c9f-bac4-be15f14782bd/cmfcmenubar-remov ...
- RIA技术
Rich Internet Application(富互联网应用程序,简称RIA),一种全新的Web应用程序架构,它结合了桌面软件良好的用户体验和web应用程序易部署的优点,很快获得了企业的青睐. 近 ...
- [ionic开源项目教程] - 第1讲 前言,技术储备,环境搭建,常用命令
前言 这是一个系列文章,将持续更新到项目完结,从环境搭建开始讲解,包括实战开发中遇到的各种问题的解决方案,都将毫无保留的分享给大家. 技术储备 开始本项目之前,请确保自己对以下技术点都有所了解. ht ...
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
- LeetCode Valid Number 有效数字(有限自动机)
题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...
- 20160130.CCPP体系详解(0009天)
程序片段(01):hanoi.c+汉诺塔.c 内容概要:汉诺塔 ///hanoi.c #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...
- Linux Shell编程(3):数组
http://snailwarrior.blog.51cto.com/680306/154704 BASH只支持一维数组,但参数个数没有限制. 声明一个数组:declare -a array (其 ...
- phonegap环境搭建
最近在开发app, html5+php 采用phonegap进行打包 前端框架采用jquery mobile 这里phonegap创建安卓项目 3种方式 1.phonegap 2.cordova 3. ...
- nginx的配置,要求根据不同的来路域名,发送到不同的端口去处理
这一台电脑上既有tomcat 也有 apache,他俩是没有办法同时享用80端口的.我现在让tomcat用8088,apache用8080,然后让nginx用80,这样nginx在收到请求后,根据不同 ...