码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 39J Spelling Check hash

时间:2014-07-18 22:27:50      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   2014   io   for   

题目链接:点击打开链接

题意:

给定2个字符串

选择第一个字符串的其中一个字母删除,使得2个字符串完全相同

问哪些位置可以选

思路:

hash求前缀后缀,然后枚举位置


#include <cstdio>
#include <algorithm>
#include<iostream>
#include<string.h>
#include <math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define mod 100000007
#define ll long long
#define N 1000050
char s[N], c[N];
vector <int> ans;
ll l1[N], r1[N], l2[N], r2[N];
int main(){
    int i, j;
    while(~scanf("%s",s+1)){
        scanf("%s",c+1);
        int len1 = strlen(s+1), len2 = strlen(c+1);
        if(len1 -1 != len2){puts("0");continue;}
        l1[0] = 0;
        for(i = 1; i <= len1; i++) {
            l1[i] = l1[i-1]*26 + s[i];
            if(l1[i]>=mod) l1[i] %= mod;
        }
        r1[len1+1] = 0;
        for(i = len1; i ; i--) {
            r1[i] = r1[i+1]*26 + s[i];
            if(r1[i]>=mod) r1[i] %= mod;
        }

        l2[0] = 0;
        for(i = 1; i <= len2; i++) {
            l2[i] = l2[i-1]*26 + c[i];
            if(l2[i]>=mod) l2[i] %= mod;
        }
        r2[len2+1] = 0;
        for(i = len2; i ; i--) {
            r2[i] = r2[i+1]*26 + c[i];
            if(r2[i]>=mod) r2[i] %= mod;
        }
        ans.clear();
        for(i = 1; i <= len1; i++) {
            ll a = l1[i-1] + r1[i+1];
            ll b = l2[i-1]+r2[i];
            if(a==b)ans.push_back(i);
        }

        cout<<ans.size()<<endl;
        for(i = 0; i <ans.size(); i++)
            printf("%d%c", ans[i], i==ans.size()-1?'\n':' ');
    }
    return 0;
}

Codeforces 39J Spelling Check hash,布布扣,bubuko.com

Codeforces 39J Spelling Check hash

标签:blog   http   os   2014   io   for   

原文地址:http://blog.csdn.net/qq574857122/article/details/37881757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!