/** * Extract all capture groups of the first/last occurence of a regex pattern in each string * * @param str character vector * @param pattern character vector * @param opts_regex list * @param firs logical - search for the first or the last occurence? * @return character matrix * * @version 0.1 (Marek Gagolewski, 2013-06-22) */ SEXP stri__match_firstlast_regex(SEXP str, SEXP pattern, SEXP opts_regex, bool first) { str = stri_prepare_arg_string(str, "str"); // prepare string argument pattern = stri_prepare_arg_string(pattern, "pattern"); // prepare string argument R_len_t vectorize_length = stri__recycling_rule(true, 2, LENGTH(str), LENGTH(pattern)); uint32_t pattern_flags = StriContainerRegexPattern::getRegexFlags(opts_regex); UText* str_text = NULL; // may potentially be slower, but definitely is more convenient! STRI__ERROR_HANDLER_BEGIN StriContainerUTF8 str_cont(str, vectorize_length); StriContainerRegexPattern pattern_cont(pattern, vectorize_length, pattern_flags); vector< vector<charptr_x2> > occurences(vectorize_length); // we don't know how many capture groups are there R_len_t occurences_max = 1; for (R_len_t i = pattern_cont.vectorize_init(); i != pattern_cont.vectorize_end(); i = pattern_cont.vectorize_next(i)) { STRI__CONTINUE_ON_EMPTY_OR_NA_STR_PATTERN(str_cont, pattern_cont, /*do nothing*/;, int pattern_cur_groups = pattern_cont.getMatcher(i)->groupCount(); if (occurences_max < pattern_cur_groups+1) occurences_max=pattern_cur_groups+1; )
int main(){ int i,j,k,a[300],n ; scanf("%d",&n) ; for(i=0;i<n;i++){scanf("%d",&a[i]) ;} occurences(a,n) ; }