import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
StringTokenizer st=new StringTokenizer(line,",");
String str=st.nextToken();
String temp=st.nextToken();
StringBuilder stb=new StringBuilder();
LinkedList<String> list=new LinkedList<String>();
for(int i=0;i<temp.length();i++){
char c=temp.charAt(i);
if(i==0){
if(c!='*'){
stb.append(c);
}
if(i<temp.length()-1){
if(c=='\\'&&temp.charAt(i+1)=='*'){
stb.deleteCharAt(stb.length()-1);
}
}
}else if(i== temp.length()-1){
if(c=='*'){
if(temp.charAt(i-1)=='\\'){
stb.append(c);
}
}else{
stb.append(c);
}
}else{
if(c=='*'){
if(temp.charAt(i-1)=='\\'){
stb.append(c);
}else{
list.add(stb.toString());
stb=new StringBuilder();
}
}else if(c=='\\'){
if(temp.charAt(i+1)!='*'){
stb.append(c);
}
}else{
stb.append(c);
}
}
}
if(stb.length()!=0){
list.add(stb.toString());
}
sb.append(findThesePatterns(str, list)).append('\n');
}
System.out.print(sb);
}
static boolean findThesePatterns(String Str,LinkedList<String> pattern){
LinkedList<LinkedList<Integer>> list=new LinkedList<LinkedList<Integer>>();
LinkedList<Integer> length=new LinkedList<Integer>();
while(!pattern.isEmpty()){
String val=pattern.remove();
list.add(searchForPattrern(Str,val));
length.add(val.length());
}
return canBeAfter(0, -1, list, length);
}
static boolean canBeAfter(int j,int index,LinkedList<LinkedList<Integer>> list,LinkedList<Integer> length){
if(j==length.size()){
return true;
}
LinkedList<Integer> less=list.get(j);
int lengthL=length.get(j);
for(int val:less){
if(val>index){
boolean res=canBeAfter(j+1,val+lengthL,list, length);
if(res){
return true;
}
}
}
return false;
}
static LinkedList<Integer> searchForPattrern(String val,String pattern){
LinkedList<Integer> index=new LinkedList<Integer>();
for(int i=0;i<val.length()-pattern.length()+1;i++){
int counter=0;
for(int j=0;j<pattern.length();j++){
if(val.charAt(i+j)!=pattern.charAt(j)){
break;
}
counter++;
}
if(counter==pattern.length()){
index.add(i);
}
}
return index;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
StringTokenizer st=new StringTokenizer(line,",");
String str=st.nextToken();
String temp=st.nextToken();
StringBuilder stb=new StringBuilder();
LinkedList<String> list=new LinkedList<String>();
for(int i=0;i<temp.length();i++){
char c=temp.charAt(i);
if(i==0){
if(c!='*'){
stb.append(c);
}
if(i<temp.length()-1){
if(c=='\\'&&temp.charAt(i+1)=='*'){
stb.deleteCharAt(stb.length()-1);
}
}
}else if(i== temp.length()-1){
if(c=='*'){
if(temp.charAt(i-1)=='\\'){
stb.append(c);
}
}else{
stb.append(c);
}
}else{
if(c=='*'){
if(temp.charAt(i-1)=='\\'){
stb.append(c);
}else{
list.add(stb.toString());
stb=new StringBuilder();
}
}else if(c=='\\'){
if(temp.charAt(i+1)!='*'){
stb.append(c);
}
}else{
stb.append(c);
}
}
}
if(stb.length()!=0){
list.add(stb.toString());
}
sb.append(findThesePatterns(str, list)).append('\n');
}
System.out.print(sb);
}
static boolean findThesePatterns(String Str,LinkedList<String> pattern){
LinkedList<LinkedList<Integer>> list=new LinkedList<LinkedList<Integer>>();
LinkedList<Integer> length=new LinkedList<Integer>();
while(!pattern.isEmpty()){
String val=pattern.remove();
list.add(searchForPattrern(Str,val));
length.add(val.length());
}
return canBeAfter(0, -1, list, length);
}
static boolean canBeAfter(int j,int index,LinkedList<LinkedList<Integer>> list,LinkedList<Integer> length){
if(j==length.size()){
return true;
}
LinkedList<Integer> less=list.get(j);
int lengthL=length.get(j);
for(int val:less){
if(val>index){
boolean res=canBeAfter(j+1,val+lengthL,list, length);
if(res){
return true;
}
}
}
return false;
}
static LinkedList<Integer> searchForPattrern(String val,String pattern){
LinkedList<Integer> index=new LinkedList<Integer>();
for(int i=0;i<val.length()-pattern.length()+1;i++){
int counter=0;
for(int j=0;j<pattern.length();j++){
if(val.charAt(i+j)!=pattern.charAt(j)){
break;
}
counter++;
}
if(counter==pattern.length()){
index.add(i);
}
}
return index;
}
}
No comments:
Post a Comment