设计思路
- 内存管理:由于文件非常大,不能一次性将整个文件读入内存。采用逐行读取的方式,避免内存溢出。
- 数据结构选择:使用哈希表来存储字符串及其出现的次数,哈希表可以快速查找和插入字符串,平均时间复杂度为O(1)。
- 排序:将哈希表中的数据提取到数组中,然后使用排序算法按照出现次数从高到低排序。
关键代码片段
program count_and_sort_strings
implicit none
type string_count_type
character(len=:), allocatable :: string
integer :: count
end type string_count_type
type(string_count_type), allocatable :: hash_table(:)
integer, parameter :: hash_table_size = 1000000
integer :: i, j, n, hash_value
character(len=1000) :: line
logical :: found
! 初始化哈希表
allocate(hash_table(hash_table_size))
hash_table%count = 0
! 逐行读取文件
open(unit=10, file='large_text_file.txt', status='old')
do
read(10, '(a)', end=100) line
hash_value = hash_function(line)
found = .false.
do i = 1, hash_table(hash_value)%count
if (hash_table(hash_value)%string(i) == line) then
hash_table(hash_value)%count(i) = hash_table(hash_value)%count(i) + 1
found = .true.
exit
end if
end do
if (.not. found) then
hash_table(hash_value)%count = hash_table(hash_value)%count + 1
if (hash_table(hash_value)%count == 1) then
allocate(hash_table(hash_value)%string(1))
else
allocate(hash_table(hash_value)%string(1:hash_table(hash_value)%count), source=hash_table(hash_value)%string)
end if
hash_table(hash_value)%string(hash_table(hash_value)%count) = line
end if
end do
100 close(10)
! 将哈希表数据提取到数组中
n = 0
do i = 1, hash_table_size
n = n + hash_table(i)%count
end do
type(string_count_type), allocatable :: result(:)
allocate(result(n))
n = 0
do i = 1, hash_table_size
do j = 1, hash_table(i)%count
n = n + 1
result(n)%string = hash_table(i)%string(j)
result(n)%count = hash_table(i)%count(j)
end do
end do
! 按照出现次数从高到低排序
do i = 1, n - 1
do j = i + 1, n
if (result(i)%count < result(j)%count) then
type(string_count_type) :: temp
temp = result(i)
result(i) = result(j)
result(j) = temp
end if
end do
end do
! 输出结果
do i = 1, n
write(*, '(a, i10)') result(i)%string, result(i)%count
end do
end program count_and_sort_strings
function hash_function(str) result(hash_value)
character(len=*), intent(in) :: str
integer :: hash_value, i
hash_value = 0
do i = 1, len(str)
hash_value = mod(31 * hash_value + ichar(str(i)), hash_table_size)
end do
end function hash_function
性能优化点解释
- 逐行读取:避免一次性将大文件读入内存,减少内存占用。
- 哈希表:使用哈希表存储字符串及其出现次数,提高查找和插入的效率,平均时间复杂度为O(1)。
- 排序算法:选择简单的冒泡排序算法,虽然时间复杂度为O(n^2),但对于数据量不是特别大的情况,实现简单且性能可以接受。如果数据量非常大,可以考虑使用更高效的排序算法,如快速排序或归并排序。
- 哈希函数:设计一个简单的哈希函数,将字符串映射到哈希表的索引,尽量减少哈希冲突。