structRecord{intcount;intnextIndex;Record(intcount,intnextIndex):count(count),nextIndex(nextIndex){}};classSolution{public:intgetMaxRepetitions(strings1,intn1,strings2,intn2){vector<Record>records;// [count(s1 matches s2[i:]), next index of s2[i:]]for(inti=0;i<s2.length();++i){intcount=0;intnextIndex=i;for(intj=0;j<s1.length();++j)if(s2[nextIndex]==s1[j])if(++nextIndex==s2.length()){// Have a match++count;nextIndex=0;}records.emplace_back(count,nextIndex);}intmatches=0;// S1 matches s2intindex=0;while(n1--){matches+=records[index].count;index=records[index].nextIndex;}returnmatches/n2;// S1 matches S2}};
classRecord{publicintcount;publicintnextIndex;publicRecord(intcount,intnextIndex){this.count=count;this.nextIndex=nextIndex;}};classSolution{publicintgetMaxRepetitions(Strings1,intn1,Strings2,intn2){List<Record>records=newArrayList<>();// [count(s1 matches s2[i:]), next index of s2[i:]]for(inti=0;i<s2.length();++i){intcount=0;intnextIndex=i;for(intj=0;j<s1.length();++j)if(s2.charAt(nextIndex)==s1.charAt(j))if(++nextIndex==s2.length()){// Have a match++count;nextIndex=0;}records.add(newRecord(count,nextIndex));}intmatches=0;// S1 matches s2intindex=0;while(n1-->0){matches+=records.get(index).count;index=records.get(index).nextIndex;}returnmatches/n2;// S1 matches S2}}