zoj 3818 2014牡丹江网赛

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5350

昨天写死都是WA,今天按枚举a,b,c的长度以及调用string类的函数substr,1A.....

还是调用系统函数代码能短很多而且代码短意味着出错机会少&思考的逻辑更容易......

写贴个昨天的WA代码  还是不明白为啥WA.....

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
//#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
//const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;
const int MAXN = 1000+100;

char str[MAXN],ss[MAXN],aa[MAXN],bb[MAXN],cc[MAXN];
int posa[MAXN],len,acnt;

void cala()
{
    acnt=0;
    for(int i=0;i<len;i++)
        if(str[i]==str[0])
            posa[acnt++]=i;
}

int test1()
{
    if(len<5)return 0;
    for(int i=acnt-1;i>1;i--)
    {
        int pos=posa[i],flag=1;
        if(pos%2 || pos<4)continue;
        strcpy(aa,str+pos);
        int lena=strlen(aa),pp=pos/2,j;
        if(lena>=pp)continue;
        for(j=0;j<lena;j++)
            if(str[j]!=str[pos+j])
            {
                flag=0;
                break;
            }
        if(!flag)continue;
        int lenb=0;
        for(j=lena;j<pp;j++)
            bb[lenb++]=str[j];
        bb[lenb]='\0';
        if(lenb==0 || lena==0 || strcmp(aa,bb)==0 || lena*3+lenb*2!=len)continue;//
        for(int j=pp;j<pos;j++)
            if(str[j-pp] != str[j])
            {
                flag=0;
                break;
            }

        if(flag)
        {
            //printf("oo#first#  %d a=%d b=%d pos=%d\n",i,lena, lenb,pos);
            return 1;
        }
    }
    return 0;
}

int test2()
{
    if(len<7)return 0;
    for(int i=acnt-1;i>1;i--)
    {
        int pos=posa[i],flag=1;
        strcpy(aa,str+pos);
        int lenab=strlen(aa),j;
        if(lenab<2 || lenab*3>=len || lenab>=pos-1)continue;//
        for(j=0;j<lenab;j++)
            if(str[j] != str[j+lenab])
            {
                flag=0;
                break;
            }
        if(!flag || lenab*2>=pos)continue;
        for(j=lenab;j<lenab*2;j++)//
            if(str[j] != str[j-lenab])
            {
                flag=0;
                break;
            }
        if(!flag)continue;
        int lenc=0;
        for(int j=lenab*2;j<pos;j++)
            cc[lenc++]=str[j];
        cc[lenc]='\0';
        int f2=0;
        int lena=0,lenb=0;
        for(int j=1;j<lenab;j++)
        {
            lena=0,lenb=0;
            for(int k=0;k<j;k++)
            {
                aa[lena++]=str[k];
            }
            aa[lena]='\0';
            for(int k=j;k<lenab;k++)
                bb[lenb++]=str[k];
            bb[lenb]='\0';
            if(strcmp(aa,bb)==0 || strcmp(bb,cc)==0 || strcmp(aa,cc)==0 || lenb==0 || lena==0 || lenc==0 || lena*3+lenb*3+lenc!=len)continue;
            f2=1;
            break;
        }
        if(flag && f2)
        {
            //printf("#second# lenab=%d lena=%d lenb=%d lenc=%d\n",lenab,lena,lenb,lenc);
            return 1;
        }
    }
    return 0;
}

int main()
{
    //IN("J.txt");
    int ncase;
    int cnt;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%s",ss);
        int ll = strlen(ss);
        cnt=0;
        for(int i=0;i<ll;i++)
            if(isalpha(ss[i]))
                str[cnt++]=ss[i];
        str[len=cnt]='\0';
        ///////////
       // printf("## %s %d\n",str,len);
        ///////////
        cala();
        if(test1() || test2())
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

再贴个AC代码

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;

int test1(string s)
{
    if(s.size()<5)return 0;
    int lena=0,lenb=0;
    for(lena=1;lena<s.size()/2;lena++)
    {
        lenb=0;
        string a=s.substr(0,lena);
        for(int j=lena;j<s.size()/2 && (lenb=j-lena+1) && lena*3+lenb*2<=s.size();j++)
        {
            string b=s.substr(lena,lenb);
            string a2=s.substr(lena+lenb,lena);
            string b2=s.substr(lena*2+lenb,lenb);
            string a3=s.substr(lena*2+lenb*2,lena);
            if(lena*3+lenb*2==s.size() && a!=b && a==a2 && a==a3 && b==b2)return 1;
        }
    }
    return 0;
}

int test2(string s)
{
    if(s.size()<7)return 0;
    int lena,lenb,lenc;
    for(lena=1;lena<s.size()/2;lena++)
    {
        string a=s.substr(0,lena);
        for(lenb=1;lenb<s.size()/2 && (lena+lenb)*3<s.size();lenb++)
        {
            string b=s.substr(lena,lenb);
            string a2=s.substr(lena+lenb,lena);
            string b2=s.substr(lena*2+lenb,lenb);
            if(a==a2&& b==b2 && a!=b)
                for(lenc=1;lenc<s.size()/2 && (lena+lenb)*3+lenc<=s.size();lenc++)
                {
                    string c=s.substr((lena+lenb)*2,lenc);
                    string a3=s.substr((lena+lenb)*2+lenc,lena);
                    string b3=s.substr(lena*3+lenb*2+lenc,lenb);
                    if(a==a3 && b==b3 && a!=c && b!=c && (lena+lenb)*3+lenc==s.size())return 1;
                }
        }
    }
    return 0;
}

int main()
{
    //IN("J.txt");
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        string ss,str;
        cin >> ss;
        for(int i=0;i<ss.size();i++)
            if(isalpha(ss[i]))
                str+=ss[i];
        if(test1(str) || test2(str))
            puts("Yes");
        else
            puts("No");
    }
    return 0;

}
时间: 2024-10-05 14:43:48

zoj 3818 2014牡丹江网赛的相关文章

zoj 3817 2014牡丹江网赛 字符串哈希

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5349 比赛的时候没看这道题,遗憾,不过想到算法不难,但是写代码比较考代码能力,我自己写了两次都不行,还是看了别人代码,写下了这个 学到: 1.hash字符串第一个下表为0的空起来,写起来方便 2.hash匹配的全部情况就是以text串的所有位置为起点做匹配,当text比patern小而且text可以循环时,这么确定 3.dfs的时候,为了加快,可以预估计至少需要匹配多长 in

zoj 3809 枚举水题 (2014牡丹江网赛 A题)

题目大意:给出一列取样的几个山的高度点,求山峰有几个? Sample Input 291 3 2 4 6 3 2 3 151 2 3 4 5Sample Output 30 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 us

zoj 3819(2014牡丹江区域赛 A题 )

题意:给出A班和B班的学生成绩,如果bob(A班的)在B班的话,两个班级的平均分都会涨.求bob成绩可能的最大,最小值. A班成绩平均值(不含BOB)>A班成绩平均值(含BOB) && B班成绩平均值(不含BOB)< B班成绩平均值(含BOB) 化简后得 B班成绩平均值(不含BOB) < X < A班成绩平均值(不含BOB) Sample Input 24 35 5 54 4 36 55 5 4 5 31 3 2 2 1Sample Output 4 42 4 1

ZOJ 3827 Information Entropy (2014牡丹江区域赛)

题目链接:ZOJ 3827 Information Entropy 根据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <string.h> #include <math.h> const double e=exp(1.0); double find(char op[]) { if(op[0]=='b') return 2.0; else if(op[0]=='n') return e; else if(op[0]=='

ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure t

ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns. Every day after work, Edward will place

2014牡丹江 现场赛 F zoj 3824 Fiber-optic Network

首先赞一下题目, 好题 题意: Marjar University has decided to upgrade the infrastructure of school intranet by using fiber-optic technology. There are N buildings in the school. Each building will be installed with one router. These routers are connected by optic

zoj 3822 Domination(2014牡丹江区域赛D题)

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar