Codeforces Round #338 (Div. 2) C. Running Track 字符串处理

题意:给两个字符串s1,s3。s3能否由s1或者s1的反字符串s2的某些连续的部分构成。例:s1 = “abc” s2 = “cbaabc” “cbaabc” = “cba” + “abc”.
思路:暴力就好,首先看”c”能不能在s1或者s2中找到,然后看”cb”能不能在s1或者s2中找到,如果能找到,继续这个过程。如果不能找到,”c”就是一个结果,”b”就是下一个字符串的第一个字符。

http://codeforces.com/contest/615/problem/C

/*********************************************
    Problem : Codeforces
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++) //遍历
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

char s1[2105],s2[2105],s3[2105];
char tmp[2105];
int lens ;

void get_s2() {
    rep(i,0,lens-1) {
        s2[lens-i-1] = s1[i];
    }
    s2[lens] = 0;
}

void input() {

}

int pos1 = 0 , pos2 = 0;
int prepos1 , prepos2;

int par[2105][2];
int pos = 0;

void get_pos() {
    prepos1 = pos1 ;
    prepos2 = pos2 ;
    if(pos > lens) {
        pos1 = pos2 = -1;
        return ;
    }
    pos1 = strstr(s1,tmp) - s1;
    pos2 = strstr(s2,tmp) - s2;
}  

void solve() {
    lens = strlen(s1);
    get_s2();
    int len = strlen(s3);
    int tot = 0;
    pos1 = -1 ; pos2 = -1;
    pos = 0;
    int ok = 0;
    s3[len] = '#';
    rep(i,0,len) {
        tmp[pos++] = s3[i];
        tmp[pos] = 0;
        get_pos();
        if(pos1 >= 0 || pos2 >= 0) {

        }
        else {
            ++ tot;
            if(prepos1 < 0 && prepos2 <0) {
                ok = 1;
                break;
            }
            if(prepos1 >= 0) {
                par[tot][0] = prepos1 + 1;
                par[tot][1] = prepos1 + strlen(tmp) - 1;
            }
            else {
                par[tot][0] = lens - prepos2 ;
                par[tot][1] = lens - prepos2 - strlen(tmp) + 2;
            }
            pos = 0;
            tmp[pos++] = s3[i];
            tmp[pos] = 0;
            get_pos();
        }
    }
    if(!ok) {
        printf("%d\n",tot);
        rep(i,1,tot) {
            printf("%d %d\n",par[i][0],par[i][1]);
        }
    }
    else {
        puts("-1");
    }
}

int main(void) {
    //freopen("a.in","r",stdin);
    while(~scanf("%s%s",s1,s3)) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值