poj 3602-字符串模拟

本文介绍了一种算法,用于计算特定文本排版中使用到的字形数量,考虑了不同类型的连字(如 ff, fi, ffi, fl 和 ffl),并解释了如何正确识别这些连字及特殊字符。
摘要由CSDN通过智能技术生成


D - Typographical Ligatures
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

Typesetting involves the presentation of textual material in graphic form on paper or some other medium. Close as it is related to our daily life, typesetting exhibits certain complexities which may be unfamiliar and sometimes unimaginable to the layman. The use of typographical ligatures is one such complexity.

Compare the two appearances of the word “define” in Figure 7. Figure 7(a) illustrates the word typeset with the “fi” ligature as Knuth’s TeX does. The letters “f” and “i” are combined into a single glyph. Figure 7(b) shows the word typeset without the ligature as Microsoft Word does. The letters “f” and “i” remain separate. Other examples include the “ff”, “ffi”, “fl” and “ffl” ligatures, as shown in Figure 8. Perhaps the most notable ligature in active use is the “&” (ampersand), which originated from “et”, the Latin word for “and”.

define define
(a) (b)

Figure 7: The word “define” typeset with and without the “fi” ligature

effect office reflect waffle

Figure 8: Examples of the “ff”, “ffi”, “fl” and “ffl” ligatures

Ligatures are primarily intended to improve spacing between letters. Despite that their impact on legibility is debated and that their use is declining, some people insist that they are an essential part of quality typesetting. In order to typeset ligatures, separate glyphs have to be used since ligatures generally differ from direct combinations of their constituent letters in most cases.

Given some text, count the number of glyphs that have to be used to typeset it. Only the “ff”, “fi”, “ffi”, “fl”, and “ffl” ligatures are considered. Note that they are case-sensitive. Ligatures are recognized following the leftmost longest rule—find the leftmost match, then the longest match if there are any ties. Each distinct letter, ligature or punctuation mark requires a separate glyph. However, if a letter appears only in ligatures and nowhere else, it shall not be assigned a glyph. Furthermore, left single and double quotes differ from their right counterparts. Spaces do not require a glyph.

Input

The input contains a single test case consisting of some text. The text is given on multiple lines not longer than 100 characters each. The text contains only letters (lowercase and uppercase), punctuation marks and spaces. Punctuation marks include periods, commas, semicolons, colons, single and double quotes, exclamation and question marks. Quotes are represented by “`” (left single quote), “'” (right single quote), “``” (left double quote) and “''” (right double quote), respectively. They are also recognized following the leftmost longest rule. The input ends where EOF is met.

Output

Print the number of glyphs that have to be used to typeset the given text.

Sample Input

```define, effect; office.
reflect? waffle!'''

Sample Output

23


代码:

#include<stdio.h>
#include<string.h>

char s[1000];
int count[1000];
int ff[7];
int len;

void solve(){
	int ans=0;
	memset(count,0,sizeof(count));
	memset(ff,0,sizeof(ff));
	for(int i=0;i<len;i++){
		if(s[i]=='f'){
			if(s[i+1]=='f' && (s[i+2]=='i' || s[i+2]=='l')){
				if(s[i+2]=='i'){
					ff[2]++;
				}
				if(s[i+2]=='l'){
					ff[4]++;
				}		
				i+=2;	
			}	
			else if(s[i+1]=='f'){
				ff[0]++;
				i+=1;			
			}
			else if(s[i+1]=='i' || s[i+1]=='l'){
				if(s[i+1]=='i')ff[1]++;
				if(s[i+1]=='l')ff[3]++;
				i++;			
			}	
			else count[s[i]]++;
		}
		else if(s[i]=='`' && s[i+1]=='`'){
			ff[6]++;
			i++;			

		}
		else if(s[i]=='\'' && s[i+1]=='\''){
			ff[5]++;
			i++;				

		}	
		else count[s[i]]++;
	}
	for(int i=0;i<=256;i++){
		if(count[i])ans++;
	}
	for(int i=0;i<7;i++)if(ff[i])ans++;
	printf("%d\n",ans);
}


int main(){
	char c;
	len=0;
	while(scanf("%c",&c)!=EOF){
		if(c==' ' || c=='\n')continue;
		s[len++]=c;			
	}
	//printf("%s",s);
	solve();
}


### C++ 字符串哈希练习题 #### 一、字符串哈希基础概念 字符串哈希是一种将字符串映射为整数的技术,通过这种方式可以快速比较两个字符串是否相等或查找子串等问题。通常情况下,会选取一个基数`base`来模拟多进制转换过程,并利用模运算防止数值溢出。 #### 二、经典例题解析 ##### AcWing 841. 字符串哈希[^1] 此题作为一道典型的字符串哈希入门题目,主要考察如何构建并应用简单的字符串哈希函数处理给定问题。对于长度较大的文本匹配场景尤为适用。 ```cpp const int N = ...; unsigned long long h[N], p[N]; // 初始化p数组, 计算以base为底的幂次方表 void init() { p[0] = 1; for (int i = 1; i < N; ++i) p[i] = p[i - 1] * base % mod; } // 获取区间[l,r]对应的hash值 unsigned long long get(int l, int r) { return (h[r] - h[l - 1] * p[r - l + 1]) % mod; } ``` 上述代码片段展示了基于前缀和的方式预处理字符串哈希值的方法,其中`mod`用于取余操作确保不会发生越界错误;而`get()`方法则实现了任意区间的哈希查询功能。 #### 三、实战演练建议 为了更好地掌握这一知识点,推荐尝试以下几类具有代表性的习题: - **单模式串匹配**:如POJ 2774 DNA Sequence,这类题目往往涉及在一个较长的目标序列中定位某个特定模式串的位置。 - **多重模式串匹配**:例如HDU 3746 Caesar Cipher Plus,在此类挑战里可能需要同时考虑多个不同长度的模式串与目标串之间的关系。 - **最长公共前后缀/回文串检测**:像Codeforces Round #XXX Div. Y Problem Z这样的竞赛真题也值得深入研究,它们能够很好地锻炼选手灵活运用字符串哈希技巧的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值