标签:dea break mit 二分 tchar action second int which
2 abcdefghijklmnopqrstuvwxyz abcdab qwertyuiopasdfghjklzxcvbnm qwertabcde
abcdabcd qwertabcde
这道题弹了非常久的TLE,发现不能一个字符一个字符地输出。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
const int esize = 28;
int next[maxn],mid;
char mp[esize],str[maxn];
string str1,str2;
void getNext(){
next[0] = next[1] = 0;
int n = str1.size();
for(int i = 1,j; i < n; i++){
j = next[i];
while(j && str1[i] != str1[j]) j = next[j];
if(str1[i] == str1[j]) next[i+1] = j+1;
else next[i+1] = 0;
}
}
void KMP(int sta){
int n = strlen(str) , j = 0;
for(int i = sta; i < n; i++){
while(j && str1[j] != str[i]) j = next[j];
if(str1[j] == str[i]) j++;
if(j==str1.size()) break;
}
int k;
if(n%2==0) k = sta-j;
else k = sta-j-1;
for(int i = 0; i < k; i++){
str1 += mp[str[sta+i]-‘a‘];
str2 += str[sta+i];
}
cout<<str2<<str1<<endl;
}
int main(){
int ncase;
cin >> ncase;
char tmp[esize];
getchar();
while(ncase--){
scanf("%s%s",tmp,str);
int n = strlen(str);
if(n==0){
puts("");
continue;
}
str1.clear();
str2.clear();
for(int i = 0; i < 26; i++)
mp[tmp[i]-‘a‘] = char(‘a‘+i);
if(n%2==0) mid = n/2-1;
else mid = n/2;
for(int i = 0; i <= mid; i++){
str1 += mp[str[i]-‘a‘];
str2 += str[i];
}
getNext();
KMP(mid+1);
}
return 0;
}
HDU4300-Clairewd’s message(KMP前缀匹配后缀)
标签:dea break mit 二分 tchar action second int which
原文地址:http://www.cnblogs.com/brucemengbm/p/6729137.html